Ten things to build
The lessons teach one idea at a time. These builds put the ideas together into something that does a job. Each one is complete: paste circuit.py and main.py into the editor, press Run, and it works. Then change it.
They get harder as you go down the page. Every build names the lessons it uses, so if something is new, read that lesson first. Every build is also a practice problem, so you can press Run tests to see whether your version still does the job.
| # | Build | You will use | Lessons |
|---|---|---|---|
| 1 | Traffic light | three outputs, a timed sequence | 1, 2 |
| 2 | Light switch | a button, debouncing, remembering on/off | 3, 4 |
| 3 | Reaction timer | waiting for an input, the millisecond clock | 2, 3 |
| 4 | Night light | a light sensor, the ADC, a threshold | 5 |
| 5 | Blink-speed knob | a potentiometer controlling time | 2, 5 |
| 6 | Breathing LED | PWM duty | 6 |
| 7 | Doorbell | PWM frequency, notes | 3, 6 |
| 8 | Pointer dial | mapping a voltage to an angle | 5, 6 |
| 9 | Press counter on a screen | the OLED, drawing text | 4, 7–10 |
| 10 | Parking sensor | an ultrasonic sensor, distance | 13–14 |
Before you start. Read
circuit.pybefore you press Run. It is the whole wiring diagram in four or five lines, and every bug in these builds is easier to find once you know which pin goes where.
1. Traffic light
Three LEDs take turns: red, then green, then a short amber.
from wiring import Pico, LED, Resistor
pico = Pico()
pico.GP13 >> Resistor(330) >> LED("red") >> pico.GND
pico.GP16 >> Resistor(330) >> LED("green") >> pico.GND
pico.GP17 >> Resistor(330) >> LED("yellow") >> pico.GNDfrom machine import Pin
import utime
red = Pin(13, Pin.OUT)
green = Pin(16, Pin.OUT)
amber = Pin(17, Pin.OUT)
while True:
red.on()
utime.sleep(1.0)
red.off()
green.on()
utime.sleep(1.0)
green.off()
amber.on()
utime.sleep(0.4)
amber.off()How it works
- Each LED has its own pin and its own resistor. The three circuits share only ground.
- The loop is the sequence. Exactly one light is on at any moment, because each one is switched off before the next comes on.
Make it yours
- Real UK lights show red and amber together before green. Add that step.
- Put the three times in variables at the top, so the sequence is easy to retime.
Problem: Traffic light. Next step: Pedestrian crossing.
2. Light switch
One press turns the LED on; the next turns it off. This is the most common input pattern there is.
from wiring import Pico, LED, Resistor, Button
pico = Pico()
pico.GP13 >> Resistor(330) >> LED("red") >> pico.GND
pico.GP12 >> Button() >> pico.V3_3from machine import Pin
import utime
button = Pin(12, Pin.IN, Pin.PULL_DOWN)
led = Pin(13, Pin.OUT)
held = False
while True:
pressed = button.value() == 1
if pressed and not held:
utime.sleep_ms(20) # let the contacts settle
if button.value() == 1:
led.toggle()
held = True
elif not pressed:
held = False
utime.sleep_ms(5)How it works
- The button joins GP12 to 3.3 V, so the pin needs a pull-down to read 0 when nobody presses it.
heldremembers that the button is already down. Without it, holding the button would flip the LED two hundred times a second.- Real contacts bounce for a few milliseconds. Waiting 20 ms and reading again ignores the bounce.
Make it yours
- Count the presses and
printthe count each time. - Make a long press (over one second) switch the LED off whatever its state.
Problem: Press to toggle.
3. Reaction timer
The LED comes on. Press the button as fast as you can; the console shows how long you took.
from wiring import Pico, LED, Resistor, Button
pico = Pico()
pico.GP13 >> Resistor(330) >> LED("red") >> pico.GND
pico.GP12 >> Button() >> pico.V3_3from machine import Pin
import utime
led = Pin(13, Pin.OUT)
button = Pin(12, Pin.IN, Pin.PULL_DOWN)
led.on()
while button.value() == 0:
utime.sleep_ms(2)
print("reaction", utime.ticks_ms())How it works
while button.value() == 0waits, checking every 2 ms, until the press.utime.ticks_ms()is a clock that started at zero when the program did, so it is the reaction time here.
Make it yours
- Wait a random time before the LED comes on (
import random, thenutime.sleep_ms(random.randint(1000, 3000))), and measure from the moment it lights withticks_diff. - Catch cheats: if the button is already down when the LED comes on, print
too early.
Problem: Reaction timer. Next step: Reaction game.
4. Night light
An LED comes on by itself when the room gets dark.
from wiring import Pico, LED, LDR, Resistor
pico = Pico()
pico.GP13 >> Resistor(330) >> LED("red") >> pico.GND
ldr = LDR()
pico.V3_3 >> ldr.A
ldr.B >> pico.GP26
pico.GP26 >> Resistor(10_000) >> pico.GNDfrom machine import ADC, Pin
import utime
light = ADC(26)
led = Pin(13, Pin.OUT)
while True:
reading = light.read_u16()
led.value(1 if reading < 32768 else 0)
utime.sleep_ms(50)How it works
- The light-dependent resistor (LDR) and the 10 kΩ resistor form a voltage divider. In light the LDR's resistance falls and GP26 rises towards 3.3 V; in the dark it falls towards 0 V.
read_u16()turns that voltage into 0–65535. Below half way (32768) counts as dark.
Make it yours
printthe reading, cover the LDR in the simulator, and pick a better threshold from what you see.- At exactly the threshold the LED flickers. Use two thresholds, as in Sticky dimmer, so it switches cleanly.
Problem: Night light.
5. Blink-speed knob
Turn a knob, and the LED blinks faster or slower.
from wiring import Pico, LED, Potentiometer, Resistor
pico = Pico()
pico.GP13 >> Resistor(330) >> LED("red") >> pico.GND
pot = Potentiometer(10_000)
pico.V3_3 >> pot.pin3
pot.pin1 >> pico.GND
pot.W >> pico.GP26from machine import ADC, Pin
import utime
pot = ADC(26)
led = Pin(13, Pin.OUT)
while True:
stage = pot.read_u16() / 65535
half_ms = int(200 + (1 - stage) * 800)
led.on()
utime.sleep_ms(half_ms)
led.off()
utime.sleep_ms(half_ms)How it works
- A potentiometer is a voltage divider you can turn. Its wiper (
W) gives any voltage from 0 to 3.3 V. stageis the knob's position from 0 to 1. The sum200 + (1 - stage) * 800turns that into a half-blink of 1000 ms at one end and 200 ms at the other. This mapping is the core idea of the build: read a number, scale it, use it.
Make it yours
- Swap the direction, so turning the knob up makes the blink slower.
- Print
stageas a percentage, rounded:print(round(stage * 100), "%").
Problem: Turn the speed.
6. Breathing LED
The LED glows up from off to full brightness, in steps you can see.
from wiring import Pico, LED, Resistor
pico = Pico()
pico.GP13 >> Resistor(330) >> LED("red") >> pico.GNDfrom machine import Pin, PWM
import utime
pwm = PWM(Pin(13), freq=1000)
step = 65535 // 8
for duty in range(0, 65536, step):
pwm.duty_u16(min(duty, 65535))
utime.sleep(0.2)
utime.sleep(1)How it works
- PWM switches the pin on and off a thousand times a second. The duty is how much of each pulse is on, from 0 to 65535. The LED is too slow to flicker that fast, so it looks dimmer or brighter.
- The
forloop raises the duty in eight equal steps.
Make it yours
- Make it breathe: wrap it in
while True, and add a second loop that fades back down. - Use 64 steps of 20 ms instead of 8 steps of 0.2 s. The fade gets smooth.
Problem: PWM fade. Next step: One-second glow.
7. Doorbell
Press the button and a buzzer plays three rising notes.
from wiring import Pico, PassiveBuzzer, Resistor, Button
pico = Pico()
pico.GP13 >> Resistor(330) >> PassiveBuzzer() >> pico.GND
pico.GP12 >> Button() >> pico.V3_3from machine import PWM, Pin
import utime
button = Pin(12, Pin.IN, Pin.PULL_DOWN)
pwm = PWM(Pin(13), freq=262, duty_u16=0)
while True:
if button.value() == 1:
break
utime.sleep_ms(5)
pwm.duty_u16(32768)
for note in (262, 330, 392):
pwm.freq(note)
utime.sleep(0.3)
pwm.duty_u16(0)How it works
- A passive buzzer makes whatever tone you drive it at. In build 6 the duty set the brightness; here the duty stays at half (32768) and the frequency sets the note.
- 262, 330 and 392 Hz are the notes C, E and G, a major chord played one note at a time.
Make it yours
- Let it ring on every press, not just the first: put the waiting loop and the tune inside one
while True. - Play a tune you know. A table of note frequencies is easy to find; 440 Hz is the A above middle C.
Problem: Doorbell. Next step: Three notes.
8. Pointer dial
Turn a knob and a servo arm follows it, like the needle on a dial.
from wiring import Pico, Servo, Potentiometer
pico = Pico()
servo = Servo()
pico.GP12 >> servo.SIG
servo.V_PLUS >> pico.V5
servo.GND >> pico.GND
pot = Potentiometer(10_000)
pico.V3_3 >> pot.pin3
pot.pin1 >> pico.GND
pot.W >> pico.GP26from machine import ADC, Pin, PWM
import utime
pot = ADC(26)
pwm = PWM(Pin(12), freq=50)
while True:
pwm.duty_ns(int(500_000 + pot.read_u16() / 65535 * 2_000_000))
utime.sleep_ms(20)How it works
- A servo reads the width of a pulse sent 50 times a second: 0.5 ms points one way, 2.5 ms the other, and everything in between is an angle in between.
- The program maps the knob's 0–65535 onto 500 000–2 500 000 nanoseconds. It is the same mapping as build 5, onto a different range.
- The servo takes its power from 5 V, never from 3.3 V. Only its signal wire comes from the Pico.
Make it yours
- Limit the arm to the middle half of its travel.
- Print the angle in degrees: 0 at one end, 180 at the other.
Problem: Point where you look.
9. Press counter on a screen
Every press adds one to a number shown big on the OLED screen.
from wiring import Pico, OLED, Button
pico = Pico()
screen = OLED()
pico.GP4 >> screen.SDA
pico.GP5 >> screen.SCL
screen.VCC >> pico.V3_3
screen.GND >> pico.GND
pico.GP12 >> Button() >> pico.V3_3from machine import I2C, Pin
from ssd1306 import SSD1306_I2C
import utime
i2c = I2C(0, sda=Pin(4), scl=Pin(5))
frame = SSD1306_I2C(128, 64, i2c)
show = frame.show
button = Pin(12, Pin.IN, Pin.PULL_DOWN)
count = 0
held = False
def draw():
frame.fill(0)
frame.text(str(count), 56, 28, 1)
show()
draw()
print(count)
while True:
pressed = button.value() == 1
if pressed and not held:
utime.sleep_ms(20)
if button.value() == 1:
count += 1
draw()
print(count)
held = True
elif not pressed:
held = False
utime.sleep_ms(5)How it works
- The OLED talks I²C on two wires, SDA and SCL. The
ssd1306driver gives you a picture in memory calledframe. - Drawing happens in two steps: draw into
frame(fill,text), thenshow()sends the whole picture to the screen. Nothing appears until you callshow(). - The button code is build 2's, unchanged. Only what happens on a press is new.
Make it yours
- Add a second button on GP13 that sets the count back to zero.
- Centre the number. Each character is 8 pixels wide, so the left edge is
64 - len(str(count)) * 4.
Problem: Press counter on screen. Next step: A small menu.
10. Parking sensor
When something comes closer than 20 cm, a buzzer sounds and a warning LED lights.
from wiring import Pico, LED, Resistor, Buzzer, HCSR04
pico = Pico()
sonar = HCSR04()
pico.GP20 >> sonar.TRIG
sonar.ECHO >> Resistor(1000) >> pico.GP21
pico.GP21 >> Resistor(2000) >> pico.GND
sonar.VCC >> pico.P40
sonar.GND >> pico.GND
pico.GP13 >> Buzzer(kind="piezo") >> pico.GND
pico.GP15 >> Resistor(330) >> LED("red") >> pico.GNDfrom machine import Pin, time_pulse_us
import utime
trig = Pin(20, Pin.OUT)
echo = Pin(21, Pin.IN)
buzzer = Pin(13, Pin.OUT)
warning = Pin(15, Pin.OUT)
while True:
trig.value(1)
utime.sleep_us(10)
trig.value(0)
pulse = time_pulse_us(echo, 1, 30000)
distance = pulse / 58
close = 0 < distance < 20
buzzer.value(close)
warning.value(close)
utime.sleep_ms(100)How it works
- A 10 µs pulse on TRIG makes the sensor send a burst of sound. ECHO then stays high for as long as the sound took to bounce back.
time_pulse_usmeasures that in microseconds. - Sound travels about 1 cm every 29 µs, and the echo goes there and back, so dividing by 58 gives centimetres.
- The sensor runs on 5 V (pin 40, VBUS) and its ECHO is 5 V too. The 1 kΩ and 2 kΩ resistors divide it down to a safe 3.3 V before it reaches GP21.
0 < distancematters: if no echo comes back,time_pulse_usreturns a negative number.
Make it yours
- Beep faster as the object gets closer, like a real car: make the pause depend on
distance. - Show the distance on an OLED (build 9) instead of the console.
Problem: Parking sensor. Next steps: Distance alarm, then the capstone.
Where to go next
- The course lists every problem by module.
- Real loads shows how to switch a lamp, a fan or a motor that needs more power than a pin can give. Relay lamp and Fan thermostat are builds 2 and 4 with a relay.
- The capstone is the last step: design a small system of your own.