← All lessons
First Code

Hello,Pico

RP2350

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.

  • Grades3-8 (3-5 with a grown-up)
  • Time30-45 min
  • LevelBeginner
  • Standards6 aligned
Heads up

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

  1. 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.

    The blink loop: toggle the LED, wait half a second, then go back to the top, foreverwhile True: repeat foreverFlip the LEDled.toggle()Waittime.sleep(0.5)back to the top
    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.
  2. 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.

  3. Wire up your own LED

    Unplug the USB cable first. Always change wiring with the power off.

    1. Push your Pico into the breadboard so it straddles the middle gap.
    2. 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.
    3. Connect pin 20 to one end of the 220 Ω resistor.
    4. Connect the other end of the resistor to the LED’s long leg.
    5. Connect the LED’s short leg back to pin 18 (GND).
    Wiring: pin 20 (GP15) to a 220 ohm resistor, to the LED’s long leg; the LED’s short leg back to pin 18 (GND)Pico 2 W (left edge)pin 18 · GNDpin 19 · GP14pin 20 · GP15220 Ω resistorlong leg (+)to the resistorshort leg (−)to 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.
    An LED up close: the long leg is positive, the short leg is negative, and the rim is flat on the negative sideLong leg = + (anode)goes toward the pin and resistorShort leg = − (cathode)goes to GNDFlat edgeon the − side of the rim
    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.
  4. Blink your LED

    Plug the USB cable back in, connect on the coding page, and run this. The only change is the pin: 15 instead 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.

  5. 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.

How it works

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 story behind it

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.2 seconds, 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) and toggle()
  • A circuit needs a complete path, and an LED needs a resistor and the right orientation
  • main.py runs on its own when the board powers up

Official docs

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?
Open in the coding page with this code →

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?

Ask a question or share a tip

Build it with us. Keep it. Explore more on your own.

Find your next build