Hardik's Notes · Build
Build it this weekend: a night light that dims
A light sensor, an LED and 25 lines of MicroPython on a Raspberry Pi Pico. The lamp gets brighter as the room gets darker.

What will you make?
A lamp that watches the room. In daylight the LED is off. As it gets dark the LED fades in. In a dark room it is fully on.
What do you need?
- A Raspberry Pi Pico (or Pico W) with MicroPython installed
- One light sensor (an LDR, a resistor whose value changes with light)
- One 10 kilohm resistor
- One LED and one 330 ohm resistor
- A breadboard and jumper wires
- A USB cable and a computer with Thonny or another MicroPython editor
How do you wire it?
The LDR and the 10 kilohm resistor make a voltage divider (two resistors that split a voltage). The Pico reads the voltage where they meet.
- Connect the Pico's 3V3(OUT) pin to one leg of the LDR.
- Connect the other leg of the LDR to GP26. Also connect it to one leg of the 10 kilohm resistor.
- Connect the other leg of the 10 kilohm resistor to any GND pin.
- Connect the LED's long leg to GP15 through the 330 ohm resistor.
- Connect the LED's short leg to any GND pin.
More light means a lower LDR resistance, so a higher voltage at GP26. That gives a higher reading in the code. The pins GP26 and GP15 are GPIO numbers, as in the MicroPython quick reference.
What is the code?
Save this as main.py on the Pico so it runs when you plug it in.
from machine import ADC, Pin, PWM
import time
ldr = ADC(Pin(26)) # reads the light sensor
led = PWM(Pin(15), freq=1000) # dims the LED
DARK = 8000 # reading in a dark room (change after step 5)
BRIGHT = 50000 # reading in a bright room (change after step 5)
while True:
total = 0
for _ in range(10):
total += ldr.read_u16()
time.sleep_ms(10)
light = total // 10
level = (BRIGHT - light) / (BRIGHT - DARK)
if level < 0:
level = 0
if level > 1:
level = 1
led.duty_u16(int(level * 65535))
print("light:", light, "led:", int(level * 100), "%")
time.sleep_ms(200)
How does it work?
ADC(Pin(26))reads the voltage as a number from 0 to 65535. MicroPython’s docs say the range is 0 to 3.3 volts.- The loop reads ten times and takes the average. That stops the LED from flickering.
levelturns the reading into a number from 0 (off) to 1 (full). Dark gives a high level.PWMswitches the LED on and off very fast.duty_u16sets how long it stays on, from 0 to 65535. A longer on-time looks brighter.
If it doesn’t work
- The LED never changes. Open the shell in Thonny and read the
light:numbers. If they stay the same, check the wire to GP26. - It is backwards. The LED is bright in daylight. Swap the LDR and the 10 kilohm resistor.
- It is always fully on or off. Your room’s numbers are outside
DARKandBRIGHT. Cover the sensor, note the number, then shine a torch on it and note that one. Put those two numbers in the code. - Nothing prints. Check the code is saved as
main.py, then unplug and replug the Pico.
Make it yours
- Add a button that turns the lamp off for the night.
- Use a second LED so the colour changes with the dark.
- Print the level as a row of
#signs to see it as a bar. - Make it slower, so the lamp fades over a few seconds.
What’s the catch?
LDRs differ from one to the next. The numbers in this code are guesses, so you must measure your own.
Sources
- Quick reference for the RP2 (PWM and ADC), MicroPython documentation
Keep reading
Three more from the notebook.
3 November 2026
How an AI found a new antibiotic, and won a prize
Two MIT scientists won a 2026 prize for using AI to find antibiotics. Here is how the AI did it, in plain words, and what it has not done yet.Read it1 November 2026
An AI small enough to fit in a PC's boot sector
A maker has made a tiny neural network that runs before any operating system loads. Here is how small an AI can really be.Read it31 October 2026
Four small AI machines, all built on one board
Arduino named the winners of a contest for its UNO Q board. Four of them use AI to sense water, eggs, rides and light.Read it
Beyond the notebook
This is what we do in schools.
A year-long programme for classes 6 to 12. Creativity, taught as a skill, inside the classroom you already have, led by your teachers.