Hello,Pico
What’s the smallest thing a computer can do in the real world? Turn a light on and off. Every robot, smart lamp and traffic light starts here. In this lesson your board blinks its own LED, then one you wire up yourself, using a few lines of Python.
Do Set Up Your Board first. Your board needs MicroPython installed and should connect to the coding page before you start.
You will need
| Item |
|---|
| A Raspberry Pi Pico, Pico W, or any MicroPython board |
| A USB data cable |
| A computer with Chrome or Edge |
Steps
-
Blink the built-in LED
Your Pico has a tiny green LED right on the board. Open the coding page, connect your board, and run this:
from machine import Pin import time led = Pin("LED", Pin.OUT) while True: led.toggle() time.sleep(0.5)The little light on the board starts blinking. Press Stop when you’re ready to move on.
Fig. 1 A loop is a set of steps a computer repeats. while True:means “do this forever,” so the board flips the LED, waits half a second, and starts again, thousands of times without getting bored. -
Change the speed
The number in
time.sleep(0.5)is how many seconds the board waits. Change it, run again, and watch:time.sleep(1)is slow and steady.time.sleep(0.1)is a fast flicker.time.sleep(0.01): can you still see it blink?
You just changed how a machine behaves by changing one number. That’s programming.
-
Wire up your own LED
Unplug the USB cable first. Always change wiring with the power off.
- Push your Pico into the breadboard so it straddles the middle gap.
- Find pin 20 (GP15) and pin 18 (GND) on the left edge (Fig. 2). Pin 1 is next to the USB port, and the numbers count down the edge.
- Connect pin 20 to one end of the 220 Ω resistor.
- Connect the other end of the resistor to the LED’s long leg.
- Connect the LED’s short leg back to pin 18 (GND).
Fig. 2 Electricity needs a complete loop. It leaves the board at pin 20, passes through the resistor and the LED, and comes home at GND. Break the loop anywhere and the light stays off. Fig. 3 LEDs only work one way around. If yours doesn’t light up later, flip it: that fixes it more often than anything else. -
Blink your LED
Plug the USB cable back in, connect on the coding page, and run this. The only change is the pin:
15instead of"LED".from machine import Pin import time led = Pin(15, Pin.OUT) while True: led.toggle() time.sleep(0.5)It’s blinking! You wrote code that controls a real part you wired yourself.
-
Make it run on its own
Right now the program only runs while the coding page is connected. On the coding page, click Save to Board. This saves your code to the board as
main.py, the file MicroPython runs every time it powers up.Unplug the board, then plug it into any USB charger or power bank. It starts blinking with no computer at all.
What just happened?
Each pin on your board is like a tiny switch that code can flip. Pin(15, Pin.OUT) makes pin 20 (GP15) an output. When it’s on, the pin puts out 3.3 volts and current flows through your loop. When it’s off, the pin drops to 0 volts and the current stops. toggle() flips it from whichever state it’s in to the other.
Why the resistor?
An LED lets current through very easily. Without anything to slow it down, it would draw too much and burn out, and it could damage the pin too. The resistor limits the current to a safe trickle.
A red LED uses about 2 volts of the pin’s 3.3, which leaves about 1.3 volts across the resistor. Ohm’s law gives the current: 1.3 V ÷ 220 Ω ≈ 0.006 amps, or 6 milliamps. That’s bright enough to see and gentle enough for the LED and the pin.
The first light you could see
Before 1962, LEDs only glowed in infrared, which human eyes can’t see. That year Nick Holonyak Jr., an engineer at General Electric, made the first LED that glowed visible red. Today LEDs are in almost every screen, flashlight and traffic light on Earth.
Try this next
- SOS: blink three short, three long, three short. (Short =
0.2seconds, long =0.6.) - Flicker test: keep making the wait shorter. At some point your eyes stop seeing the blinks and the LED looks steady. What number is that for you?
- Two LEDs: wire a second LED and resistor to pin 19 (GP14), sharing the same GND. Can you make them take turns?
Stuck? Check these
- LED is backwards. Turn it around so the long leg goes toward the resistor (Fig. 3).
- Wrong pin. Pin 20 on the board is called GP15 in code. Count from pin 1 next to the USB port.
- Built-in LED won’t blink on a Pico W or Pico 2 W. Use
Pin("LED"), not a number. On those boards the LED is wired through the wireless chip. - Loose breadboard connection. Push each leg and wire all the way in.
- Board not connecting. See the checklist in Set Up Your Board. Is Thonny still open?
What you learned
- A loop repeats steps, and
while True:repeats them forever - Pins are switches your code can flip with
Pin(..., Pin.OUT)andtoggle() - A circuit needs a complete path, and an LED needs a resistor and the right orientation
main.pyruns on its own when the board powers up
Official docs
- MicroPython: machine.PinEverything a pin can do, from the people who make MicroPython.
- MicroPython: Quick reference for the RP2Short examples for the chip inside the Pico.
- MicroPython: timeHow
sleep()and other timing tools work. - Raspberry Pi: Pico-series documentationOfficial pinout diagrams for every Pico, including the Pico 2 W.
- Raspberry Pi: official blink exampleThe same idea written another way, using a timer instead of a loop.
Run the code
from machine import Pin
import time
led = Pin(15, Pin.OUT)
while True:
led.toggle()
time.sleep(0.5)
# Try it: change the sleep numbers and run again.
# What happens with 0.1? What about 2? Standards
This lesson supports these standards from the Massachusetts Digital Literacy and Computer Science Framework and related frameworks.
- MA DLCS 3-5.CT.b.1Define an algorithm as a sequence of instructions that can be processed by a computer.
- MA DLCS 3-5.CT.d.2Use arithmetic operators, conditionals, and repetition in programs.
- MA DLCS 3-5.CT.d.3Use interactive debugging to detect and correct simple program errors.
- MA DLCS 3-5.CS.a.2Describe the function and purpose of various input and output devices.
- MA DLCS 6-8.CT.d.3Create a program, individually and collaboratively, that implements an algorithm to achieve a given goal.
- MA DLCS 6-8.CT.d.5Trace programs step-by-step in order to predict their behavior.
Want a hand?
Build it with us. Keep it. Explore more on your own.
Find your next build