ENG | Raspberry Pi Pico and MicroPython, Getting started
Explore the basics of Raspberry Pi Pico and MicroPython. Learn how to set up your board, install MicroPython, and start programming with a simple LED example.
Introduction
Spring is here and with it comes Pi day! Best time to try a new microcontroller: Raspberry Pi Pico 🙂
I want to evaluate this against Arduino in the future and possibly make one E-ink project if I have time and willpower to do so.
What is Raspberry Pi Pico
The Raspberry Pi Pico is a low-cost, highly capable microcontroller board designed by the Raspberry Pi Foundation. Despite its small size and affordable price, the Pico packs a powerful RP2040 dual-core Arm Cortex-M0+ processor, making it an ideal choice for a wide range of projects, from simple electronics to complex embedded systems.
One of the standout features of the Raspberry Pi Pico is its support for MicroPython, a lean and efficient implementation of the Python 3 programming language optimized for microcontrollers. MicroPython allows developers to write code using the familiar and user-friendly Python syntax, while taking advantage of the Pico’s hardware capabilities.
In this article, we’ll guide you through the process of setting up your Raspberry Pi Pico with MicroPython, enabling you to begin exploring the world of microcontroller programming and unleashing the full potential of this powerful little board.
What is not Raspberry Pi Pico
It’s not a single board computer (SBC) similar to Raspberry Pi or Raspberry Pi Zero, but a completely different product. It cannot run operation system. It’s microcontroller with completely different goal, more similar to popular Arduino, STM32, ESP32 and alike.
What is MicroPython
MicroPython is a compact and efficient implementation of the Python 3 programming language designed specifically for microcontrollers and embedded systems. It brings the simplicity and readability of Python to the world of low-level hardware programming, allowing developers to write code that interacts directly with sensors, actuators, and other peripherals.
While MicroPython shares most of the syntax and features of standard Python, it has been optimized for resource-constrained environments, making it an ideal choice for microcontrollers like the Raspberry Pi Pico. With MicroPython, you can leverage the power of Python to quickly prototype and develop applications for a wide range of projects, from Internet of Things (IoT) devices to robotics and automation systems.
By combining the ease of use and productivity of Python with the low-level control and performance of microcontrollers, MicroPython opens up new possibilities for developers of all skill levels, enabling them to bring their ideas to life with minimal overhead and complexity.
Installing MicroPython on Raspberry Pi Pico
- Connect to Pico using microUSB cable
Let’s see what official documentation says:
Raspberry Pi Pico Python SDK PDF, page 4:
Raspberry Pi Pico has a BOOTSEL mode for programming firmware over the USB port. Holding the BOOTSEL button when powering up your board will put it into a special mode where it appears as a USB Mass Storage Device. First make sure your Raspberry Pi Pico is not plugged into any source of power: disconnect the micro USB cable if plugged in, and disconnect any other wires that might be providing power to the board, e.g. through the VSYS or VBUS pin. Now hold down the BOOTSEL button, and plug in the micro USB cable (which hopefully has the other end plugged into your computer).
A drive called RPI-RP2 should pop up. Go ahead and drag the MicroPython firmware.uf2 file onto this drive. This programs the MicroPython firmware onto the flash memory on your Raspberry Pi Pico.
It should take a few seconds to program the UF2 file into the flash. The board will automatically reboot when finished, causing the RPI-RP2 drive to disappear, and boot into MicroPython.
- Ok. Pico will appear as a 128MB USB drive. If not, disconnect it and plug it again while holding
BOOTSEL
button. - Download MicroPython for your Raspberry (depending on version with or without WiFi)
- Now you can upload MicroPython by copying it to small drive
- It will immediatelly restart and USB drive will disconnect and won’t be available
- Download PuTTY e.g. via Windows Store
- Determine to which serial port Pico is attached, e.g. using Device manager
- Connect to Pico (Serial port, COM5 depends on your config, speed may be 9600 or 115200)
- Terminal will likely appear, if there is program running, try to abort it using
Ctrl+C
. Typehelp()
to get some basic, but useful help
- Now you can try to play with LED using Python shell - or rather REPL (Read, Eval, Print Loop)
1 2 3 4 5
>>> from machine import Pin >>> led = Pin("LED", Pin.OUT) >>> led.value(1) >>> led.value(0) >>>
MicroPython in Visual Studio Code
- Install MicroPico Extension (if you have Remote-SSH extension, make sure to install it on a local computer)
- Now you can basically click on github link above or read extension page, but step-by-step guide is
- Create new folder (e.g
c:\dev-py\mp2
for micropython, test2) - Configure project (
Ctrl+Shift+P
for commands, searchMicroPico: Configure project
) This will create some files and connect to Pico
- Create new file (e.g.
test.py
)
- Paste example code (from VS Code extension readme)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from machine import Pin from utime import sleep pin = Pin("LED", Pin.OUT) print("LED starts flashing...") while True: try: pin.toggle() sleep(1) # sleep 1sec except KeyboardInterrupt: break pin.off() print("Finished.")
- Run it (
Ctrl+Shift+P
:MicroPico: Run current file on Pico
) or using icon in the status bar.
- To make your program run at boot, rename file to
main.py
chooseMicroPico: Upload project to Pico
. However VS Code always retakes control when project is open for editing purposes. - You can download files from Pico, but preferably do it into new, empty directory
- Extra: try
MicroPico: Help > Show Pico Pin Map
- Create new folder (e.g
Summary
This article describes setting of MicroPython and first steps. At the moment I haven’t tried anything but LED, I have no idea how to work with sensors and so on.
C++ and possibilities are uncharted territory which waits to be explored.
Resources
- How to Use VSCode with Raspberry Pi Pico W and MicroPython a bit outdated, but helpful youtube video.
- Raspberry Pi PICO - Starting With MicroPython + Examples - I2C OLED, ADC, PWM
- MicroPico readme file for VS Code extension.
- Raspberry Pi Documentation - MicroPython official documentation.
- Raspberry Pi Pico Python SDK: A MicroPython environment for RP2040 microcontrollers.
- Getting started with Pico
- The C++ SDK
- Raspberry Pi Pico c/C++ SDK