Built-in project · Raspberry Pi Pico

MicroPython night lamp on a Pico: light sensor on GP26

MicroPython on a Raspberry Pi Pico: a photoresistor and a 10k on GP26 set the LED on GP15, so the darker it gets, the brighter the LED. The whole build, a Raspberry Pi Pico and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $4.30. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • A voltage divider with a light sensor
  • Reading the ADC as a 16-bit number
  • PWM duty as LED brightness
  • Turning a reading into a percent
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico
  • Parts: 6

How it works

A 10k resistor goes to 3.3 V and the light sensor goes to ground, with GP26 at the point between them. In the dark the sensor's resistance climbs, so the voltage on GP26 rises. The ADC reads that voltage as a number from 0 to 65535.

The program uses the same number as the PWM duty for the red LED on GP15, so less light gives a brighter lamp. It also prints how dark it is as a percent, only when that changes by more than 2.

Parts list

6 parts, plus the jumper wires. They retail for about $4.30; on Mokxi they're free.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω pin 1Raspberry Pi Pico pin GP15
Resistor, 10k Ω pin 2Raspberry Pi Pico pin GP26
Light sensor pin 1Raspberry Pi Pico pin GP26
Resistor, 10k Ω pin 13.3 V
Light sensor pin 2Ground
LED, red pin AResistor, 220 Ω pin 2
LED, red pin CGround

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico
# A night lamp: a photoresistor from GP26 to ground and a 10k from 3.3 V.
# Less light, more resistance, a higher voltage on GP26, and the LED on
# GP15 gets brighter.
from machine import ADC, Pin, PWM
import time

sensor = ADC(26)
led = PWM(Pin(15))
led.freq(10000)
last = -1

while True:
    value = sensor.read_u16()
    led.duty_u16(value)
    percent = value * 100 // 65535
    if abs(percent - last) > 2:
        print("dark", percent, "%")
        last = percent
    time.sleep_ms(50)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The lamp gets dimmer in the dark. This project
    The sensor and the 10k resistor may be swapped. The 10k goes to 3.3 V and the sensor goes to ground.
  • The LED does not light. This project
    Check its direction. The anode (A) connects to the 220 ohm resistor from GP15, and the cathode (C) to ground.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The reading barely changes, or sits stuck near 0 or the top of the range. Light sensor
    An LDR is a resistor, so use it in a divider with a fixed resistor, about 10k, and read the middle node. Without the divider the analog pin sees nothing useful.
  • Readings go the wrong way: higher in the dark. Light sensor
    Which side the LDR sits on decides the direction. LDR on the supply side gives a higher reading in the light. LDR on the ground side gives a lower reading in the light. Swap them if needed.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.