Electronics School · Stage 4: Explore Digital Electronics · Lesson 24

Introduction to Programming

Write your first program and make an LED blink: the “Hello, world” of electronics.

The question

How do you tell a microcontroller what to do?

What you’ll discover: how to write, run and change a simple MicroPython program, using outputs, delays, loops and decisions.

In simple terms

BeginnerInstructions, in order

A program is a list of instructions the microcontroller follows, one after another, exactly as written. Computers don’t guess what you meant, so every detail matters.

Most programs use just a few ideas:

  • Outputs: switch a pin on or off.
  • Delays: wait for a set time.
  • Loops: repeat instructions.
  • Decisions: do something only if a condition is true, like a button being pressed.

Getting set up

  1. With an adult, install the free Thonny editor on a computer from thonny.org.
  2. Put MicroPython on your Pico (a one-time step). The easiest way: hold the white BOOTSEL button on the Pico while you plug in the USB cable, then in Thonny choose Tools, Options, Interpreter, and Install or update MicroPython, picking your exact board (Pico, Pico W, Pico 2 or Pico 2 W). Raspberry Pi’s getting-started guide shows another way, by dragging a file onto the Pico when it appears as a USB drive.
  3. In Thonny, choose the MicroPython (Raspberry Pi Pico) interpreter. Now you can type and run programs.
About power: the Pico runs from a USB cable plugged into a computer, a USB battery pack, or a store-bought USB phone charger (a finished, certified product that does the mains conversion safely inside its case). USB supplies a safe 5 V, and the Pico’s pins use just 3.3 V, so this fits our low-voltage rule. Never connect a Pico pin to more than 3.3 V, never connect the 3V3 or VBUS pins straight to GND, and always put a resistor in series with an LED on a pin.

Your first program: blink

Every Pico has a small LED on the board itself. This program blinks it. (On the original Pico it’s connected to GP25; on the Pico W it’s wired through the wireless chip. The name “LED” works on the Pico, Pico W, Pico 2 and Pico 2 W with current MicroPython.)

from machine import Pin
import time

led = Pin("LED", Pin.OUT)   # the board's built-in LED

while True:                 # repeat forever
    led.on()
    time.sleep(0.5)         # wait half a second
    led.off()
    time.sleep(0.5)

Click Run. The LED blinks once a second. Click Stop to end it.

Predict, then change it: what happens if you change both 0.5 values to 0.1? What if the on time is 0.1 and the off time is 1?

Go deeper

IntermediateAdding a button and a decision

Connect a pushbutton between GP16 (physical pin 21, on the right side) and a GND pin, using diagonally opposite legs of the button. Pin 23, two pins further up the right side, is a GND pin. For example, place the button across the centre gap below the Pico, run a jumper from GP16’s row to one button leg, and a jumper from the diagonally opposite leg to pin 23’s row. This program lights the LED only while the button is pressed:

from machine import Pin
import time

led = Pin("LED", Pin.OUT)
button = Pin(16, Pin.IN, Pin.PULL_UP)   # reads 1 when released, 0 when pressed

while True:
    if button.value() == 0:   # pressed
        led.on()
    else:
        led.off()
    time.sleep(0.01)

Notice the indentation: in Python, the spaces at the start of a line show which instructions belong inside the loop or the if.

AdvancedGood habits

Name things clearly (red_led, not x), add comments explaining why, and change one thing at a time when debugging, just like troubleshooting a circuit (Lesson 17). When something goes wrong, Thonny shows an error message with a line number: read it carefully, because it usually points straight at the problem.

Programs that need to do several things at once, like blinking an LED while watching a button, can’t use long sleep calls. Instead they check the time on each pass through the loop (with time.ticks_ms() and time.ticks_diff()) and act when enough time has passed. This is called non-blocking code.

Quick check

What does a loop do?

It repeats instructions, such as blinking an LED over and over.

In the blink program, what controls how fast the LED blinks?

The numbers in time.sleep(): the on and off times in seconds.

With PULL_UP turned on, what does a button pin read when pressed?

0, because pressing it connects the pin to GND.

Words to know

Program
A list of instructions a computer follows in order.
Loop
Instructions that repeat.
Condition
A test that is true or false, used to make decisions.
Bug
A mistake in a program.

Learn more

Electronics School is part of the Discovery Zone. Every activity here runs on small batteries or a USB cable: never wall outlets. The one plug-in tool is the optional soldering iron, set up by an adult.

Was this page helpful?
Get all four of our sites in one free email every Friday: Prairie Signals
⇧Top
Hello! “Finance is the art of passing currency from hand to hand until it finally disappears.”