Build it step by step
0 of 7 doneWire the ring
5V to VBUS, GND to GND, DIN through 330 ohms to GP15.
Build the divider
LDR from 3V3(OUT) to GP26, 10 kilo-ohms from GP26 to GND.
Run the program
In a bright room the ring is dark.
Cover the LDR with your hand
The beam fades in and sweeps round.
Uncover it
The beam fades away.
Tune the numbers
Change DARK_BELOW and LIGHT_ABOVE to suit your room.
Save as main.py
So it starts on power-up.
How do you build a lighthouse beam at dusk?
A Pico sweeps a bright beam round a 16-LED ring like a lighthouse, but only after a photoresistor sees that the room has gone dark, and fades away at dawn. You need Raspberry Pi Pico H, WS2812B ring 16 LEDs, LDR Photoresistor, Resistor 10 kΩ, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 7 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 6
Parts list
- Development BoardsBuy
- OptoelectronicsBuy
- SensorsBuy
- PassivesBuyResistor 10 kΩ × 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 |
|---|---|---|
| Ring 5V | Pico VBUS pin 40 | 5 V from USB (brightness is kept low) |
| Ring GND | Pico GND pin 38 | Common ground |
| Ring DIN | 330 ohm resistor to Pico GP15 pin 20 | Data line |
| Photoresistor leg 1 | Pico 3V3(OUT) pin 36 | Top of the divider |
| Photoresistor leg 2 | Pico GP26 pin 31 and the 10 kilo-ohm resistor | Divider output |
| 10 kilo-ohm resistor other end | Pico GND pin 33 | Bottom of the divider |
Code
A photoresistor, or LDR, is a resistor whose resistance falls when light falls on it. With a 10 kilo-ohm resistor it forms a voltage divider, and the voltage at the middle goes up in bright light and down in the dark, which the Pico reads with its analog input. The program averages eight readings and decides it is dark below one number and light again above a higher number. Using two numbers, called hysteresis, stops it flickering at dusk. When it is dark the beam fades in over a few seconds, and when it is light it fades out. The beam is drawn as one bright LED with a dimmer tail behind it, moving one step at a time round the ring, like a lighthouse lamp. The WS2812B ring has a control chip in each LED, and the 330 ohm resistor protects the first one on the data line. The brightness is held to twenty percent, which keeps the current well within what a USB port can give and is gentler on the eyes. Do not look into the ring at close range. Point the photoresistor away from the ring so the beam does not turn it off.
from machine import Pin, ADC
import neopixel
import time
N = 16
ring = neopixel.NeoPixel(Pin(15), N)
ldr = ADC(26)
DARK_BELOW = 18000 # the reading goes down as the room gets dark
LIGHT_ABOVE = 24000 # a higher number for waking again (avoids flicker)
MAX_BRIGHT = 0.2 # keep the ring dim: 20 percent
dark = False
level = 0.0
pos = 0
def scaled(rgb, k):
return (int(rgb[0] * k), int(rgb[1] * k), int(rgb[2] * k))
while True:
total = 0
for _ in range(8):
total += ldr.read_u16()
reading = total // 8
if reading < DARK_BELOW:
dark = True
elif reading > LIGHT_ABOVE:
dark = False
target = 1.0 if dark else 0.0
level += (target - level) * 0.05 # fade in and out over a few seconds
for i in range(N):
ring[i] = (0, 0, 0)
beam = (255, 210, 120)
ring[pos] = scaled(beam, level * MAX_BRIGHT)
ring[(pos - 1) % N] = scaled(beam, level * MAX_BRIGHT * 0.35)
ring[(pos - 2) % N] = scaled(beam, level * MAX_BRIGHT * 0.10)
ring.write()
if level > 0.02:
pos = (pos + 1) % N # sweep only while the beam is on
time.sleep_ms(90)Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Bright room | Ring off |
| Cover the LDR | Beam fades in over a few seconds |
| Beam | One bright LED with a tail sweeping round |
| Uncover the LDR | Fades out |
| Dim light | No flicker between on and off |
If it does not work
Ring never lights
Check the data arrow, 5 V and the order of the wires.
Always lit
Raise DARK_BELOW, or move the LDR out of the shade.
Never lights
Lower DARK_BELOW and LIGHT_ABOVE, or check the divider wiring.
Flickers
Widen the gap between the two numbers.
Wrong colour
The ring may use a different order, swap the colour values.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the lighthouse beam at dusk?
Raspberry Pi Pico H, WS2812B ring 16 LEDs, LDR Photoresistor, Resistor 10 kΩ, Resistor 330 Ω and Breadboard 830 Tie Points.
How long does the lighthouse beam at dusk take to build?
About 120 minutes for a beginner maker, in 7 steps.
How do I know the lighthouse beam at dusk works?
Bright room: Ring off. Cover the LDR: Beam fades in over a few seconds. Beam: One bright LED with a tail sweeping round.
Ring never lights: what should I check?
Check the data arrow, 5 V and the order of the wires.
Always lit: what should I check?
Raise DARK_BELOW, or move the LDR out of the shade.
Never lights: what should I check?
Lower DARK_BELOW and LIGHT_ABOVE, or check the divider wiring.
Flickers: what should I check?
Widen the gap between the two numbers.
