When one thinks of programming a Raspberry Pi, Python immediately springs to mind. This popular programming language is relatively easy to use and excellent for beginners. So, it is unsurprising that the first Raspberry Pi microcontroller can be programmed using MicroPython. One of the easiest methods of programming the Pico with MicroPython is using the Thonny IDE. This simple development environment is available on a wide variety of operating systems. In this short tutorial we will go through setting up Thonny with the Pico!
Why use the Pico? This is the Raspberry Pi Foundation’s first attempt at an extremely low cost, highly capable, microcontroller. It packs a powerful dual-core processor, plenty of RAM, and numerous multi-function IO pins that make it a popular choice in the microcontroller category. We manufacture a variety of Raspberry Pi Pico add-on boards to extend its functionality.
Depending on which operating system you are using, the steps here will vary slightly. If you are using Windows, Mac, or Linux (Other than Raspberry Pi OS) simply proceed to https://thonny.org and click the appropriate download link. Once downloaded, run the installer.
If you are using the Raspberry Pi OS, Thonny is already installed. You may need to update it using the following code in a terminal window (CTRL + ALT + T)
sudo apt update && sudo apt upgrade -y
With the software installed on the computer, we now need to install the firmware on the Raspberry Pi Pico for MicroPython. This only needs to be done the first time you use a Pico with MicroPython.
Hold the BOOTSEL button down while connecting your Pico to your computer. Depending on your computer / operating system you may be notified that a new USB Drive has been detected.
Click the version in the bottom right corner, and select ‘MicroPython’ (Raspberry Pi Pico)’ from the menu that appears. If this option is not available in the list, it may mean that the Pico is not plugged in, or it was plugged in without the ‘BOOTSEL’ button pressed.
When the correct version is selected, A new window should pop open prompting a firmware installation.
In the window that has now appeared, click ‘Install’ to install the MicroPython firmware onto your Raspberry Pi Pico.
Once the installation has completed, click ‘Close’.
In the top window of Thonny, copy the following code. Just like Python, MicroPython is sensitive to whitespace, so ensure it is formatted exactly as pictured.
There is a small difference in design between the Raspberry Pi Pico W and the standard Pico with regards to the built-in LED. On the Pico, it is connected to GPIO 25 per normal. With the Pico W, the built-in LED is actually connected to the wireless chip and cannot be accessed through GPIO 25, instead you will want to use “LED” in place of 25 for the address.
from machine import Pin from time import sleep led = Pin(25, Pin.OUT) #If using a Pico #led = Pin("LED", Pin.OUT) #If using Pico W while(True): led.value(1) sleep(1) led.value(0) sleep(1)
Click the green “Play” button in the toolbar to test out the code. This will prompt you to save the file to a location. Select “Raspberry Pi Pico” and enter a filename. Once saved, it will run the program on your Pico. The built-in LED should flash once every second.
If you ever need to stop the program or the Pico will not respond, you can force it to stop by hitting the red “Stop” button in the toolbar. Pressing the button one more time will reconnect Thonny to your Pico.
Thonny and your Pico are now ready!
3 thoughts on “Programming the Raspberry Pi Pico with MicroPython”
One note, on a Pico W, use ‘LED’ instead of 25 for the port. Took me a bit of searching online to find out why it wasn’t blinking for me.
Chris @ BCR
Ahhh good catch – we have updated accordingly!
This was a wonderfully easy tutorial for me to start on the Pico H with next to no programming knowledge.