Skip to content

Getting Started with MicroPython

1. Setting Up the Development Environment

To begin programming the Raspberry Pi Pico with MicroPython, you need to set up your development environment.

1.1 Install Required Software

Option 1: VS Code + MicroPython Plugin

  1. Install Visual Studio Code from code.visualstudio.com.
  2. Install the MicroPico and Raspberry Pi Pico extension for MicroPython. The extensions can be fund on the left sidebar. Open the extensions by clicking on it and in the search bar type pico. Installing MicroPico extension may automatically can install the Raspberry Pi Pico extension too.
  3. Connect your Pico and open a new terminal in VS Code.

Key Features of VS Code:

  1. Support for Multiple Programming Languages

    • VSCode supports the most popular programming languages (e.g., Python, C/C++, Java, JavaScript, etc.)
    • Additional languages can be enabled via extensions
    • Intelligent Code Completion (IntelliSense)

    • Provides real-time suggestions while coding

    • Speeds up development and reduces errors
    • Integrated Terminal

    • Built-in terminal eliminates the need to switch between the editor and command line

    • Can be opened with the shortcut Ctrl + `
    • Version Control (Git Integration)

    • Built-in Git support allows tracking changes and facilitates teamwork

    • Extensibility (Extensions)

    • Thousands of extensions are available via the Extensions Marketplace

    • Includes language support, formatting tools, and debugging utilities

Thonny is a simple and beginner-friendly IDE with built-in MicroPython support.

  1. Download and install Thonny from thonny.org.
  2. Open Thonny and go to Tools → Options.
  3. Select the Interpreter tab and choose MicroPython (Raspberry Pi Pico).
  4. Connect your Pico via USB and click Stop/Restart Backend to verify the connection.

This course uses mpremote — the official MicroPython remote tool:

  1. Install Python 3 (if not installed already).
  2. Create a virtual environment and install mpremote:
    python -m venv ~/pico-env
    source ~/pico-env/bin/activate    # Linux/Mac
    # ~/pico-env\Scripts\activate     # Windows
    pip install mpremote
    
  3. Connect your Pico and verify:
    mpremote connect list
    
  4. Open the REPL:
    mpremote repl
    

Remember to activate the environment (source ~/pico-env/bin/activate) each time you open a new terminal.

See the mpremote cheatsheet for the full command reference.

2. Installing MicroPython on Raspberry Pi Pico

To use MicroPython, you need to flash the MicroPython firmware onto your Pico.

Steps:

  1. Download the latest MicroPython firmware for Pico 2 from the official website.
  2. Hold down the BOOTSEL button on the Pico while plugging it into your computer.
  3. The Pico will appear as a USB mass storage device.
  4. Copy the MicroPython .uf2 file to the Pico.
  5. The Pico will reboot automatically into MicroPython mode.

3. Running Your First MicroPython Script

3.1 Using Thonny

  1. Open Thonny and select MicroPython (Raspberry Pi Pico) as the interpreter.
  2. Type the following code in the editor:
    print("Hello, Raspberry Pi Pico!")
    
  3. Click Run.
  4. You should see Hello, Raspberry Pi Pico! in the output window.

3.2 Using REPL (Read-Eval-Print Loop)

The REPL allows you to interact with MicroPython directly. 1. Open Thonny or a terminal. 2. Connect to the Pico and enter the REPL prompt:

python -m serial.tools.miniterm /dev/ttyUSB0 115200
3. Type:
print("Hello, MicroPython!")
4. Press Enter to see the output.

4. Blinking an LED (First Hardware Interaction)

Let's blink the built-in LED on the Pico to test MicroPython.

MicroPython Code:

from machine import Pin
from time import sleep

led = Pin("LED", Pin.OUT)  # Onboard LED on Pico/Pico 2

while True:
    led.toggle()
    sleep(0.5)

Steps:

  1. Copy and paste this code into Thonny or a VS Code file.
  2. Click Run.
  3. The onboard LED should blink every 0.5 seconds.

Now that you have set up MicroPython and tested your first script, explore.


➡ Next Steps