Last Updated on 2nd September 2023 by peppe8o
In this tutorial I’m going to show you how to move your first steps with Raspberry PI Pico, installing the open source Thonny IDE (integrated development environment) and running a very simple command.
The new Raspberry PI Pico changes user approach to this device, because it works as microcontroller (unlikely other Raspberry PI boards working as linux computer). For this reason, starting your first steps with RPI Pico requires a different approach
With the announcement of new Raspberry PI Pico, the RPI Foundation took its first steps in the microcontrollers world. Common Raspberry PI users can be a little confused when getting this board and asking themselves “Where is the SD card slot?”. The answer is that no SD cards are present as Pico stores programs in its internal memory. Another common question is “What OS to use with Raspberry PI Pico?”. Same answer, there’s no operating system as with microcontrollers you will put your program directly on firmware and it will run until you won’t replace the main program.
Many of these questions are similar to ones when comparing the difference between Raspberry PI computer boards and microcontrollers (like Arduino). And Raspberry PI Pico is more similar to Arduino.
What is interesting with RPI Pico, is that you can use MicroPython to program your device.
In following paragraphs I will show you how to install Thonny in your computer to connect and program your Raspberry PI Pico.
What We Need
As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if continue with the project or remove them from the shopping cart. So, hardware will be only:
- A common computer (maybe with Windows, Linux or Mac). It can also be a Raspberry PI Computer board
- Raspberry PI Pico microcontroller (with a common micro USB cable)
Check hardware prices with following links:
To move your first steps with Raspberry PI Pico you need to install Thony IDE. This application is already installed in Raspberry PI OS Desktop. With different computer, you can install it fromt their official page: https://thonny.org.
Use links on top right side to download the installer:
Execute the installation procedure according to your OS. With Windows, it is enought a double click on downloaded installer and follow setup wizard. If not present in your OS, this will also install Python in your computer.
Once installed, run Thonny to get its home window:
This window is splitted in 2 areas. The top one (labelled as “untitled”) is where you can write your scripts before running them. The bottom “Shell” is where you can use an interactive mode to run line by line your commands.
You can see from this image that the shell is running on bundled Python. This means that Thonny is running python commands on your current computer.
If you have never used your Thonny with Raspberry Pi Pico, you will need to add the MicroPython firmware to your Pico. Identify the BOOTSEL button on Pico:
Press the BOOTSEL button and hold it while you connect the other end of the micro USB cable between RPI Pico and your computer. This installs your Raspberry Pi Pico as a mass storage device. You can release BOOTSEL button once you will see a new storage device in your computer, named “RPI-RPI2”.
Back to Thonny window, click on “Python” button in bottom-right side (see picture below). This will allow you selecting the Thonny interpreter:
Select “MicroPython (Raspberry PI Pico)” option. A dialog box will pop up to install the latest version of the MicroPython firmware on your Raspberry Pi Pico. Click the “Install” button to copy the firmware to your Raspberry Pi Pico.
At the end of installation following result will be shown:
Click Close. You have installed RPI Pico firmware and you will not require previous steps on next connection.
Run MicroPython on Raspberry PI Pico
When micropython installation on Pico has been finished, you will see that shell has moved interpreter to Raspberry PI Pico:
A very simple command test is importing the “os” python module and asking info to your PICO device about its OS version. From thonny shell use following command:
This will import os module. Then:
This will return following info:
So showing you RPI Pico info.
Execute your First Python Script
Executing your first script is also simple, moreover if you are going to run it from your connected computer.
Our first example makes the Pico on-board LED blinking, following the most common microcontroller first example.
In your Thonny script section (top window) add following code:
from machine import Pin, Timer led = Pin(25, Pin.OUT) timer = Timer() def blink(timer): led.toggle() timer.init(freq=25, mode=Timer.PERIODIC, callback=blink)
This simple code imports required modules. Then it associate to a “led” variable it internal LED pin number (25) and set this to output mode.
A “timer” variable is initialized and then a blink function is defined. This function is a timer callback function, that means this will be called in a timer instance and will executed at each timer trigger. Its scope is only toggling our on-board LED: if LED is on, this will put to off; if LED is off, this will put to on.
Timer init statement will start trigeering blink function 25 times for second (frequency value).
Click “Run” icon from your Thonny IDE. This will require (only for your script first run) where to save your python code:
Select “This computer” and then select path and file name on the following window.
After saving, your python script will start running and you will see your board LED blinking. Use the “Stop” button from Thonny to stop script execution.
Where To Save Files?
Beginners could also ask where to save files on Thonny.
You can think of “.py” files for your MicroPython project divided into 2 main categories:
- running program
Libraries are “.py” files that include code that make it simpler to execute functions or use objects in your running program. If you need to use a library, you don’t need to run explicitly but you need to link them in your running program with the “import” statement. For example, “import mylib” will make your RPI Pico search for a “mylib.py” in the default libraries path (more info can be found in Adding external modules to MicroPython with Raspberry PI Pico tutorial). Some main libraries are already built in your McroPython firmware and you don’t need to make available it (for example the “machine” module). Libraries / external modules must be always saved in your Raspberry PI Pico storage.
Now, consider that Raspberry PI Pico is a microcontroller. This means that RPI Pico can run both connected to a PC or without a PC with a simple power supply plugged into its MicroUSB port.
If you need to run programs only when the Pico is connected to your PC, you can save the “.py” program to your local PC and run it. In this case, you will always need to start/stop the program from your IDE.
But if you need to execute your program also when your Pico is alone with a simple MicroUSB power supply (without a PC communicating him what to do), then the program has to be located in your Raspberry PI Pico storage. Moreover, once powered the Pico looks at its storage for a “main.py” file. This will be the file automatically executed every time the RPI Pico is powered.
Common Problems With Windows Computers
Some Windows users can experience problems connecting Raspberry PI Pico to their PC.
While the first part should go without problems, installing Pico firmware could result in RPI no more visible from your computer. They can see Raspberry PI Pico in computer devices, but listed as a generic “Board CDC” with a yellow warning icon. Moreover, when trying to forcing connection from thonny by manually configuring interpreter, it can result in access denied errors.
This problem is common to Windows 7 and requires manually installing drivers. Even if Thonny can be installed without an administrative user, drivers require administrator permissions. The procedure, shared from a user on Raspberry PI forum, has following steps:
- Download the USB CDC driver from microchip.com COM_Driver
- unzip the amtel_devices_cdc.inf file to a folder named “pico-serial”
- Change the name of amtel_devices_cdc.inf file to pico-serial.inf
- open/edit the “pico-serial.inf” in a basic editor like notepad
- remove and replace the lines under the following headings:
[DeviceList] %PI_CDC_PICO%=DriverInstall, USB\VID_2E8A&PID_0005&MI_00 [DeviceList.NTAMD64] %PI_CDC_PICO%=DriverInstall, USB\VID_2E8A&PID_0005&MI_00 [DeviceList.NTIA64] %PI_CDC_PICO%=DriverInstall, USB\VID_2E8A&PID_0005&MI_00 [DeviceList.NT] %PI_CDC_PICO%=DriverInstall, USB\VID_2E8A&PID_0005&MI_00 [Strings] Manufacturer = "ATMEL, Inc." PI_CDC_PICO = "Pi Pico Serial Port" Serial.SvcDesc = "Pi Pico Serial Driver"
- Close and save and make sure your file retains the name as pico-serial.inf
- Go to your pc device list, find the pico under Ports, named something like CDC Device. A yellow exclamation mark indicates it.
- Right click on the CDC Device and update or install the driver choosing the file you created from the location you saved it at.
This procedure is available at https://www.raspberrypi.org/forums/viewtopic.php?t=300633.
A pre-configured “inf” file (compressed) can be get from my download area: pico-serial.zip
Interested to do more with your Raspberry PI Pico? Try to look at my Raspberry PI Pico tutorials for useful and funny 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?