This guide will help you get started with the Raspberry Pi Pico and the Thonny Python IDE, which has out-of-the-box support for Micropython and Pico.
Let's get set up with scripting in Thonny for the Raspberry Pi Pico.
We'll install Thonny, configure for Pico and write our first script. To follow along you'll need:
Contents
If you're working on a Raspberry Pi 4, you're in luck - Thonny comes pre-installed. For those on another operating system, download Thonny here and run the installer.
Once the installer finishes, run Thonny.
Hold the BOOTSEL button on your Pico, and connect your Pico to your computer via USB.
Go to Run > Select interpreter and choose MicroPython (Raspberry Pi Pico).
It's also a good idea to install or update firmware. This will update your Pico with the latest version of MicroPython, or install MicroPython if it wasn't already.
We can immediately start executing code in the REPL - Enter this code in the shell tab: print("Hello, World!")
The command will be sent to your Pico, which will execute the command and display back the message.
We can also take control of the on-board LED by executing the following code:
from machine import Pin led = Pin(25, Pin.OUT) led.toggle()
This code will toggle the LED. If you keep executing led.toggle() the LED will keep changing state.
Writing a Script
Create a new script with File > New and paste in the following code:
from machine import Pin from time import sleep led = Pin(25, Pin.OUT) n = 0; while True: led.toggle() print("13 x {} = {}".format(n, 13*n)) # print the thirteen-times table n = n + 1 sleep(0.5)
Save the script - you will be prompted to save to your computer OR the pico. Select save to Pico and name the file main.py
Return to the REPL and press Ctrl+D (or use the Stop/Restart button) to restart your Pico. The LED should flash at a steady rate and the shell should begin printing multiples of thirteen.
Installing a Package
Packages are reusable pieces of code that another programmer has written for a common task, such as interfacing with a specific piece of hardware. Thonny has support for installing micropython packages from the Python Package Index - aka 'PyPI' directly onto the Raspberry Pi Pico.
To install a package, ensure the Pico is plugged in and go to Tools > Manage Packages, which will show the Manage Packages dialog.
Enter the name of the package you would like to install into the search bar and click 'Search on PyPI'.
In the search results list, click the title of the package you would like to install. A title that's in bold simply indicates a direct search match on the text you entered in the search bar.
The Manage Packages dialog will show an overview of the package you have clicked. Click Install to install the package on your Pico.
You can confirm the package has been installed by checking the 'Raspberry Pi Pico' section of the File View in Thonny. The view should show a new folder named 'lib', and inside this folder will be one or more folders containing the metadata and source code of the library you just installed.
Conclusion
We've installed Thonny and uploaded scripts to our Raspberry Pi Pico - if you have any questions feel free to start the conversation below, or open a topic in our forums - we're full time makers and happy to help!