Engineering · Raspberry Pi · Project 11 of 47 · Sensors · Beginner
Automatic night lamp
A light sensor (LDR) switches a lamp on when it gets dark. It is the idea behind street lights and automatic machine lighting.

Parts in this circuit
| Part | Type |
|---|---|
| LDR | Light sensor |
| 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 | Orange | LDR - 3.3 V | Pin 1 (3V3) |
| 2 | Coloured | LDR - signal | Pin 7 (GPIO4) |
| 3 | Black | LDR - GND | Pin 6 (GND) |
| 4 | Coloured | Lamp - anode + | Pin 11 (GPIO17) |
| 5 | Black | Lamp - cathode - | Pin 9 (GND) |
Python code (gpiozero)
from gpiozero import LightSensor, LED
from signal import pause
ldr = LightSensor(4, threshold=0.3) # dark when below 30 %
lamp = LED(17)
def dark():
lamp.on()
print("It is dark: lamp ON")
def light():
lamp.off()
print("It is light: lamp OFF")
ldr.when_dark = dark
ldr.when_light = light
print("Drag the Daylight slider below 30 %")
pause()
How the code works
- An LDR has low resistance in light and high resistance in the dark. With a capacitor, the Pi times how fast the pin charges.
- LightSensor turns that timing into a value from 0 (dark) to 1 (bright).
- when_dark and when_light run your functions when the value crosses the threshold.
You will learn
- Light-dependent resistor (LDR)
- Thresholds
- Events from analog-like sensors
Try this
- Move the threshold to 0.5 and test again.
- Add a delay: the lamp should only turn on if it stays dark for 3 seconds.
- Print a message with the light level every second.
On a real Pi
- The LDR goes between 3.3 V and the GPIO pin. A 1 microfarad capacitor goes from the pin to GND.
- Add some hysteresis in real life, or the lamp will flicker at dusk.
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, yellow | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 330 ohm resistor, 1/4 W One per LED | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | LDR (GL5528) photoresistor | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | 1 µF capacitor | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | 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.