Light-dependent resistors (LDRs), also called photoresistors, are simple sensors whose resistance varies based on ambient light. In bright conditions, they exhibit low resistance; in darkness, their resistance increases significantly. This property makes them excellent for light-detecting projects such as automatic lighting, alarm systems, and environmental light monitoring.
What You’ll NeedRaspberry Pi Pico (W or standard)
- Raspberry Pi Pico (W or standard)
LDR sensor (raw LDR or preconfigured breakout module)
- LDR sensor (raw LDR or preconfigured breakout module)
Breadboard and jumper wires
- Breadboard and jumper wires
Optional: pull-down resistor (~10 kΩ) when using a raw LDR
- Optional: pull-down resistor (~10 kΩ) when using a raw LDR
You can connect the LDR to the Pico in two common ways:
Raw LDR + resistor in a voltage divider configuration:
Connect one end of the LDR to 3.3 V.
- Connect one end of the LDR to 3.3 V.
Attach the other end of the LDR to both the ADC input (e.g., GPIO 26/27/28) and to ground through a resistor.
- Attach the other end of the LDR to both the ADC input (e.g., GPIO 26/27/28) and to ground through a resistor.
As the LDR’s resistance changes with light, the voltage at the divider midpoint shifts accordingly.
- As the LDR’s resistance changes with light, the voltage at the divider midpoint shifts accordingly.
- Raw LDR + resistor in a voltage divider configuration:Connect one end of the LDR to 3.3 V.Attach the other end of the LDR to both the ADC input (e.g., GPIO 26/27/28) and to ground through a resistor.As the LDR’s resistance changes with light, the voltage at the divider midpoint shifts accordingly.
LDR breakout module:
These modules often contain an LDR, resistor divider, potentiometer, and dual outputs: an analog voltage (AO) and a digital signal (DO).
- These modules often contain an LDR, resistor divider, potentiometer, and dual outputs: an analog voltage (AO) and a digital signal (DO).
You can read analog variations via AO or set a light threshold with DO and the onboard potentiometer.
- You can read analog variations via AO or set a light threshold with DO and the onboard potentiometer.
- LDR breakout module:These modules often contain an LDR, resistor divider, potentiometer, and dual outputs: an analog voltage (AO) and a digital signal (DO).You can read analog variations via AO or set a light threshold with DO and the onboard potentiometer.
Use this simple wiring:
3.3 V → LDR → ADC pin
- 3.3 V → LDR → ADC pin
ADC pin → 10 kΩ resistor → GND
- ADC pin → 10 kΩ resistor → GND
This classic divider enables the Pico’s ADC to convert the analog voltage into a digital reading between 0 and 65, 535 Sample MicroPython Code
Here’s a basic script for reading analog values in Thonny:
python
CopyEdit
from machine import ADC
import utime
ldr = ADC(28) # could use 26 or 27
while True:
value = ldr.read_u16() # 0–65535 scale
volts = value / 65535 * 3.3
print(f"ADC={value} Voltage={volts:.2f} V")
utime.sleep(1)
python
CopyEdit
from machine import ADC
import utime
ldr = ADC(28) # could use 26 or 27
while True:
value = ldr.read_u16() # 0–65535 scale
volts = value / 65535 * 3.3
print(f"ADC={value} Voltage={volts:.2f} V")
utime.sleep(1)
This measures light intensity by printing the ADC value and calculated voltage
Using a Breakout Module (Analog & Digital)With modules like the KY‑018, you get both settings:
AO (Analog Output): Connect to ADC to capture varying light levels.
- AO (Analog Output): Connect to ADC to capture varying light levels.
DO (Digital Output): Reads HIGH/LOW based on light threshold—adjustable via potentiometer
- DO (Digital Output): Reads HIGH/LOW based on light threshold—adjustable via potentiometer
Sample code for digital reading:
python
CopyEdit
from machine import Pin
import utime
sensor = Pin(0, Pin.IN) # replace with correct GPIO
while True:
state = sensor.value()
print("Bright" if state == 0 else "Dark")
utime.sleep(1)
python
CopyEdit
from machine import Pin
import utime
sensor = Pin(0, Pin.IN) # replace with correct GPIO
while True:
state = sensor.value()
print("Bright" if state == 0 else "Dark")
utime.sleep(1)
Advanced ApplicationsYou can extend this basic setup to build real-world projects:
Automatic night lamps: Use the digital output to trigger LEDs or relay controls
- Automatic night lamps: Use the digital output to trigger LEDs or relay controls
Light meter: Continuously monitor light and log or graph values over time
- Light meter: Continuously monitor light and log or graph values over time
Solar trackers or intelligent curtains: Use threshold-based or analog readings to automate hardware movements.
- Solar trackers or intelligent curtains: Use threshold-based or analog readings to automate hardware movements.
Calibration: Ambient light responses differ between LDR units, so calibrate thresholds or voltage readings to your environment
- Calibration: Ambient light responses differ between LDR units, so calibrate thresholds or voltage readings to your environment
Analog resolution: Pico’s ADC provides 12-bit precision (0–4095) for raw readings, which you can scale accordingly
- Analog resolution: Pico’s ADC provides 12-bit precision (0–4095) for raw readings, which you can scale accordingly
Component variability: Combining an LDR with a resistor and voltage divider simplifies calibration and improves measurement consistency
- Component variability: Combining an LDR with a resistor and voltage divider simplifies calibration and improves measurement consistency
Comments