An Easy-to-Follow Step-by-Step Tutorial for Getting Started with MicroPython on Raspberry Pi Pico

This guide will walk you through the process of programming a Raspberry Pi Pico using MicroPython, a beginner-friendly programming language specifically designed for microcontrollers. We’ll cover ever …


Updated September 16, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


This guide will walk you through the process of programming a Raspberry Pi Pico using MicroPython, a beginner-friendly programming language specifically designed for microcontrollers. We’ll cover everything from setting up your development environment to writing and running your first code on the Pico board. By following this tutorial, you’ll be able to start building exciting projects with Raspberry Pi Pico in no time!

Overview

Raspberry Pi Pico is a tiny, low-cost microcontroller developed by the Raspberry Pi Foundation. It’s powered by a dual-core ARM Cortex M0+ processor and comes with a wealth of features including 26 GPIO pins, built-in USB, and Bluetooth connectivity. MicroPython is an easy-to-learn programming language that allows you to write Python code directly on the Pico board without having to compile it or install any additional libraries.

In this tutorial, we’ll cover the following topics:

  1. Setting up your development environment
  2. Installing MicroPython on Raspberry Pi Pico
  3. Writing and running your first program
  4. Working with GPIO pins
  5. Using built-in hardware peripherals (such as LEDs, buttons, and sensors)
  6. Interfacing with external devices
  7. Debugging and troubleshooting

Setting up your development environment

Before we start programming the Pico board, we need to set up our development environment. Here are the steps:

Install MicroPython on your computer

First, you’ll need to download and install MicroPython on your computer. You can find the latest release on the official MicroPython GitHub page: https://github.com/micropython/micropython/releases. Choose the appropriate version for your operating system (Windows, macOS, or Linux) and follow the installation instructions provided in the README file.

Install a code editor

Next, you’ll need a text editor to write your Python code. You can use any text editor you like, but we recommend using an IDE (Integrated Development Environment) specifically designed for MicroPython, such as Thonny: https://thonny.org/. Download and install Thonny, then start it up.

Connect the Pico board to your computer

Now, connect your Raspberry Pi Pico board to your computer using a USB cable. Once connected, you should see a new drive appear on your computer’s file system (usually named “RPI-RP2”). This is the mass storage device that MicroPython uses to transfer files between your computer and the Pico board.

Installing MicroPython on Raspberry Pi Pico

With your development environment set up, it’s time to install MicroPython on the Pico board. Here are the steps:

  1. Download the latest version of MicroPython UF2 bootloader for Raspberry Pi Pico from the official GitHub page: https://github.com/raspberrypi/pico-bootloader/releases. Choose the “UF2” file that matches your operating system (Windows, macOS, or Linux).

  2. Drag and drop the UF2 file onto the Pico’s mass storage drive (“RPI-RP2”) to flash MicroPython onto the board. The board will automatically reboot after installation is complete.

  3. Once the board reboots, it should show up as a new drive on your computer (usually named “RPI-RP2”). Open Thonny and click “Tools” -> “MicroPython (Raspberry Pi Pico)” -> “Select device”. Choose the appropriate drive to connect Thonny to the Pico board.

Writing and running your first program

Now that MicroPython is installed on the Pico board, let’s write our first program. Open Thonny and create a new file named main.py. This is the default filename that MicroPython will look for when it starts up. Add the following code to the file:

print("Hello, Raspberry Pi Pico!")

Save the file and click “Run” in Thonny to execute your program. The message “Hello, Raspberry Pi Pico!” should appear in the console window.

Working with GPIO pins

Raspberry Pi Pico has 26 GPIO (General Purpose Input/Output) pins that you can use for various purposes, such as reading sensor inputs or controlling LEDs. Here’s an example of how to turn on an LED connected to pin 25:

from machine import Pin
import time

led = Pin(25, Pin.OUT)    # Set up GPIO pin 25 as output

while True:
    led.value(1)        # Turn the LED on
    time.sleep(0.5)    # Wait for 0.5 seconds
    led.value(0)      # Turn the LED off
    time.sleep(0.5)    # Wait for 0.5 seconds

In this code, we first import the Pin class from the machine module and create an instance of it to represent pin 25 (led). We set the pin mode to output using Pin.OUT. Then, we use a while loop to turn the LED on for 0.5 seconds, followed by turning it off for 0.5 seconds.

Using built-in hardware peripherals

Raspberry Pi Pico comes with several built-in hardware peripherals that you can use in your programs, such as an LED and a button. Here’s how to blink the onboard LED when the button is pressed:

from machine import Pin
import time

led = Pin(25, Pin.OUT)     # Onboard LED connected to pin 25
button = Pin(2, Pin.IN)  # Button connected to pin 2

while True:
    if button.value() == 0:   # If the button is pressed (0 means pressed for pull-down button)
        led.value(1)      # Turn the LED on
    else:
        led.value(0)      # Turn the LED off
    time.sleep(0.01)     # Wait for 10 ms before checking again

In this code, we create an instance of Pin class to represent both the LED (led) and the button (button). We set the button’s mode to input using Pin.IN. Then, we use a while loop to check the button state every 10 milliseconds (0.01 seconds) and toggle the LED accordingly.

Interfacing with external devices

You can connect various external sensors and devices to Raspberry Pi Pico using its many input/output pins. Here’s an example of how to read data from a digital temperature sensor connected to pin 4:

from machine import Pin, ADC
import time

temp_sensor = ADC(Pin(4))   # Create an instance of the ADC class for reading temperature sensor values
conversion_factor = 3.3 / (65535)  # Calculate conversion factor based on sensor's output range

while True:
    temp_raw = temp_sensor.read_u16()   # Read the raw ADC value from the temperature sensor
    temperature = temp_raw * conversion_factor  # Convert the raw value to voltage
    print("Temperature: {:.2f} V".format(temperature))
    time.sleep(1)         # Wait for 1 second before reading again

In this code, we create an instance of ADC class to read values from the temperature sensor connected to pin 4. We then calculate the conversion factor based on the sensor’s output range and use a while loop to print the temperature in Volts every second.

Debugging and troubleshooting

MicroPython has built-in tools for debugging and troubleshooting your programs. Here are some common issues you might encounter and how to resolve them:

Syntax errors

If MicroPython complains about a syntax error, check your code for any missing brackets or parentheses. The MicroPython interpreter will usually point out the line number where the error occurred.

Runtime errors

Runtime errors can be caused by incorrect GPIO pin numbers, incorrect module names, or other issues related to hardware interactions. Make sure you have connected your external devices correctly and that your code is using the correct pin numbers and module names. You can also add print() statements in your code to check the values of variables at different points to help identify where things are going wrong.

General errors

If you’re having trouble with a particular operation, try searching for information online or on MicroPython’s forum. The community is very helpful and might have encountered similar issues before. You can also post your own questions there if you get stuck.

Conclusion

MicroPython is a powerful tool for embedded programming that allows you to write Python code directly on microcontrollers like the Raspberry Pi Pico. With its easy-to-use syntax and vast library of modules, it’s an excellent choice for prototyping and building IoT devices. In this tutorial, we covered how to get started with MicroPython on Raspberry Pi Pico, working with GPIO pins, interfacing with external hardware, and debugging your code.