Created on: 24 December 2021
Easiest way to start using a Raspberry PI Pico is covered in this tutorial. A new Raspberry PI Pico microcontroller board can be quickly and easily tested. This is done by switching and blinking the on-board LED using the Python programming language with MicroPython. A “hello world” program is also included in this tutorial.
There is no need to do any soldering or circuit building when doing a quick test of the Raspberry PI Pico microcontroller board. The USB port powers the board and a program controls the on-board LED. Use the following hardware with this tutorial.
This is a summary of the more detailed steps on the easiest way to start using a Raspberry PI Pico microcontroller board. The steps are as follows with details in the next section.
Follow the tutorial steps below to easily start using the Raspberry PI Pico board. Program the board using the Thonny IDE running on the host computer, as described in the steps. “Host computer” refers to the computer used to program the target Raspberry PI Pico board in this tutorial.
Plug the Raspberry PI Pico board into a free USB port of the host computer using a micro USB cable.
After plugging the Pico board into the host computer, the board appears as a Flash drive. If it does not, then unplug the board from the USB port. Hold down the BOOTSEL switch found on top of the board and plug the board back into the USB port. After three seconds, release the BOOTSEL switch.
The Pico board opens in a new file manager window. On Linux Mint MATE the file manager opens as shown in the image below.
On a Windows computer the file manager opens as shown in the following image.
Leave the Pico board Flash drive window open. Download and copy a file to the Pico using the file manager window as explained in the next steps.
Download the UF2 MicroPython file from the Raspberry PI website. The Raspberry PI MicroPython page has a link to the same file.
Locate the UF2 file on the host computer downloaded in the previous step. At the time of writing the file name is rp2-pico-20210902-v1.17.uf2. This file will have a different date and version number if it is updated. Drag this file and drop it into the Raspberry PI Pico Flash drive folder that opened in the first step of this tutorial. This copies the file to the Pico board. After copying has finished, the Pico Flash drive folder disappears. The MicroPython interpreter is now running on the Pico board and the board is ready to be programmed using the Thonny IDE.
Visit the Thonny website to download the Thonny MicroPython IDE. Thonny is available for Windows, Mac and Linux. Scroll down the Thonny page to find download and install instructions. Follow the instructions and install Thonny on the host computer.
Thonny is the easiest way to start using a Raspberry PI Pico by programming the board using MicroPython. Change a setting in Thonny in the following steps to use Thonny with the Pico board.
Start the Thonny IDE installed in the previous step. It is found in a Thonny folder on the Windows menu, and on the Programming menu of a Linux Mint MATE computer. After a default install, the Thonny IDE looks as follows when started.
By default, the Thonny IDE is set up to use the Python interpreter that was installed with it. A setting must be changed to make it use the MicroPython interpreter that was installed to the Pico board in step three of this tutorial.
On the top menu bar of the Thonny IDE, select Tools → Options… to open the Thonny options dialog box. Click the Interpreter tab in the dialog box. In the drop-down box on the Interpreter tab, select MicroPython (Raspberry Pi Pico) as shown in the following image.
Click the OK button to close the dialog box. If a message is displayed at the bottom of the Thonny window that says that it couldn’t find the device automatically, then first unplug the Pico board from the USB port. Close the Thonny application window. Plug the Pico board back into the USB port. Finally start Thonny again. The following image shows the message that appears in the bottom Shell pane of Thonny after Thonny connects to MicroPython that is running on the Pico board, a message is shown in the bottom Shell pane, as shown in the image below.
Note in the image that MicroPython (Raspberry Pi Pico) appears at the bottom right of the Thonny window. If this is not selected, go back and change it using the Thonny options dialog box. If there are any other problems connecting to the Pico board from Thonny, then try restarting the host computer. This includes problems when following the remainder of this tutorial.
Enter the following line of code in the bottom Shell pane of Thonny to set up the pin connected to the on-board LED as an output pin.
led_onboard = machine.Pin(25, machine.Pin.OUT)
Switch the LED on by entering the following.
led_onboard.toggle()
Check that the green LED next to the micro USB port of the Raspberry PI Pico board switches on, as the next image shows.
Enter the above line of code again to switch the LED off. Entering the same code again after the LED is off switches it on again. Press the keyboard up arrow key to bring up the last line of code typed. This avoids having to type the line of code over again. After the line of code appears in the window, press the Enter key to run it. The following image shows what the bottom Shell pane of Thonny looks like after toggling the on-board LED on and off a few times.
It is easy to write a one line “hello world” program for the Raspberry PI Pico using the bottom interactive Shell pane in the Thonny IDE. Simply enter the following line of code at the prompt in the bottom Shell pane.
print("Hello, world!")
Running the above line of code results in the message between double-quotes being printed in the bottom shell pane below where the code was entered. The following image shows the result.
The image shows that the print() command was entered below the previous commands. The result is printed back to the Shell window by MicroPython running on the Pico board.
In the previous code examples, the interactive Shell window was used to run code on the Pico board. A Python program can be entered into the top script pane of Thonny and saved to the Pico board. Enter the following code into the top script pane of Thonny.
import utime led_onboard = machine.Pin(25, machine.Pin.OUT) while True: led_onboard.toggle() utime.sleep(1)
When entering the code, use the Tab keyboard key to indent the last two lines of code. Only the second last line actually needs to be indented using the Tab key. When the Enter key is pressed after the second last line, then Thonny IDE automatically indents the next line of code.
After entering the above code, save the file using the Ctrl + S keyboard keys. Alternately, select File → Save using the top Thonny menu or the Save toolbar icon. A dialog box prompts to save either to the host computer or to the Pico board, as the following image shows.
Click Raspberry Pi Pico in the Where to save to? dialog box. In the save dialog box that opens, enter a file name of blink.py in the bottom File name field. Click the OK button when done. This saves the script entered into Thonny as a file called blink.py in the Pico board memory.
To run the script, click the Run current script icon on the top toolbar (the green icon). Hover the mouse cursor over the toolbar icons so that the tool tip pops up to show what each item is. If the script was entered correctly and the board is working, the LED should start blinking or flashing at a slow rate.
Click the red STOP icon to stop the script from running.