In this tutorial we’ll see how to start programming the Raspberry Pi Pico. We’ll use MicroPython to let blink the onboard LED of the Raspberry Pi Pico.
Once this tutorial completed, you will have had a quick introduction of how to connect the the Raspberry Pi Pico 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
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.
- 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
- 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?