Measuring light with Raspberry Pi

Measuring the quantity of light with photoresistor.

Setup

  +----- Photoresistor --+-- Condensator (10 uF) --+
  |                      |                         |
  o                      o                         o
PIN #17               PIN #11                    PIN #9
(3.3 V)               (GPIO 2)                  (Ground)

				

Python code:

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

def RCtime (pin):
   count = 0
   GPIO.setup(pin, GPIO.OUT)
   GPIO.output(pin, GPIO.LOW)
   time.sleep(0.1)
   
   GPIO.setup(pin, GPIO.IN)
   
   while (GPIO.input(pin) == GPIO.LOW):
      count += 1
   return count

try:
   while True:
      print RCtime(11)
except KeyboardInterrupt:
   pass
finally:
   GPIO.cleanup()

Execute the python script:

$ sudo python photoresistor.py

Stop the script with CTRL + C.