The Raspberry Pi Pico is the first microcontroller-based development board from the Raspberry Pi Foundation. Instead of the Linux operating system found on other Raspberry Pi boards, the Pico must be attached to another computer to program it.

Microcontroller programming is a difficult subject to learn, but luckily the Raspberry Pi foundation has made it easy to get up and running with the Pico. Today you'll learn how to install all the tools needed to get started with the Raspberry Pi Pico.

These instructions focus on Windows, but Linux and Mac installation are very similar, just make sure to get the tools for your operating system instead.

1. Install MicroPython on the Raspberry Pi Pico

MicroPython is a special branch of the Python programming language devoted to microcontrollers. It has simple to understand syntax and extensive libraries designed to make programming various hobby development boards easier for beginners. Note that while regular Raspberry Pi boards do use Python, you'll need to follow a dedicated Raspberry Pi tutorial as the steps here don't apply to single-board computers.

The Raspberry Pi Foundation has made it incredibly easy to install MicroPython onto the Pi Pico. It uses the UF2 file extension, designed specifically for flashing microcontrollers over USB. Instead of needing a special programmer or piece of software, you can copy code over like you would a file to a pen drive or external hard drive.

The MicroPython environment is available as a downloadable UF2 file from the Pi Foundation website.

MicroPython UF2 download

To install the MicroPython environment on to your Raspberry Pi Pico, follow these steps:

  1. Download the MicroPython UF2 file from the Raspberry Pi website
  2. Hold down the BOOTSEL button on your Pico and plug it into your computer's USB port.
  3. Open Explorer, and open the RPI-RP2 directory like you would any other hard drive
  4. Drag and drop the UF2 file into the RPI-RP2 directory

That's it! It might not seem like much has happened, but you are now running MicroPython on your Pi Pico. You could now open a terminal program like Putty to talk to the Pi Pico over the USB Serial port, but there's a much better way to interact with your Pico: The Thonny IDE.

2. Install the Thonny IDE

Thonny is an open-source Python integrated development environment (IDE) designed for beginners. It's powerful, easy to understand, and already comes with MicroPython and Raspberry Pi Pico support.

To get Thonny, download it for free from the official website by clicking the link in the top right corner.

Thonny website

When the download finishes, install and open the Thonny IDE. You'll be asked what language you'd like Thonny to run in before being greeted with a new Thonny window. Make sure your Pi Pico is plugged in, click on the button on the bottom right of the window that reads Python, and change it to MicroPython (Raspberry Pi Pico).

Changing the Thonny interpreter

The REPL window should change to show you are now running on the Pico, and you can test it out with a quick Hello World!

Hello Pico REPL test

Now that it's working, let's move on to coding something.

3. Program the Raspberry Pi Pico

MicroPython is identical in syntax to regular Python, and if you aren't familiar, it's worth learning the basics of Python to understand Pi Pico code better. If you don't know Python, don't worry! This tutorial uses example code to get you going without needing any previous programming experience.

The Raspberry Pi Foundation provides example code to help you get started coding the Pico, which is available from its official GitHub repository. To get the examples, click on Code > Download ZIP and extract them to a directory of your choice. In Thonny, use Ctrl + o or select File > Open to open the blink.py example. The code should look like this:

        from machine import Pin, Timer

led = Pin(25, Pin.OUT)
tim = Timer()
def tick(timer):
    global led
    led.toggle()

tim.init(freq=2.5, mode=Timer.PERIODIC, callback=tick)

Click the green run button. A popup will ask you where you want to save the file. Select your Raspberry Pi Pico, and rename the file to main.py.

Save script dialog box

You should see your LED blinking! Renaming the file to main.py is optional, though if you want your code to run when the Pico is connected to an external power source rather than a computer, you'll need to do it. The Pico looks for a main.py when it boots up for instructions, and if it isn't there, it won't do anything.

Another neat thing you may notice is that the REPL is still active. The timer and LED are working in the background now, leaving you free to send more commands to the Pico through the REPL.

5. Something More Advanced

Getting an LED to blink is a great start, but to get a sense of just how useful the Raspberry Pi Pico can be, let's test the onboard temperature sensor. Once again, the Raspberry Pi foundation makes this easy to do. It provides example code to read from the onboard sensor, convert it into human-readable temperature information, and print it to the Thonny REPL.

Pi Pico Temperature sensor

Open adc > temperature.py in the examples folder, or simply copy the raw code directly from GitHub into Thonny, before saving it as main.py. The code should look like this:

        
import machine
import utime

sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)

while True:
    reading = sensor_temp.read_u16() * conversion_factor
    
    # The temperature sensor measures the Vbe voltage of a biased bipolar diode, connected to the fifth ADC channel
    # Typically, Vbe = 0.706V at 27 degrees C, with a slope of -1.721mV (0.001721) per degree.
    temperature = 27 - (reading - 0.706)/0.001721
    print(temperature)
    utime.sleep(2)

Click the green run button, and the code should start to run, printing the current ambient temperature into the Thonny REPL.

6. Let Your imagination Go Wild

Now that you are set up to program the Pico, you can experiment with its features using the MicroPython library. There are already many beginner projects and tutorials for the Pi Pico, and the Raspberry Pi Foundation has even released an official book on the Pico, available from the Raspberry Pi website.

Raspberry Pi Pico: Cheap but Powerful

The Raspberry Pi Pico is a fantastic microcontroller for the money and capable of much more than there was space to show in this brief introduction.

To regular Raspberry Pi users, this way of working may feel a little strange, but microcontrollers are cheap and reliable, and there are few better ways to learn to program them than with the Raspberry Pi Pico.