Interfacing LDR (Light Sensor) with Raspberry Pi: Measure Light Intensity
Learn how to interface an LDR sensor with Raspberry Pi to detect ambient light levels. Build automatic lighting systems, smart gardens, and light-sensitive devices using Python.
1. Introduction to Raspberry Pi and LDR
An LDR (Light Dependent Resistor) changes its resistance based on light intensity. You can use it with Raspberry Pi to create automatic lights, smart blinds, or day/night detectors.
Why Use LDR with Raspberry Pi?
- Detect day and night for automation
- Control appliances based on light level
- Essential for smart environment sensing
2. Components and Tools You’ll Need
Required:
- Raspberry Pi (any model with GPIO)
- LDR (Photoresistor)
- 10kΩ Resistor
- Breadboard
- Jumper wires
- MCP3008 ADC (for analog reading)
Optional but Helpful:
- OLED/LCD to display light values
- LED or Buzzer for output trigger
- Graphing software (like Matplotlib)
3. Understanding the LDR Sensor
LDR is a passive component whose resistance decreases with increasing light. It cannot be directly connected to Raspberry Pi GPIO since Pi doesn’t read analog signals.
LDR Working Principle:
- High resistance in dark, low in bright light
- Forms voltage divider with a fixed resistor
- Output is analog voltage depending on light
4. Circuit Diagram: Wiring LDR with Raspberry Pi
Wiring via MCP3008 (Analog to Digital Converter):
- Connect LDR in series with a 10kΩ resistor (voltage divider)
- Connect the middle point to CH0 of MCP3008
- Connect MCP3008 to Raspberry Pi using SPI
- Power the circuit via 3.3V and GND of Pi
5. Installing Required Libraries
Install SPI and ADC libraries:
sudo apt-get update
sudo apt-get install python3-pip
sudo pip3 install spidev
Note: Enable SPI using `sudo raspi-config` > Interfacing Options > SPI > Enable
6. Python Code to Read Light Intensity
- Create a file: nano ldr_sensor.py
- Paste the code below:
import spidev
import time
# Create SPI
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1350000
def read_channel(channel):
adc = spi.xfer2([1, (8 + channel) << 4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
return data
try:
while True:
light_level = read_channel(0)
print("Light Intensity:", light_level)
time.sleep(1)
except KeyboardInterrupt:
print("\nExiting")
Run Command: python3 ldr_sensor.py
Output: Light Intensity: 853 (Value changes based on light brightness)
7. Troubleshooting Common Issues
Check These If Things Don’t Work:
- Is SPI enabled in Pi’s config?
- Are MCP3008 connections correct?
- Is the LDR facing light? Try a torch to test.
Verify Wiring with:
gpio readall (or use breadboard diagram tools)
8. Cool Projects You Can Build
1. Automatic Street Light
Turn on an LED automatically at night based on LDR reading.
2. Smart Blinds
Close or open blinds based on light level using a motor.
3. Plant Light Monitor
Track whether your indoor plant is getting enough light.
9. Bonus: Raspberry Pi Pico with LDR (Without ADC)
Use RC timing method to read LDR without an ADC
Measure how long it takes to charge a capacitor via LDR.
from machine import Pin
import time
ldr = Pin(16, Pin.OUT)
while True:
ldr.init(Pin.OUT)
ldr.value(0)
time.sleep_ms(10)
ldr.init(Pin.IN)
start = time.ticks_us()
while ldr.value() == 0:
pass
duration = time.ticks_diff(time.ticks_us(), start)
print("Light level:", duration)
time.sleep(1)
10. Optimization Tips
- Smooth readings using averaging techniques
- Use multiple LDRs for better light coverage
- Plot real-time graph using Matplotlib or thingspeak API
- Combine with RTC module for time-based logging
11. FAQs: Raspberry Pi with LDR Sensor
Q: Why do I need MCP3008?
A: Raspberry Pi has no built-in analog input. MCP3008 converts analog signal from LDR to digital.
Q: Can I use LDR directly with Pi?
A: No, not directly for analog. Use MCP3008 or RC time delay technique (for Pico).
Q: What’s the typical LDR resistance range?
A: LDRs range from ~1kΩ (bright light) to ~1MΩ (dark).
12. Conclusion: What You’ve Learned
- LDR basics and voltage divider configuration
- Why analog signal needs an ADC for Raspberry Pi
- Python code to read light intensity
- Project applications like auto lights and smart gardening