MicroPython Environment and Development Tools
Summary
This chapter walks you through everything you need to start writing MicroPython programs on a real microcontroller: downloading and installing the Thonny IDE, flashing the MicroPython firmware onto your Pico, and using the interactive REPL to run code instantly. You will learn how to transfer files between your computer and the Pico using Thonny's file manager, mpremote, and rshell, and how boot.py and main.py control what runs at startup. The chapter also introduces the MicroPython standard library modules — uos, utime, and sys — that your programs will use throughout the course.
Concepts Covered
This chapter covers the following 20 concepts from the learning graph:
- MicroPython
- MicroPython REPL
- MicroPython Firmware
- Flashing Firmware
- Thonny IDE
- VS Code IDE
- Thonny File Manager
- mpremote Tool
- rshell Tool
- File Transfer to Pico
- MicroPython Interpreter
- Interactive Mode
- Script Mode
- Boot.py File
- Main.py File
- MicroPython Modules
- MicroPython Standard Library
- uos Module
- utime Module
- sys Module
Prerequisites
This chapter builds on concepts from:
- Chapter 1: Python Basics — Programs, Variables, Data Types, and Operators
- Chapter 2: Collections, Control Flow, Functions, and Error Handling
Welcome to Chapter 3
It is time to leave the code editor and talk to a real microcontroller! In this chapter you will set up your development tools, put MicroPython on your Pico, and run your first program on real hardware. This is one of the most exciting steps in the whole course. Let's build something amazing!
What Is MicroPython?
MicroPython is a small, efficient version of Python 3 designed to run on microcontrollers. A regular computer has gigabytes of RAM and a fast operating system. A microcontroller like the Raspberry Pi Pico has only 264 KB of RAM and no operating system at all. MicroPython fits Python into that tiny space by leaving out features that microcontrollers do not need (like file sorting and web frameworks) while keeping everything that matters for hardware control.
MicroPython includes a MicroPython interpreter — the program that reads your Python source code and runs it directly on the microcontroller. You interact with it in two ways: interactive mode (one command at a time) and script mode (a complete program stored as a file).
Setting Up Thonny IDE
Thonny is the recommended editor for this course. It was designed specifically for learning Python, and it has built-in support for MicroPython and the Pico. Download it free from thonny.org and install it on your computer.
To configure Thonny for MicroPython:
- Open Thonny.
- Go to Tools → Options → Interpreter.
- Select MicroPython (Raspberry Pi Pico) from the dropdown.
- Click OK.
VS Code with the MicroPython extension is an alternative for students who already know VS Code. It offers more advanced features but has a steeper setup. For beginners, stick with Thonny.
Flashing the MicroPython Firmware
Before your Pico can run MicroPython, you must install the MicroPython firmware — a single .uf2 file that replaces the factory software. Flashing means copying this file onto the Pico.
Follow these steps exactly:
- Hold down the BOOTSEL button on the Pico.
- While holding BOOTSEL, plug the USB cable into your computer.
- Release the BOOTSEL button. The Pico appears as a USB drive called RPI-RP2.
- Download the latest MicroPython firmware from micropython.org/download/RPI_PICO.
- Drag the
.uf2file onto the RPI-RP2 drive. - The Pico restarts automatically. The USB drive disappears — that means it worked!
Monty's Tip
If the RPI-RP2 drive does not appear, try a different USB cable. Some cables are charge-only and carry no data. A data cable is required for flashing firmware.
The MicroPython REPL — Interactive Mode
After flashing, connect the Pico to Thonny. At the bottom of the Thonny window you will see the MicroPython REPL (Read-Evaluate-Print Loop). The REPL shows the >>> prompt, which means it is waiting for your next command.
The REPL works in interactive mode: you type one line of Python, press Enter, and the Pico runs it immediately. This is perfect for quick experiments:
1 2 3 4 5 6 7 | |
Interactive mode is great for testing ideas and exploring what the hardware can do — no need to save a file first.
Diagram: REPL Workflow Diagram
REPL Workflow Interactive Diagram
Type: diagram
sim-id: repl-workflow
Library: p5.js
Status: Specified
Bloom Level: Understand (L2) Bloom Verb: explain Learning Objective: Students can describe the Read-Evaluate-Print Loop cycle and distinguish interactive mode from script mode.
Canvas layout: - Center: four labeled boxes arranged in a cycle: "Read" → "Evaluate" → "Print" → "Loop back to Read" - Below the cycle: two side-by-side columns: "Interactive Mode" vs "Script Mode" - Each box is clickable
Visual elements: - Four rounded rectangles in a circular arrangement with arrows between them - "Read": "You type a Python line and press Enter" - "Evaluate": "The MicroPython interpreter runs the code" - "Print": "The result appears on the >>> prompt" - "Loop": "The prompt reappears, waiting for the next command" - Color: active step highlighted in teal; others gray
Interactive controls:
- Clicking any box reveals a pop-up with a concrete example (e.g., clicking "Read" shows >>> 2 + 2)
- A "Next Step" button cycles through the four stages with highlighting
- Two toggle buttons: "Interactive Mode" and "Script Mode" — each shows a brief description in a side panel
Instructional Rationale: The four-step cycle with click-to-reveal makes the REPL loop concrete. Toggling between modes clarifies the difference between one-shot commands and stored programs.
Implementation: p5.js with four clickable rounded rects; state machine cycles through steps; createButton() for Next Step and mode toggles.
Script Mode — Writing Complete Programs
In script mode, you write a complete program in Thonny's editor, save it, and run it all at once. Use File → New to open an editor tab, write your program, then click the green Run button (or press F5).
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Script mode is how you write every real project in this course.
Transferring Files to the Pico
When you run a script in Thonny, it runs once and stops. To make a program run every time the Pico powers on, you need to transfer it to the Pico's internal flash memory. There are three ways to do this.
Thonny File Manager
In Thonny, go to View → Files to open the file manager. It shows two panels: your computer on the left, and the Pico on the right. Drag files from left to right to copy them onto the Pico.
mpremote
mpremote is a command-line tool. You install it with pip install mpremote, then use it from a terminal:
1 2 3 | |
rshell
rshell is another command-line tool with a shell-like interface:
1 2 | |
| Tool | How to use | Best for |
|---|---|---|
| Thonny File Manager | Drag-and-drop in GUI | Beginners |
| mpremote | Short commands in terminal | Everyday file management |
| rshell | Shell-like commands | Scripted deployments |
boot.py and main.py — Startup Files
When the Pico powers on, MicroPython automatically looks for two special files:
- boot.py — runs first, every time. Use it for hardware setup, Wi-Fi connection, and configuration that must happen before your program starts.
- main.py — runs second, every time. This is where your main program lives.
1 2 3 4 5 6 7 8 9 | |
Key Idea: main.py Runs Forever
Unlike programs on your laptop that finish and exit, a Pico program usually runs in an infinite while True: loop. The Pico has one job — keep blinking, keep reading sensors, keep controlling motors — and it does it until you unplug it or reset it.
The MicroPython Standard Library
MicroPython includes a set of built-in modules — the MicroPython standard library. These modules give you ready-made functions for hardware control, time, the file system, and system information. You will use three of them in almost every project.
utime — Timing Functions
The utime module provides time and delay functions:
1 2 3 4 5 6 | |
uos — Operating System / File System
The uos module lets you work with the Pico's file system:
1 2 3 4 5 6 | |
sys — System Information
The sys module gives you information about the MicroPython environment:
1 2 3 4 5 6 | |
The following table summarizes the three standard library modules:
| Module | Purpose | Key functions |
|---|---|---|
utime |
Time and delays | sleep(), sleep_ms(), ticks_ms() |
uos |
File system | listdir(), mkdir(), remove() |
sys |
System info | version, platform, exit() |
You Can Do This!
Setting up hardware tools for the first time is often the trickiest part of any course. If your Pico does not show up, if Thonny gives a connection error, or if the firmware flash did not work — that is normal. Take it one step at a time, and check the troubleshooting tips in the debugging chapter. Every maker hits these bumps at the start.
Key Takeaways
- MicroPython is Python 3 designed to run on microcontrollers with very little RAM.
- Flash the firmware once by holding BOOTSEL while connecting USB.
- The REPL runs one command at a time (interactive mode).
- Script mode runs a complete saved program.
- Copy programs to the Pico using Thonny's file manager, mpremote, or rshell.
- boot.py runs first; main.py runs second — both run automatically on power-up.
utime,uos, andsysare the three standard library modules you will use most.
Your First Real Hardware Step!
You have set up your tools, flashed your Pico, and run code on real hardware. That is a huge milestone — welcome to the world of physical computing! In Chapter 4 you will learn all about the microcontroller hardware itself: pins, power, memory, and the amazing features of the RP2040 chip inside your Pico. You've got this, maker!