Now we can be able to measure the intensity of light in a room using a single photocell and a capacitor connected to the raspberry pi with a bit of code in python.
What is Photocell?
The Photocell is a light sensor in which the resistance varies according to the intensity of light. The resistance reduces when it is in brighter surroundings. We have to set up a threshold value for the measurements of the intensity because it cannot give the precise measurements. If the measurements are below the threshold then it is dark, else it is bright.
Role of a Capacitor
A Capacitor is an electrical component that can store electrical energy temporarily. It is measured in Farads which is characterized by capacitance. The capacitor consists of 2 conductors that can hold the electric charge and when it is fully charged the capacitor starts discharging. This kind of alternative behavior is used to generate AC.
When the switch is pressed the current starts flowing and the capacitor starts charging up. The capacitor stops charging when the voltage at its end reaches the voltage of the battery. Then as there is no potential difference in the upper half of the circuit, no current flows there.
Things needed
- A Raspberry pi
- 1 x breadboard
- A Photocell
- A Resistor
- A Capacitor ( 1 microfarad)
Circuit
We need to measure the resistance of the photoresistor. The Raspberry pi acts as the battery whereas the GPIO pin 1 provides 3.3 V to the photoresistor. Make the GPIO pin 12 as the bidirectional pin ( input and output pin). When the capacitor is charging it will take some time to reach a voltage that registers as high. GPIO pin 6 is grounded which is connected to the negative side of the capacitor (short end). Check how long it takes for the input pin to become high and use the result to calculate the resistance of the photocell.
- Insert a photocell in a breadboard.
- Connect the GPIO pin 1 (3.3 V) to the resistor which is connected serial to the Photocell.
- Connect the other end of the photocell to the GPIO pin 12 and the Capacitor as shown in the diagram.
- GPIO pin 6 ( ground) is connected to the other end of the capacitor ( short end ).
Code
#measuring the light intensity using a photocell import RPi.GPIO as GPIO,time,os #import the libraries DEBUG=1 GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) def RCtime(RCpin): # function start reading=0 GPIO.setup(RCpin,GPIO.OUT) GPIO.output(RCpin,GPIO.LOW) time.sleep(2) # time to discharge capacitor GPIO.setup(RCpin,GPIO.IN) while (GPIO.input(RCpin) == GPIO.LOW): # the loop will run till the capacitor is charged reading += 1 # measuring time which in turn is measuring resistance return reading # function while True: print RCtime(12) # calling the function
Output
- With light:
2. Without light: