Skip to content

Session 1: Meet the Pico 2 W and the MicroPython Runtime

Synopsis

Covers the Pico 2 W hardware profile, MicroPython firmware, core I/O capabilities, memory constraints, and how embedded Python differs from desktop Python.

Session Content

Session 1: Meet the Pico 2 W and the MicroPython Runtime

Session duration

~45 minutes

Session goals

By the end of this session, learners will be able to: - Identify the Raspberry Pi Pico 2 W hardware features - Set up a MicroPython development environment using Thonny - Flash MicroPython onto the Pico 2 W - Connect to the Pico REPL and run simple Python code - Understand the MicroPython runtime and how it differs from standard Python


1) Session overview

Topics covered

  • What the Raspberry Pi Pico 2 W is
  • Key hardware features and onboard components
  • What MicroPython is and why it is used on microcontrollers
  • Development workflow with Thonny
  • Flashing MicroPython firmware
  • Using the REPL to test code interactively
  • Basic file structure on the board

Prerequisites

  • Basic Python knowledge
  • Raspberry Pi Pico 2 W
  • USB cable capable of data transfer
  • Thonny IDE installed on a computer
  • Optional: breadboard, jumper wires, external LED, 220Ω resistor

2) Hardware introduction: what is the Pico 2 W?

The Raspberry Pi Pico 2 W is a compact microcontroller board designed for embedded projects and IoT applications. It includes: - A dual-core microcontroller - Onboard Wi-Fi connectivity - GPIO pins for digital input/output - SPI, I2C, UART, PWM, ADC support - USB connectivity for programming and power

Onboard features

  • USB connector: powers the board and provides data connection
  • BOOTSEL button: used to enter USB mass storage mode for firmware flashing
  • LED: onboard status LED, useful for first tests
  • GPIO pins: general-purpose pins for interacting with sensors and actuators

Key difference from a normal computer

A microcontroller: - Runs one program at a time - Has limited RAM and storage - Boots directly into the program stored on the device - Interacts with physical hardware directly


3) What is MicroPython?

MicroPython is a lean implementation of Python designed to run on microcontrollers.

Why use MicroPython on the Pico 2 W?

  • Python-like syntax that is familiar to Python developers
  • Fast prototyping
  • Direct hardware access
  • Interactive REPL for testing
  • Good fit for IoT and hardware control

MicroPython vs CPython

Feature CPython on PC MicroPython on Pico 2 W
Target Desktop/server Microcontroller
Memory Large Small
Libraries Very broad Hardware-focused subset
Execution OS-based Bare-metal/embedded runtime
Hardware access Through OS/drivers Direct via machine module

4) Development environment setup in Thonny

Install Thonny

  1. Download and install Thonny from the official website.
  2. Launch Thonny.

Select the interpreter

In Thonny: 1. Open Tools → Options 2. Go to Interpreter 3. Select MicroPython (Raspberry Pi Pico) 4. Choose the correct port if detected automatically

Verify connection

After selecting the interpreter, Thonny should show a REPL prompt like:

>>>

If the board is connected and MicroPython is installed, Thonny can send code directly to the Pico.


5) Flashing MicroPython onto the Pico 2 W

Steps

  1. Unplug the Pico 2 W from USB.
  2. Hold down the BOOTSEL button.
  3. While holding BOOTSEL, plug the board into USB.
  4. Release BOOTSEL after it appears as a USB drive.
  5. Download the latest MicroPython firmware for Raspberry Pi Pico 2 W.
  6. Copy the .uf2 firmware file to the Pico mass storage drive.
  7. The board will reboot automatically.

After flashing

Reconnect in Thonny: - Choose the MicroPython interpreter - Select the serial port for the Pico 2 W - Open the REPL and test code


6) MicroPython runtime basics

Key concepts

  • REPL: Read-Eval-Print Loop; interactive prompt for testing commands
  • main.py: runs automatically after boot if present
  • boot.py: runs at startup before main.py
  • Modules: code files imported into your scripts
  • machine module: used for hardware access

Typical file flow

  1. Pico powers on
  2. boot.py runs
  3. main.py runs
  4. Your application starts

7) Hands-on exercise 1: First REPL commands

Objective

Use the REPL to verify that the Pico is alive and running MicroPython.

Steps

  1. Open Thonny
  2. Connect to the Pico REPL
  3. Enter the following commands
print("Hello from Pico 2 W!")

Example output

Hello from Pico 2 W!

Try a few more:

import os
print(os.uname())

Example output

(sysname='rp2', nodename='rp2', release='1.x.x', version='MicroPython v1.x.x on ...', machine='Raspberry Pi Pico 2 W with RP2350')

8) Hands-on exercise 2: Control the onboard LED

Objective

Blink the onboard LED using MicroPython.

Code

Create a new file in Thonny and run it on the Pico.

from machine import Pin
from time import sleep

# On the Pico 2 W, the onboard LED is typically connected internally
# and can be controlled using the "LED" pin name.
led = Pin("LED", Pin.OUT)

while True:
    led.value(1)     # Turn LED on
    print("LED ON")
    sleep(1)

    led.value(0)     # Turn LED off
    print("LED OFF")
    sleep(1)

Example output

LED ON
LED OFF
LED ON
LED OFF

What to observe

  • The onboard LED should blink every second
  • The REPL prints status messages

9) Hands-on exercise 3: Save and auto-run a program

Objective

Learn how to make a script run automatically on boot.

Steps

  1. Open a new file in Thonny
  2. Paste the blink code
  3. Save it to the Pico as main.py
  4. Press the reset button or disconnect/reconnect USB

Expected behavior

The Pico should start blinking the LED automatically when powered on.

Example main.py

from machine import Pin
from time import sleep

led = Pin("LED", Pin.OUT)

while True:
    led.toggle()
    print("LED toggled")
    sleep(0.5)

10) Common runtime concepts and limitations

Memory

Microcontrollers have limited RAM, so: - Avoid loading large data into memory - Prefer simple data structures - Be careful with infinite loops and recursion

Timing

Use sleep() carefully: - Long sleeps block execution - For responsive programs, later sessions will introduce non-blocking approaches with uasyncio

File storage

Files are stored on the Pico’s onboard flash. Keep scripts small and organized.


11) Mini demo: checking board information

Code

import os

# Display system information about the MicroPython runtime
info = os.uname()
print("System name:", info.sysname)
print("Node name:", info.nodename)
print("Release:", info.release)
print("Version:", info.version)
print("Machine:", info.machine)

Example output

System name: rp2
Node name: rp2
Release: 1.24.0
Version: MicroPython v1.24.0 on ...
Machine: Raspberry Pi Pico 2 W with RP2350

12) Suggested discussion points

  • Why MicroPython is a good choice for learning embedded programming
  • The role of the REPL in rapid prototyping
  • How the Pico 2 W differs from using Python on a laptop
  • Why resource constraints matter in embedded systems

13) Troubleshooting

Board not detected in Thonny

  • Use a data-capable USB cable
  • Try a different USB port
  • Reboot Thonny
  • Reflash MicroPython using BOOTSEL mode

REPL not responding

  • Check interpreter selection in Thonny
  • Ensure correct serial port is selected
  • Disconnect/reconnect the board

LED code does not work

  • Confirm that the script is being run on the Pico, not locally on the PC
  • Ensure Pin("LED", Pin.OUT) is supported for the board
  • Save the file and run again

14) Knowledge check

Questions

  1. What is the purpose of the BOOTSEL button?
  2. What is the REPL used for?
  3. What is the difference between boot.py and main.py?
  4. Why is MicroPython useful on a microcontroller?
  5. What does Pin("LED", Pin.OUT) do?

15) Wrap-up

Key takeaways

  • The Pico 2 W is a powerful microcontroller for embedded and IoT projects
  • MicroPython provides a familiar Python development experience
  • Thonny is a convenient IDE for flashing, editing, and testing code
  • The REPL is essential for interactive development
  • main.py can be used to run code automatically on boot

Next session preview

In the next session, you will work with GPIO pins and learn how to connect and control external components such as LEDs and buttons using MicroPython.


Back to Chapter | Back to Master Plan | Next Session