Build it step by step
0 of 6 doneBuild the divider
Photoresistor from 3V3(OUT) to GP27, 10 kilo-ohm from GP27 to GND.
Wire the LED
GP15 through 330 ohms, cathode to GND.
Switch the room light on
Then run the program.
Cover the sensor with your hand
The LED fades up.
Uncover it
The LED fades out.
Try a small change in light
No flicker.
How do you build a pico dusk night light?
A Pico switches an LED on at dusk using a photoresistor and fades it up and down with PWM, with no knob needed. You need Raspberry Pi Pico H, LDR Photoresistor, Resistor 10 kΩ, LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 6 steps and about 120 minutes.
- Level
- Intermediate
- Time
- About 120 min
- Parts
- 6
Parts list
- Development BoardsBuy
- SensorsBuy
- PassivesBuyResistor 10 kΩ × 1
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 330 Ω × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Photoresistor one leg | Pico 3V3(OUT) pin 36 | Top of the divider |
| Photoresistor other leg and 10 kilo-ohm resistor | Pico GP27 pin 32 (ADC1) | Divider output |
| 10 kilo-ohm resistor other leg | Pico GND | Bottom of the divider |
| LED anode | 330 ohm resistor to Pico GP15 pin 20 | Night light |
| LED cathode | Pico GND | Return |
Code
A photoresistor changes its resistance with light, and in series with a fixed resistor it forms a divider whose output voltage rises as the light gets brighter. The ADC turns the voltage into a number. The program takes the light level at start-up as a reference, so switch the room light on first, and decides it is getting dark when the reading falls below half of that. It switches back off only when the light rises above 65 percent of the reference. The gap between the two limits is called hysteresis, and it stops the LED flickering when the light sits right at the threshold. The LED is not switched on and off sharply: a PWM signal changes its brightness a little each cycle, which gives a smooth fade. Low voltage only, and the 330 ohm resistor protects the LED and the pin.
import time
from machine import ADC, Pin, PWM
ldr = ADC(27)
led = PWM(Pin(15))
led.freq(1000)
def read_light():
total = 0
for _ in range(8):
total += ldr.read_u16()
return total // 8
# Set the reference from the room light at start-up (switch the room light on first)
time.sleep(1)
reference = read_light()
ON_BELOW = reference * 5 // 10 # go dark below half the start-up light
OFF_ABOVE = reference * 65 // 100 # come back on above 65 percent: hysteresis
target = 0
duty = 0
while True:
light = read_light()
if light < ON_BELOW:
target = 30000 # about half brightness
elif light > OFF_ABOVE:
target = 0
if duty < target:
duty = min(duty + 300, target) # fade up
elif duty > target:
duty = max(duty - 300, target) # fade down
led.duty_u16(duty)
time.sleep_ms(20)Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Room light on | LED off |
| Sensor covered | LED fades on |
| Sensor uncovered | LED fades off |
| Light near the threshold | No flicker |
| Restart in a dim room | Reference is lower, so it needs darker to switch |
If it does not work
LED never lights
Check polarity, GP15 and the resistor.
LED always on
Run it with the room light on, or swap the divider parts.
Flickers
Widen the gap between ON_BELOW and OFF_ABOVE.
No reaction to light
Check the divider wiper goes to GP27.
Program does not start on power-up
Save it on the Pico as main.py.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pico dusk night light?
Raspberry Pi Pico H, LDR Photoresistor, Resistor 10 kΩ, LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points.
How long does the pico dusk night light take to build?
About 120 minutes for a intermediate maker, in 6 steps.
How do I know the pico dusk night light works?
Room light on: LED off. Sensor covered: LED fades on. Sensor uncovered: LED fades off.
LED never lights: what should I check?
Check polarity, GP15 and the resistor.
LED always on: what should I check?
Run it with the room light on, or swap the divider parts.
Flickers: what should I check?
Widen the gap between ON_BELOW and OFF_ABOVE.
No reaction to light: what should I check?
Check the divider wiper goes to GP27.
