Session 3: Writing and Running Basic MicroPython Programs
Synopsis
Builds confidence with scripts, modules, the boot process, main.py and boot.py, and simple examples using GPIO and timing.
Session Content
Session 3: Writing and Running Basic MicroPython Programs
Session Overview
This session introduces the fundamentals of writing, uploading, and running MicroPython programs on the Raspberry Pi Pico 2 W using Thonny. You will learn the MicroPython file structure, how to create and run scripts, how to use the REPL, and how to make the Pico respond using LEDs and basic input/output.
Duration: ~45 minutes
Audience: Python developers with basic programming knowledge
Focus: Getting started with MicroPython programming workflow on Pico 2 W
Learning Objectives
By the end of this session, learners will be able to:
- Set up Thonny for Raspberry Pi Pico 2 W development
- Connect to the Pico 2 W REPL
- Write and run basic MicroPython programs
- Use
main.pyandboot.pyappropriately - Work with digital output using the onboard LED
- Read digital input from a button
- Understand basic program flow in MicroPython on embedded devices
Prerequisites
- Raspberry Pi Pico 2 W
- USB cable for data transfer
- Thonny IDE installed
- MicroPython firmware installed on the Pico 2 W
- Breadboard
- Jumper wires
- 1 push button
- 1 LED
- 1 resistor (220Ω to 330Ω)
Development Environment Setup
1. Install and Open Thonny
- Download and install Thonny from the official website.
- Open Thonny.
2. Select the Pico Interpreter
In Thonny: - Go to Tools → Options → Interpreter - Choose MicroPython (Raspberry Pi Pico) - Select the correct port for the Pico 2 W - Confirm the connection
3. Verify REPL Access
Open the Shell pane in Thonny and confirm you can see the MicroPython prompt:
>>>
Try typing:
print("Hello, Pico!")
Expected output:
Hello, Pico!
Session Flow
1. Theory: How MicroPython Programs Run on Pico 2 W
- MicroPython is a lightweight Python implementation designed for microcontrollers.
- On the Pico 2 W, programs can run:
- interactively in the REPL
- as saved files on the device
- Important files:
boot.py: runs first at startupmain.py: runs afterboot.pyat startup- Code can be uploaded directly from Thonny to the Pico filesystem
2. Theory: Basic Program Structure
A basic MicroPython program typically includes: - imports - setup code - a main loop - cleanup or error handling when necessary
Example structure:
from machine import Pin
from time import sleep
led = Pin("LED", Pin.OUT)
while True:
led.toggle()
sleep(0.5)
Hands-On Exercise 1: Hello, Pico in the REPL
Goal
Use the REPL to run simple Python expressions and print output.
Steps
- Connect the Pico 2 W in Thonny.
- In the Shell, enter:
print("Hello from Pico 2 W")
- Try a few basic expressions:
2 + 3
name = "MicroPython"
print("Learning", name)
Expected Output
Hello from Pico 2 W
5
Learning MicroPython
Hands-On Exercise 2: Blink the Onboard LED
Goal
Write a basic MicroPython script and run it on the Pico 2 W.
Code
Create a new file in Thonny and enter:
# Blink the onboard LED on the Raspberry Pi Pico 2 W
# This program toggles the built-in LED every 0.5 seconds.
from machine import Pin
from time import sleep
# The onboard LED is available as "LED"
led = Pin("LED", Pin.OUT)
print("Starting LED blink program...")
while True:
led.value(1) # Turn LED on
print("LED ON")
sleep(0.5)
led.value(0) # Turn LED off
print("LED OFF")
sleep(0.5)
Run Instructions
- Click the Run button in Thonny
- Save the file to the Pico when prompted
- Name the file
blink.py
Expected Behavior
- The onboard LED blinks on and off every 0.5 seconds
- The Shell prints:
Starting LED blink program...
LED ON
LED OFF
LED ON
LED OFF
Hands-On Exercise 3: Save a Program as main.py
Goal
Make a program run automatically when the Pico starts.
Code
Save the following as main.py on the Pico:
# main.py
# This program runs automatically when the Pico 2 W starts.
from machine import Pin
from time import sleep
led = Pin("LED", Pin.OUT)
print("main.py is running")
while True:
led.toggle()
print("Toggled LED")
sleep(1)
Steps
- Save the file to the Pico as
main.py - Press the Pico reset button or disconnect/reconnect USB
- Observe that the program starts automatically
Expected Output
main.py is running
Toggled LED
Toggled LED
Toggled LED
Theory: boot.py vs main.py
boot.py
- Runs first at startup
- Used for low-level initialization
- Common uses:
- configure pins
- set up network access
- redirect logging
main.py
- Runs after
boot.py - Used for application code
- Common uses:
- sensor reading
- display updates
- network communication
- device control logic
Hands-On Exercise 4: Read a Button and Control an LED
Goal
Use a push button as input and an LED as output.
Wiring
Connect: - One side of the button to GP15 - The other side of the button to GND - LED anode to GP16 through a 220Ω resistor - LED cathode to GND
Code
# Button-controlled LED
# Press the button to turn the LED on.
from machine import Pin
from time import sleep
# Configure button with internal pull-up resistor
button = Pin(15, Pin.IN, Pin.PULL_UP)
# Configure external LED on GP16
led = Pin(16, Pin.OUT)
print("Press the button to turn the LED on")
while True:
# Button reads 0 when pressed because of pull-up wiring
if button.value() == 0:
led.value(1)
print("Button pressed -> LED ON")
else:
led.value(0)
print("Button released -> LED OFF")
sleep(0.1)
Expected Behavior
- LED turns on when button is pressed
- LED turns off when button is released
- Thonny Shell prints button state changes
Guided Discussion: Program Control Flow
Discuss:
- Why while True is common in embedded programming
- Why delays are needed to avoid excessive CPU usage
- Why microcontroller code often runs forever
- How input changes can affect output in real time
Common Mistakes and Troubleshooting
- No REPL connection: check USB cable and interpreter selection
- Program does not run: ensure file is saved to the Pico, not only locally
- LED does not blink: confirm you used
Pin("LED", Pin.OUT) - Button reading is always wrong: check pull-up configuration and wiring
- Program seems frozen: an infinite loop may be running; stop it in Thonny with the stop button
Mini Challenge
Modify the blink program so that: - the LED blinks faster when the button is pressed - the LED blinks slower when the button is not pressed
Starter Code
from machine import Pin
from time import sleep
button = Pin(15, Pin.IN, Pin.PULL_UP)
led = Pin("LED", Pin.OUT)
while True:
if button.value() == 0:
led.toggle()
print("Fast blink")
sleep(0.2)
else:
led.toggle()
print("Slow blink")
sleep(1)
Review Questions
- What is the purpose of the REPL?
- What is the difference between
boot.pyandmain.py? - How do you make a Pico program start automatically?
- Why do many embedded programs use
while True? - How does pull-up input wiring work with a button?
Summary
In this session, you learned how to:
- connect Thonny to the Pico 2 W
- run commands in the REPL
- create and save MicroPython scripts
- use main.py for automatic startup
- control LEDs and read buttons with basic MicroPython code
Next Session Preview
The next session will introduce:
- variables, data types, and operators in MicroPython
- basic decision-making with if statements
- program logic for hardware interaction
Back to Chapter | Back to Master Plan | Previous Session | Next Session