Raspberry Pi Light Sensor Tutorial – Wire an LDR and Read Light Levels with Python
In this comprehensive Raspberry Pi light‑sensor tutorial, I walk you through wiring a photoresistor to the GPIO pins and reading the data with a concise Python script. The goal is to give you a hands‑on understanding of the circuit, the code, and practical applications such as a light‑activated alarm clock.
This project is ideal for beginners who want to explore sensor integration, yet it also serves as a foundation for more sophisticated projects.
Below, I detail the components, step‑by‑step wiring, and the Python logic that turns the Pi’s digital pins into a makeshift analogue measurement.
Equipment
Everything you’ll need is listed here. While a breadboard isn’t strictly required, it greatly simplifies the assembly process, especially if you plan to experiment further.
Recommended
Raspberry Pi
8 GB SD Card or a Micro SD Card for Pi 2/B+
Ethernet cable or Wi‑Fi dongle
Light sensor (LDR)
1 µF capacitor
Optional
Raspberry Pi case
USB keyboard
USB mouse
GPIO breakout kit
Breadboard
Breadboard wires
Video
For visual learners, the accompanying video demonstrates the complete wiring and shows the real‑time sensor output. The same information is provided in the text below.
Building the Light‑Sensor Circuit
The circuit is intentionally simple: an LDR, a 1 µF capacitor, and a single GPIO pin. The LDR’s resistance drops to a few hundred ohms in bright light and rises to several megohms in darkness. By measuring the RC time constant of the capacitor, the Pi can infer the light level even without analogue inputs.
- Connect pin #1 (3.3 V) to the breadboard’s + rail.
- Connect pin #6 (GND) to the breadboard’s – rail.
- Place the LDR on the breadboard and run a wire from one end to the + rail.
- Run a wire from the other end of the LDR to pin #7 on the Pi.
- Insert the 1 µF capacitor between the wire from pin #7 and the – rail, ensuring the negative lead goes to the – rail.
Refer to the diagram below for a visual layout. Once the hardware is in place, the Python code will convert the digital pin into a crude analogue measurement.
Python Code
The script below measures how long it takes for the capacitor to charge to 75 % of the supply voltage. This RC time is proportional to the LDR’s resistance, giving a rough estimate of ambient light.
#!/usr/local/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
# Pin that connects to the RC network
pin_to_circuit = 7
def rc_time(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(rc_time(pin_to_circuit))
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
Deploying & Running
On a Raspberry Pi running Raspbian, the required libraries are pre‑installed. If you need to install them manually, run:
sudo apt-get update
sudo apt-get install python3-rpi.gpio
Clone the repository and run the script:
git clone https://github.com/pimylifeup/Light_Sensor/
cd Light_Sensor
sudo python3 light_sensor.py
Alternatively, paste the code into a new file, save it as light_sensor.py, and execute the same command.
Enhancing Accuracy & Possible Applications
While an LDR provides a quick, low‑cost solution, its limited dynamic range and temperature sensitivity may be insufficient for precision work. For applications that demand higher accuracy, consider sensors such as the Adafruit high‑dynamic‑range digital light sensor.
- Light‑Activated Alarm: Use the LDR to trigger an alarm that ramps up as daylight increases.
- Garden Sunlight Monitor: Log the light intensity in different garden zones to inform planting decisions.
- Room‑Occupancy Alert: Detect unexpected light in a dark room and send a notification.
Feel free to experiment, tweak the RC network, or integrate the sensor into larger projects. If you encounter issues or have suggestions, drop a comment below.
Source: Raspberry Pi Light Sensor – A Simple LDR Tutorial
Manufacturing process
- Build a Raspberry Pi Temperature Logger with a $5 I2C Sensor
- Control an LED with a PIR Motion Sensor on Raspberry Pi
- Build a Low‑Cost Raspberry Pi Soil Moisture Sensor for Smart Irrigation
- Build a Multi‑Sensor Temperature & Light Monitoring System with Raspberry Pi & DS18B20
- Build a Raspberry Pi Geiger Counter – Open‑Hardware Radiation Sensor Tutorial
- Raspberry Pi–Based Bathroom Occupancy Sensor with Voice & SMS Alerts via Twilio
- Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
- Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2
- Build a Raspberry Pi Obstacle‑Avoiding Robot – A Beginner’s Guide
- Arduino Nano Fingerprint Sensor Project – Step‑by‑Step Tutorial