Built-in project · Raspberry Pi Pico W

Pico W photoresistor divider on GP28

MicroPython on a Pico W: a photoresistor and a 10k divide 3.3 V with the middle on GP28, and ADC(28) prints the light level twice a second. The whole build, a Raspberry Pi Pico W and 2 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.

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

The circuit runs here once the page has loaded.

What you'll learn

  • A voltage divider with a sensor
  • How a photoresistor changes with light
  • Reading an analog pin with ADC and read_u16
  • Why this circuit reads lower in bright light
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 4

How it works

A 10k resistor from 3.3 V and the photoresistor to ground form a voltage divider. The middle point goes to GP28. A photoresistor's resistance drops in bright light, so more light pulls the middle point toward ground.

That means more light gives a lower reading here. The script reads GP28 with read_u16 and prints it as light every half second.

Parts list

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

Kits that can build it

These 2 starter kits have 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, 10k Ω pin 2Raspberry Pi Pico W pin GP28
Light sensor pin 1Raspberry Pi Pico W pin GP28
Resistor, 10k Ω pin 13.3 V
Light sensor pin 2Ground

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 W
# Feel the light: a 10k from 3.3 V and the photoresistor to ground make a
# divider with its middle on GP28. More light, less resistance in the
# photoresistor, so a lower reading.
from machine import ADC
import time

light = ADC(28)

while True:
    print("light", light.read_u16())
    time.sleep_ms(500)

Troubleshooting

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

  • The reading goes down when you add light This project
    That is correct for this wiring. The photoresistor is on the ground side, so more light means a lower reading. Swap the two parts to flip it.
  • The reading is stuck at the top or bottom This project
    Check that the middle of the divider, not an end, goes to GP28.
  • 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.
  • Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
    Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts.
  • WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
    Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.

Change it and keep it

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