In this tutorial we’ll see how to start programming the Raspberry Pi Pico or Raspberry Pi Pico W. We’ll use MicroPython to let blink the onboard LED of the Raspberry Pi Pico (W).
Once this tutorial completed, you will have had a quick introduction of how to connect the the Raspberry Pi Pico or Raspberry Pi Pico W to a computer, how program it and how it can be used without being attached to a computer.
- Prepare the hardware
– First you need a computer to run Thonny. Thonny is a user-friendly Python IDE (Integrated Development Environment) to interact with the Raspberry Pi Pico board. In this tutorial we use a Raspberry Pi computer running on Raspberry Pi OS, it comes with Thonny preinstalled. So, you don’t need to install any additional software. Just make sure you’re running the latest version of Raspberry Pi OS. If necessary, have a look at our tutorial “How to update your Raspberry Pi to the latest version”. But no worries if you don’t have a Raspberry Pi computer. You can install Thonny on any PC if you want. On the Thonny website you can download this IDE for free.
– Next you need an USB cable with micro-USB plug.
– And finally you need a Raspberry Pi Pico of course. For this tutorial it doesn’t matter if GPIO-pins are soldered or not.
Visit our shop if you miss any components.
- Connect the Pico board to your computer
If your Raspberry Pi Pico board has never been used, you can simply connect your board to your computer with the USB-cable.
But if your Pico board has been used and firmware is present on it, it’s necessary to push and hold the white BOOTSEL button while you connect the board to your computer. Release the button a few seconds after both elements are connected.
- Check if the Pico board has been recognised by your computer
Before continuing, make a quick check if your Pico board is well connected to your computer. If necessary, repeat step 2 and/or check your USB-cable.
You can close the Popup window “Removable medium is inserted”.
- Open Thonny Python IDE
You can find Thonny in the applications menu (left upper corner of your screen) as shown in the figure below. If you don’t find “Tonny” between your applications, then you probably don’t have the latest version of Raspberry Pi OS. Consult our tutorial “How to update your Raspberry Pi to the latest version” before continuing.
- Change the interpreter
To program our Pico board, we use MicroPython. MicroPython is a Python Language Interpreter that is developed for Microcontrollers as our Pico. The syntax is very similar to Python.
As the default interpreter for Thonny is Python 3, we need to change it to MicroPython. You can do this by clicking on the “Python” text at the right bottom of the Thonny window.
- Install MicroPython firmware on the Raspberry Pi Pico (skip this step for a Raspberry Pi Pico W)
As soon as you chose to change the interpreter to MicroPython (and your Pico is connected to your computer), a PopupWindow appears asking you to install the MicroPython firmware on the Pico board. Click on the ‘Install’ button to start the firmware installation. It only takes a few seconds to install the firmware.
If you have previously stored scripts on your already used board, they won’t be overwritten with this operation.
And if for some reasons you got an error message, you can also install the firmware on your Pico with a drag and drop procedure. This method is explained in the official Raspberry Pi Documentation.
- Install MicroPython firmware on the Raspberry Pi Pico W (skip this step for a Raspberry Pi Pico)
Unlike with the Raspberry Pi Pico, you can’t use Thonny to upload the firmware onto your Raspberry Pico W. The drag-and-drop procedure is a little different but remains quick and easy.
First you need to download the MicorPython UF2 file for the Raspberry Pi Pico W. Be careful, this file is not the same as for the Raspberry Pi Pico. Click on the link below to start the download:
If your Raspberry Pi Pico W is connected to a Raspberry Pi computer and you’re following this tutorial on a different computer, transfer the downloaded file to your Raspberry Pi computer. If you don’t know how to do, check our tutorial
“How to transfer files between Raspberry Pi and PC“. Another option is to open a browser on your Raspberry Pi computer and download the file directly on your Raspberry Pi computer.
Next, (disconnect and) connect your Raspberry Pi Pico W by pushing and holding the white BOOTSEL button. See step 2 and 3 here above.
Finally, drag and drop the MicroPython UF2 file you just downloaded onto the RPI-RP2 volume appearing on the desktop. After this operation, your Raspberry Pico W will reboot.
- Write the code
The aim is here to write a very basic script to let blink the onboard LED of the Pico board. Let blinking an LED is often used as the ‘Hello world’ program for microcontrollers :-).
Write or paste following code in the IDE:
from machine import Pin
led = Pin(25, Pin.OUT)
Be careful, MicroPython is whitespace-sensitive. Don’t remove the “tab” before
Warning ! If you are programming a Raspberry Pi Pico W, you need to replace the 25 by “LED”. The third line from the code here above should look like this :
led = Pin('LED', Pin.OUT)
- Save your script
Now, it’s time to save your script. You can either save it on your computer or on your Pico board. In our case, we chose to save it on our Pico board.
And enter a file name. We’ll name our script ‘blink.py’. No problem to chose your own filename of course. Just keep in mind to add the ‘.py’ extension at the end.
- Run your script
Click on the Run button and the builtin LED of the Pico board should start blinking. To stop the LED blinking, just click on the STOP button.
- Let your Pico run without being attached to your computer
In this last step we see what to do to let your Pico run the MicroPython script without being attached to your computer.
Like this, you will be able to connect your Pico to a battery or an USB power supply for example. Just pay attention to the voltage ! It’s safe to use a voltage between 1.8V and 5.5V.
To let run your MicroPython script automatically, without being connected to the computer, just save the script (File > Save as…) to your Pico board and name it ‘main.py‘.
Congratulation! You made your first MicroPython script for the Raspberry Pi Pico. Have lots of fun with your next projects!
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?
1 thought on “How to start programming the Raspberry Pi Pico”
Jó oktató anyag kezdőknek! Köszönöm (69,5 éves nyugdíjas a sötét estéken).