Mission Brief: Approach to the Inmost Cave
Deep beneath Neon City's bustling market, Kai and Orion find what they've been hunting for: a hidden underground lab — the perfect hideout. But a base is only safe if it knows when someone's coming. Before they can settle in, they need a way to sense the world outside the door: to catch the instant a shadow falls across the entrance and an intruder slips into the dark.
Your Raspberry Pi Pico W can't see, but it can sense light. Wire up a photoresistor — a component whose resistance changes with light — and the board gains an eye. Feed that reading into an analog input, set a threshold, and you've built the first piece of the lab's security system: a light-sensitive alarm that fires the moment the light level drops. This is the first time your circuit reacts to the world around it instead of just running a fixed pattern.
What You'll Learn
By the end of this mission you'll be able to:
- Read an analog signal with the Pico's ADC (analog-to-digital converter) instead of a simple on/off digital signal.
- Use a photoresistor in a voltage divider so changing light becomes a changing voltage the board can measure.
- Make a decision in code with an
if / elsestatement and drive an output based on a live sensor reading.
Understanding Light Sensors and Analog Input
A photoresistor (also called an LDR, for “light-dependent resistor”) changes its resistance based on how much light hits it. In bright light its resistance drops; in darkness it climbs. On its own that change is invisible to the board — so we pair it with a fixed resistor to form a voltage divider. As the photoresistor's resistance shifts, the voltage at the middle of the divider rises and falls with the light.
That middle point connects to pin GP26, one of the Pico's ADC pins. The digital signals you used before were just on and off — HIGH or LOW. An analog pin is different: it measures the actual voltage and turns it into a number, telling you how bright or dark the area is. In MicroPython, read_u16() gives you that reading as a value from 0 to 65535 — pick a threshold in that range and your program can tell “dark” from “bright.”
What You'll Need
- Raspberry Pi Pico W
- USB cable
- Breadboard
- 1 LED
- 1 photoresistor (LDR)
- 220Ω resistor (for the LED)
- 10kΩ resistor (for the photoresistor voltage divider)
- Jumper wires
Wiring Your Light Sensor
Follow the diagram above for the exact layout. Wire the LED first: its long leg (the anode) connects to pin GP15, and its short leg returns to the ground rail through the 220Ω resistor. Now build the photoresistor circuit: put the photoresistor on the breadboard, run one leg to the ground rail through the 10kΩ resistor, and connect the opposite leg to the 3.3V pin. The junction on the ground side of the photoresistor connects to GP26, the ADC pin. Double-check every connection before powering up — a loose jumper is the most common reason a sensor reads garbage.
Writing the Code
Import what you need
from machine import Pin, ADC
import timePin controls the GPIO pins, ADC reads analog values, and time lets you add delays so the loop doesn't run faster than you can watch.
Set up the LED and the photoresistor
# Initialize the LED and photoresistor
led = Pin(15, Pin.OUT)
photoresistor = ADC(Pin(26))Pin(15, Pin.OUT) makes GP15 an output for the LED. ADC(Pin(26)) turns GP26 into an analog input so you can read the light level as a number.
Read the light and react
while True:
# Read the light level from the photoresistor
light_level = photoresistor.read_u16()
# Turn the LED on when it gets dark, off when it is bright
if light_level < 20000:
led.on()
else:
led.off()
time.sleep(0.5)The while True loop runs continuously so the alarm watches the light in real time. Each pass reads the current light level, then decides: if the reading falls below 20000, the board treats it as “dark — something's blocking the light” and switches the LED on; otherwise it turns the LED off. The time.sleep(0.5) keeps the loop steady. Leaving print(light_level)