← MicroPython on Pi Pico and Projects

Project 1.4

Project 1.4: Blinking Your LEDs

Write your first programs for the circuit you built, then make your three LEDs count, first in unary, then in binary

Checklist (covers 1.4 & 1.5) Slides (covers 1.4 & 1.5)

This is for after your three-LED circuit from Project 1.1 is signed off, Project 1.2 has MicroPython running on your Pico, and Project 1.3 has you comfortable saving a file to your Pico and typing commands straight into the REPL. If you finish early and your teacher is ready for you to move on, this is what comes next.

In Project 1.3 you turned the onboard LED on and off by typing commands straight into the REPL, one at a time. Now you’ll write those same commands into a script that does the on-off-on-off switching for you, automatically, forever.

Not sure where the onboard LED is? Chapter 4’s photo shows exactly where to look: it’s the small component to the left of the micro USB port.

Click in Thonny’s script area and type the following:

import machine
import time

led_onboard = machine.Pin("LED", machine.Pin.OUT)

while True:
    led_onboard.toggle()
    time.sleep(1)

Save it to your Pico, then click Run. Your Pico’s own onboard LED, the small one near the USB connector, should blink once every second.

If anything here looks unfamiliar, Chapter 4 covers the same machine.Pin and time.sleep ideas in more detail, using ViperIDE instead of Thonny. The code itself works the same either way.

Controlling an LED you wired yourself takes exactly one change: swap the pin.

Check your Project 1.1 Notes page for the GPIO pin number you recorded for one of your LEDs, then edit your program so the Pin line uses that number instead of "LED":

import machine
import time

led_one = machine.Pin(13, machine.Pin.OUT)

while True:
    led_one.toggle()
    time.sleep(1)

Use whichever pin number you actually wired, 13, 14, or 15, not necessarily 13. Save and run it. If it doesn’t light up, double check that pin number against what you wrote down, and that the LED is wired the way Project 1.1 described.

Every time .toggle() runs, it’s opening and closing a valve on that GPIO pin, exactly like the valve in The Water Analogy: open, and the 3.3V side connects through your LED and resistor to ground; closed, and nothing flows.

Counting in unary

Three LEDs can do more than blink one at a time: together, they can count. The simplest way to show a number with lights is to light that many of them: one LED on means one, two on means two, three on means three. This is called unary, the same idea as tally marks or holding up fingers: the count is the number, nothing more compact about it.

Counting in binary

Unary tops out fast: three LEDs can only ever show 0 through 3, one count per LED. Binary squeezes more out of the same three LEDs by giving each one a place value instead, the same idea as the place values in Secret Messages in Binary if you’ve done that worksheet: 4, 2, and 1. Any number from 0 to 7 is just some combination of those three, each one either on or off.

Number4s place2s place1s place
0offoffoff
1offoffon
2offonoff
3offonon
4onoffoff
5onoffon
6ononoff
7ononon

It doesn’t matter which of your three LEDs you pick for each place value, as long as you keep track of which pin is which. Check your Project 1.1 Notes page for your pin numbers.

Once you’re comfortable blinking and counting, move on to Project 1.5, where your LEDs stop being just on or off.