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.

A small circuit board with a yellow LED sits on one end of a seesaw. A sun and a moon sit on the other end.

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.

  1. Connect the Pico's 3V3(OUT) pin to one leg of the LDR.
  2. Connect the other leg of the LDR to GP26. Also connect it to one leg of the 10 kilohm resistor.
  3. Connect the other leg of the 10 kilohm resistor to any GND pin.
  4. Connect the LED's long leg to GP15 through the 330 ohm resistor.
  5. 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.
  • level turns the reading into a number from 0 (off) to 1 (full). Dark gives a high level.
  • PWM switches the LED on and off very fast. duty_u16 sets 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 DARK and BRIGHT. 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

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.

See the programme