Engineering · Raspberry Pi · Project 29 of 47 · Camera and vision · Beginner
Auto night light from the camera
The camera measures how bright the room is. When it gets dark the LED lamp comes on, and it goes off again in daylight. No light sensor needed: the camera is the sensor.

Privacy and safety. A camera records people. Tell everyone who lives in or visits the place, keep the pictures private, and delete old ones. Switch the Pi off before you plug in or remove the camera ribbon cable.
Parts in this circuit
| Part | Type |
|---|---|
| Pi Camera | Pi Camera Module 3 |
| Night lamp | LED |
Wiring, pin by pin
Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Coloured | Night lamp - anode + | Pin 11 (GPIO17) |
| 2 | Black | Night lamp - cathode - | Pin 6 (GND) |
Python code (gpiozero)
from picamera2 import Picamera2
from gpiozero import LED
from time import sleep
cam = Picamera2()
cam.configure(cam.create_preview_configuration())
cam.start()
sleep(1)
lamp = LED(17)
DARK = 40 # lamp on below this brightness
BRIGHT = 55 # lamp off above this brightness
while True:
level = cam.capture_array().brightness() # 0 = black, 255 = white
print("Brightness:", level)
if level < DARK:
lamp.on()
elif level > BRIGHT:
lamp.off()
sleep(0.5)
How the code works
- brightness() gives the average brightness of all the pixels in the picture.
- Two limits, DARK and BRIGHT, leave a gap between switching on and switching off. With one limit the lamp would flicker at dusk, when the light hovers around that value.
- Between 40 and 55 the lamp keeps the state it had.
You will learn
- Measuring light with a camera
- Hysteresis: two thresholds so the lamp does not flicker
- Using numbers to decide
Try this
- Slide the Room light from 85 % down to 0 % and watch the numbers.
- Move DARK and BRIGHT closer together and see the lamp flicker.
- Add a second LED that shows "dim" between the two limits.
On a real Pi
- For a real 230 V lamp, switch it with a relay module in a closed box (see the Relay project). Never wire mains to the Pi.
- A camera is more costly than an LDR. Use the camera when you want the picture too.
- Ignore the first picture after start: the camera needs about a second to set its exposure.
Parts to buy
The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 5 mm LED, white | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 330 ohm resistor, 1/4 W One per LED | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | Raspberry Pi Camera Module 3 (12 MP, autofocus) Or a Camera Module 2 / HQ camera | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | CSI camera ribbon cable, 15-pin, 30 cm Check the port size of your board | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | Camera mount or small tripod | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.