Session 2: Setting Up the Development Workflow
Synopsis
Introduces firmware installation, serial access, file transfer, REPL usage, and common development tools for editing, running, and testing MicroPython code on the Pico 2 W.
Session Content
Session 2: Setting Up the Development Workflow
Session Duration
- 45 minutes
Session Overview
In this session, learners set up a practical MicroPython development workflow for the Raspberry Pi Pico 2 W. The focus is on preparing the board, connecting Thonny, uploading code, using the REPL, saving scripts to the device, and validating the setup with simple hardware and software tests.
Learning Objectives
By the end of this session, learners will be able to:
- Prepare a Raspberry Pi Pico 2 W for MicroPython development
- Install and configure Thonny for Pico development
- Connect to the Pico 2 W REPL using USB
- Upload and run MicroPython scripts on the board
- Save a startup script as
main.py - Use basic debugging techniques in Thonny
- Verify the workflow with a simple LED blink program
Prerequisites
- A Raspberry Pi Pico 2 W
- USB cable with data support
- A computer with internet access
- Thonny IDE installed
- Micro-USB or USB-C adapter as required by the host computer
- Optional: external LED, resistor, breadboard, and jumper wires
Session Agenda
1. Introduction to the Development Workflow (5 minutes)
- What MicroPython is
- Why Pico development is different from standard desktop Python
- Overview of the workflow:
- Write code in Thonny
- Send code to Pico
- Run code on device
- View output in REPL
- Save persistent scripts to
main.py
2. Installing and Configuring Thonny (10 minutes)
- Install Thonny
- Select the correct MicroPython interpreter
- Connect to the Raspberry Pi Pico 2 W
- Confirm the board is visible in the IDE
3. First Connection and REPL Usage (10 minutes)
- Open the REPL
- Run a simple command
- Read board information
- Use
Ctrl+Cto interrupt running code - Reset the device if needed
4. Save and Run a Script on the Pico (10 minutes)
- Create a Python file in Thonny
- Upload it to the board
- Run it manually
- Save it as
main.pyso it runs on boot
5. Hands-On Exercise: LED Blink and Debugging (10 minutes)
- Blink the onboard LED
- Introduce simple error handling and output printing
- Verify code behavior through the REPL
Development Environment Setup
Install Thonny
- Download and install Thonny from the official website.
- Launch Thonny.
- Go to Tools > Options > Interpreter.
- Select:
- MicroPython (Raspberry Pi Pico)
- Connect the Pico 2 W via USB.
- If prompted, choose the correct port.
Confirm MicroPython Firmware
If the board is new or not running MicroPython:
1. Hold the BOOTSEL button while connecting the Pico to USB.
2. The board appears as a USB mass storage device.
3. Copy the MicroPython .uf2 firmware to the board.
4. The Pico reboots after flashing.
Verify Connection
In Thonny, the shell should show a prompt like:
MicroPython v1.x.x on 202x-xx-xx; Raspberry Pi Pico W with RP2040
Type "help()" for more information.
>>>
Theory: Understanding the Workflow
MicroPython on Pico 2 W
MicroPython is a lightweight Python implementation designed for microcontrollers. On the Pico 2 W, it allows you to: - Control GPIO pins - Read sensors - Drive actuators - Connect to Wi-Fi - Build small IoT devices
Thonny as a Development Tool
Thonny is beginner-friendly and supports: - Code editing - Running code on the device - REPL interaction - File transfer to/from the Pico - Debugging with simple variable inspection
REPL Basics
REPL stands for Read-Eval-Print Loop. It lets you: - Run commands one line at a time - Inspect variables - Test small code snippets quickly
Example:
print("Hello from Pico 2 W!")
Expected output:
Hello from Pico 2 W!
Hands-On Exercise 1: Verify the REPL
Goal
Confirm the Pico is connected and responding to MicroPython commands.
Steps
- Open Thonny.
- Connect to the Pico 2 W interpreter.
- In the Shell, type:
import machine
print(machine.freq())
Expected Output
150000000
This value indicates the CPU clock speed in Hz.
Additional Test
Try:
import sys
print(sys.platform)
Expected output:
rp2
Hands-On Exercise 2: Blink the Onboard LED
Goal
Create a simple script that blinks the onboard LED on the Pico 2 W.
Notes
The onboard LED can usually be accessed using machine.Pin("LED") on Pico boards that support it.
Code
from machine import Pin
from time import sleep
# Create an output pin for the onboard LED
led = Pin("LED", Pin.OUT)
# Blink the LED forever
while True:
led.toggle()
print("LED toggled")
sleep(0.5)
What This Teaches
- Importing modules
- Configuring GPIO
- Using an infinite loop
- Adding diagnostic output
- Timing with
sleep()
Expected Output
LED toggled
LED toggled
LED toggled
...
Hands-On Exercise 3: Save a Startup Script
Goal
Make the Pico run a script automatically after reboot.
Steps
- Open a new file in Thonny.
- Paste the blink script.
- Save it to the Pico as
main.py. - Press the reset button or disconnect/reconnect USB.
Result
The script starts automatically when the Pico powers on or resets.
Example main.py
from machine import Pin
from time import sleep
# Set up the onboard LED
led = Pin("LED", Pin.OUT)
# Main loop
while True:
led.on()
print("LED ON")
sleep(1)
led.off()
print("LED OFF")
sleep(1)
Expected Output
LED ON
LED OFF
LED ON
LED OFF
...
Debugging Techniques
1. Use Print Statements
Add print() calls to show program flow.
Example:
print("Starting program...")
2. Interrupt a Running Program
- Press Ctrl+C in the Thonny Shell to stop a loop.
3. Reset the Board
- Use the reset button if available
- Or disconnect and reconnect USB
4. Check Common Errors
- Wrong interpreter selected
- USB cable without data wires
- Script saved to the computer instead of the Pico
- Syntax errors from missing colons or indentation
Mini Challenge
Modify the blink script so that:
- The LED turns on for 200 ms
- Turns off for 800 ms
- Prints "blink cycle complete" after each cycle
Starter Code
from machine import Pin
from time import sleep
led = Pin("LED", Pin.OUT)
while True:
led.on()
sleep(0.2)
led.off()
sleep(0.8)
print("blink cycle complete")
Expected Output
blink cycle complete
blink cycle complete
blink cycle complete
...
Knowledge Check
- What is the REPL used for?
- How do you connect Thonny to the Pico 2 W?
- What file name should be used for code that runs at boot?
- How can you stop a running infinite loop?
- Why is
print()useful during development?
Session Summary
In this session, learners:
- Installed and configured Thonny
- Connected to the Pico 2 W MicroPython REPL
- Uploaded and ran scripts on the board
- Saved code as main.py
- Tested GPIO control with the onboard LED
- Practiced basic debugging and workflow validation
Recommended Practice After the Session
- Repeat the blink example from memory
- Change the blink interval
- Add extra
print()statements to trace execution - Experiment with
Pin("LED").on()and.off()in the REPL - Save a custom startup script and reboot the Pico
Reference Code Snippets
Quick REPL Test
print("Pico 2 W is connected")
LED Control in REPL
from machine import Pin
led = Pin("LED", Pin.OUT)
led.on()
led.off()
Show Platform Information
import sys
print(sys.platform)
Suggested Equipment for the Next Session
- Raspberry Pi Pico 2 W
- Breadboard
- 1 LED
- 1 220Ω resistor
- Jumper wires
- USB cable
- Thonny IDE
Back to Chapter | Back to Master Plan | Previous Session | Next Session