Post

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

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
    Copy Micropython
    • 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)
    PuTTY setup
  • Terminal will likely appear, if there is program running, try to abort it using Ctrl+C. Type help() to get some basic, but useful help
    PuTTY MicroPython 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)
    Install extension
  • Now you can basically click on github link above or read extension page, but step-by-step guide is
    1. Create new folder (e.g c:\dev-py\mp2 for micropython, test2)
    2. Configure project (Ctrl+Shift+P for commands, search MicroPico: Configure project) This will create some files and connect to Pico
    3. Create new file (e.g. test.py)
    4. 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.")
      
    5. Run it (Ctrl+Shift+P: MicroPico: Run current file on Pico) or using icon in the status bar.
    6. To make your program run at boot, rename file to main.py choose MicroPico: Upload project to Pico. However VS Code always retakes control when project is open for editing purposes.
    7. You can download files from Pico, but preferably do it into new, empty directory
    8. Extra: try MicroPico: Help > Show Pico Pin Map

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

This post is licensed under CC BY 4.0 by the author.