In this tutorial, we will learn how to use Thonny Integrated Development Environment to write programs for Raspberry Pi Pico using MicroPython firmware. By the end of this guide, you will be able to install Thonny IDE in Windows, Linux, and Mac operating systems. Moreover, we will also look through its built-in features, write and run a simple MicroPython code for Raspberry Pi Pico and have a general overview of the environment.
We have a similar guide with ESP32 and ESP8266:
Thonny IDE Introduction
Thonny is an open-source IDE which is used to write and upload MicroPython programs to different development boards such as Raspberry Pi Pico, ESP32, and ESP8266. It is extremely interactive and easy to learn IDE as much as it is known as the beginner-friendly IDE for new programmers. With the help of Thonny, it becomes very easy to code in Micropython as it has a built-in debugger that helps to find any error in the program by debugging the script line by line.
You can realize the popularity of Thonny IDE from this that it comes pre-installed in Raspian OS which is an operating system for a Raspberry Pi. It is available to install on r Windows, Linux, and Mac OS.
We also have a getting started guide for Raspberry Pi Pico using Thonny IDE:
Prerequisites
Before installing Thonny IDE on your Windows, Linux and Mac based PCs make sure you have the latest version of Python3 downloaded and installed. In case you do not have it, you can take a look at our previous article: Getting Started with MicroPython on ESP32 and ESP8266 to see how to install it easily.
Setting up Raspberry Pi Pico for MicroPython
It is very easy to get started with Raspberry Pi Pico using MicroPython. Follow the steps carefully to successfully set up Raspberry Pi Pico to be used in Thonny IDE to program in MicroPython.
- Plug the Raspberry Pi Pico in your computer while holding down the BOOTSEL button.
Release the BOOTSEL button and the following two files will show up. Our computer recognizes it as a storage device. Click INDEX.HTM file.
This will open up the official Raspberry Pi Pico documentation guide.
Now under the Microcontrollers tab, scroll down and click ‘MicroPython.’
A getting started guide with MicroPython for Raspberry Pi Pico will open up. We will have to download the MicroPython bootloader. It is a UFC file. Scroll down and find the link to download.
After the download is completed, drag this file and drop it in your Raspberry Pi Pico folder that opened up when we plugged it in our system. Now it will consist of three files. The Raspberry Pi Pico detects it and reboots.
Now we are ready to program Raspberry Pi Pico in MicroPython using Thonny IDE.
Thonny IDE Installation Steps
In this section, we will list step by step instructions to download and install Thonny IDE on Windows, Linux and Mac OS based computers.
Installing Thonny IDE in Windows Operating System
We will show you how to download and install Thonny in Windows operating system. By Clicking here, you will be redirected to the official page of Thonny IDE. You can download the latest version of the IDE. The window shown below will be displayed. Click on download version 3.3.6 for Windows.
To start the installation process, run the .exe file and the Thonny Setup window opens. Click Next to proceed.
You can set the directory where the installation file should save by clicking on browse button. After specifying the location, click Next.
After that follow the instruction and as soon as the installation process finishes, the following window will open up. We have successfully installed Thonny IDE in Windows.
Installing Thonny IDE in Mac OS X
Thonny IDE is not available as a verified application on App Store. that is why Mac OS X blocks its installation. In order to work in this IDE in our Mac, we will need to unblock it and perform some additional security steps.
- In the menu, click System Preferences
- Inside it, Click on Security & Privacy
- The following dialog box appears. Click on the lock to make changes to your Security settings.
- You will have to enter your username and password and then click the Unlock button
Click ‘Anywhere’ in allow apps to download from, after you have successfully entered your credentials and unlocked.
Now, we will be able to download Thonny just like we did for Windows OS. Now follow these steps to install Thonny IDE in Mac OS X.
- Go to the following website (https://thonny.org/) to download and install the IDE. The following page will open. Click Download version 3.3.6 Mac.
It will start downloading. After the process is completed, go to Downloads and click the .dmg file.
We can bring the shortcut to the desktop by dragging it.
After opening the IDE, the following window will be displayed.
After that follow the instruction and as soon as the installation process finishes, the following window will open up. We have successfully installed Thonny IDE in Mac OS X.
Installing Thonny IDE in Linux
In order to install Thonny IDE on Linux OS, you should first check the Linux distribution you are using and install Python dependencies accordingly.
For Linux OS, you should first install these Python dependencies to proceed to the next step:
- python3
- python3-pip
- python3-tk
This can be achieved in Ubuntu by typing in the following command in the terminal:
sudo apt install python3 python3-pip python3-tk
Press Enter and type the following command to install the Thonny IDE
bash <(wget -O - https://thonny.org/installer-for-linux)
We can also perform the installation via pip3 by giving the command:
sudo pip3 install thonny
In Fedora distribution, use this command to install Thonny IDE:
sudo dnf install thonny
After the installation has completed, type Thonny in the terminal, and the following window will appear. (This is for the case if you used pip3 for installation)
thonny
We have successfully installed the IDE in Linux.
Test Thonny IDE Installation with LED Blinking Example
Now, as we have installed our Thonny IDE for Windows, Linux, and Mac Operating Systems. But the procedure to use this IDE remains the same in all operating systems. Without any further details, let’s get started with Thonny IDE to write our first MicroPython Program.
Running a Simple on-board LED
Open Thonny IDE and at the bottom right hand select the device as Raspberry Pi Pico.
Now, your Raspberry Pi Pico board and the Thonny IDE are connected. This can be seen below in the window. This can be seen by the prompt in the Shell window below.
Now types help() in command prompt and if you get this response from Raspberry Pi Pico that means you have successfully installed and configured Thonny IDE for Raspberry Pi Pico.
Testing Raspberry Pi Pico
Now let us light the built-in LED of our Raspberry Pi Pico board using MicroPython. Write down the following code in the Shell terminal. You will see the onboard LED of Raspberry Pi Pico will turn on. The onboard LED is connected with GPIO25 in Raspberry Pi Pico.
from machine import Pin
Pin(25, Pin.OUT).value(1)
Here we are importing the Pin class from the machine module as we have to set it as an output. The built-in led is connected through GPIO25 and configured as an output through the Pin() method.
Similarly, we can turn the LED off by giving the following command in the shell terminal:
Pin(25, Pin.OUT).value(0)
By accessing value (1) for Pi Pico and value (0), we are turning the LED ON and LED OFF respectively.
After we are able to successfully turn the built-in LED on and off, we can be certain that the IDE is working perfectly with the Raspberry Pi Pico.
Thonny IDE Overview
Now, let us take a look at the Thonny IDE in detail. Once we install our program and click to open it, the following window opens up. This has two sections:
- Editor Section
- Shell or Terminal Section
Editor Section
The Editor section is found at the top half of the screen. In this section, we create our .py files and can open several files at once and work on them.
The Shell section is found at the bottom. Through this, we can give the command to the ESP board without (writing, saving, uploading) it in a separate file. Immediate execution is achieved through this. It also displays all the errors encountered in the code as well as the state of the connection clearly.
Tools Section
The tools section has several different tools with different functionalities. I will describe them in order of the numbers.
- The first icon which shows a paper is for creating a New File. By using this button, we can create several different files and work on them at once.
- This is the Open folder. It helps us in opening a file which was already saved in our PC.
- This is the save button. By clicking on this icon we can save our code in whatever step it is.
- This is the Run button. When we click on it the code runs and is executed.
- This button is used in debugging. This helps us in detecting errors whether they are syntax or logical
- This arrow button (Step Over) is used after the debug button. With this button, we can jump from one line or one block of code to another.
- This arrow button (Step Into) is used after the debug button. With this button, we take a smaller step than the previous button and work deeply.
- This arrow button (Step Out) is also used after the debug button. With this button, we can exit the debugging.
- This is the Resume button. It allows us to go back into play mode instead of debug mode.
- This is the Stop button. By clicking this, the program code halts whichever line it currently is.
Features
Go to View and click on it. Several tabs can be seen. Click on any to customize your IDE according to your needs. You can tick all the tabs which you want to appear on your IDE.
Writing Your First MicroPython Script with Thonny IDE
In this section, we will see how to create a new MicroPython file and how to upload the script to Raspberry pi Pico using Thonny IDE. For demonstration, we will perform a simple experiment of blinking onboard LED of Pi Pico by following all the necessary steps
Create MicroPython File in Thonny IDE
First, we need to create a main.py file that will contain our main program code which would be executed. To create a new file click on the new file icon (1). After that copy the following code in a new file and save it by clicking the save icon (2) with the name main.py.
from machine import Pin
from time import sleep
led = Pin(25, Pin.OUT)
while True:
led.value(1)
sleep(1)
led.value(0)
sleep(1)
When you click on the save icon (2), this pop-up window will appear. This gives two options to save this file either to your computer or directly to your devices such as Raspberry Pi Pico. That means we can also directly upload files to a device. But for now, save it on your computer.
Uploading MicroPython Script
To upload MicroPython script to your device, go to Files and click on ‘Save as’. Select Raspberry Pi Pico from “Where to save to” Window.
Type the file name and press “OK”. It will upload file to your device.
This would upload the code onto our Raspberry Pi Pico board. The onboard LED will start blinking after a delay of 1 second.
Press the run button (4) to run the current script and stop button (10) to stop the script from running.
Alternatively, you can also upload code to a device by using this command :
%upload 'Path_to_file\main.py' main.py
In this user guide, we took a look at another common IDE used with Micro-Python: Thonny. By the end of this article, you would be able to have a better understanding of how to program your Raspberry Pi Pico using Micro-Python with this interactive IDE.
In summary:
In this tutorial, we have learned to program Raspberry Pi Pico using Thonny IDE. This IDE is easy to use to write MicroPython programs for Pi Pico. It is available in Windows, Linux, and Mac operating systems.
Hi. I can run the initial code typed in to the shell and get make the LED switch on and off and I can run the main.py script from Thonny to switch the LED on and off once per second but when I upload the script to the Pico as main.py nothing happens.
Are you using a pico W? try this
import machine
from time import sleep
led = machine.Pin(“LED”, machine.Pin.OUT)
while True:
led.off()
sleep(1)
led.on()
sleep(1)