Raspberry Pi Photocell Logging & Alert System – Build a Low‑Power Light Sensor with Python
This guide walks you through building a Raspberry Pi‑based photosensor that logs light levels and triggers alerts. Perfect for monitoring dark spaces like closets or equipment lockers, or simply keeping track of ambient illumination.
We’ll use LEDs as the output indicators, but the circuit can be adapted to control relays, buzzers, or even a notification service. All you need is an Adafruit RPi Starter Kit – a cost‑effective bundle that includes everything required – or you can assemble the parts yourself from the bill of materials below.
- 3 × 10 mm diffused LEDs (red, blue, green)
- 1 × 1 µF capacitor
- 1 × photocell (photoresistor)
- 3 × 560 Ω resistors
- 1 × Pi Cobbler breakout
- 10 × breadboard jumper wires
- 1 × full‑length solderless breadboard
Unlike a typical microcontroller, the Raspberry Pi lacks an onboard analog‑to‑digital converter. We overcome this by timing the charge of a capacitor through the photocell – an inexpensive RC‑timing technique that delivers reliable readings.
Prerequisites
Programming is done in Python. We recommend using Adafruit’s Occidentalis distribution, which includes the RPi.GPIO library and pre‑compiled modules for the Pi. If you prefer a minimal setup, simply install RPi.GPIO via pip.
Because the circuit consumes very little current, a standard Pi power supply is sufficient. For calibration, a variable light source (e.g., a desk lamp with adjustable brightness) will help you see how the sensor reacts to different illumination levels.
Step 1 – Wiring the RC Circuit
1. Insert the Cobbler into the breadboard, ensuring no pins touch the same power rail – otherwise you risk shorting the Pi.
2. Connect the 3.3 V pin to the breadboard’s positive rail and the GND pin to the negative rail.
3. Place the photocell on the breadboard’s gap between the two halves.
4. Wire one side of the photocell to the 3.3 V rail, and the other side to pin 18 on the Pi.
5. Connect the 1 µF capacitor from pin 18 to GND. This completes the RC network.
At this point you can proceed to calibrate the sensor.
Step 2 – Calibration & Testing
Run the following Python script. It continuously measures how long it takes for the capacitor to charge through the photocell. The returned value drops as light intensity increases.
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
RC_PIN = 18
def rc_time(pin):
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
time.sleep(0.1) # fully discharge the capacitor
GPIO.setup(pin, GPIO.IN)
count = 0
while GPIO.input(pin) == GPIO.LOW:
count += 1
return count
while True:
print(rc_time(RC_PIN))
time.sleep(0.5)
As you vary the light source, you’ll observe a clear numerical trend: higher light levels produce lower readings. Capture a few samples in the dark and under typical illumination to establish a threshold for triggering alerts.
Step 3 – Implementing Alerts
With the threshold defined, modify the script to turn LEDs on or off, log data to a file, or send a notification via email or MQTT. For example, to blink a red LED when the room becomes bright:
RED_LED = 17
GPIO.setup(RED_LED, GPIO.OUT)
while True:
level = rc_time(RC_PIN)
if level < THRESHOLD:
GPIO.output(RED_LED, GPIO.HIGH)
else:
GPIO.output(RED_LED, GPIO.LOW)
time.sleep(1)
For more complex workflows, consider integrating with Home Assistant, Node‑RED, or a custom Flask web service.
Safety & Best Practices
- Always double‑check pin connections before powering the Pi.
- Use a 3.3 V supply for the RC network; avoid 5 V which could damage the Pi’s GPIO.
- Keep the capacitor’s value low (1 µF) to prevent long charge times that slow down the script.
- Document your calibration data; it makes troubleshooting and future adjustments straightforward.
With this setup, you now have a reliable, low‑power light sensor that logs and reacts to illumination changes. Feel free to expand the project by adding multiple sensors, logging to a database, or visualizing the data on a web dashboard.
Manufacturing process
- Seamless MQTT Integration: NodeMCU DHT22 Sensors with Raspberry Pi 3 B+ as Broker
- Raspberry Pi–Controlled Aquaponics System: Build Guide & Code
- Log & Graph 24‑V Thermostat Events with an Optocoupler and Raspberry Pi
- Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
- Build a Multi‑Sensor Temperature & Light Monitoring System with Raspberry Pi & DS18B20
- Mastering Raspberry Pi Sensor & Actuator Control: Accelerometer, Servo, and Data Streaming
- Raspberry Pi Home Security System: PIR Motion Detection + Camera Email Alerts
- Raspberry Pi Tank: Adding Range & Bearing Sensors for Precise Navigation
- Smart Indoor Air Quality & Waste Monitoring System
- Build a Raspberry Pi 3 & Arduino Laptop: Step‑by‑Step Guide