An Light-Dependent Resistor – LDR (sometimes called a photocell) is a special type of resistor. When light hits the LDR, its resistance is very low, but when it’s in the dark its resistance is very high.
By placing a capacitor in series with an LDR, the capacitor will charge at different speeds depending on whether it’s light or dark.
What I used
- Photoresistor 5K to10K Ohm Light Dependent Resistor
- Electrolytic Radial Capacitor
- Breadboard Jumper Cables
- 830 Point Solderless Breadboard
- Raspberry Pi
- SD card with With Raspberry Pi OS
- Keyboard and mouse
- Screen or Monitor or even a TV
We will assume that you have your raspberry pi setup and ready to go. If you need help with this visit our blog
Creating a light-sensing circuit
Place an LDR into your breadboard much like and LED, except as it is already a resistor no resistor is needed. Now place a capacitor in series with the LDR. As the capacitor is a polar component, you must make sure the long leg is on the same track as the Light-Dependent Resistor leg. Finally add jumper cables to the neg gutter and GPIO pin ( eg. GPIO25)
Coding a light sensor
Luckily, most of the complicated code you would have to write to detect the light levels received by the LDR has been abstracted away by the gpiozero library. This library will handle the timing of the capacitor’s charging and discharging for you.
Use the following code to set up the light sensor:
from gpiozero import LightSensor
ldr = LightSensor(25) # alter if using a different pin
Run this code, then cover the LDR with your hand and watch the value change. Try shining a strong light onto the LDR.