If you want to program your ESP32 and ESP8266 with MicroPython firmware, it’s very handy to use an IDE. In this guide, we’ll introduce you to Thonny IDE. After completing this guide, you’ll have your first LED blinking using MicroPython and Thonny IDE.
We’ve experimented with several IDEs to program the ESP32 and ESP8266 boards using MicroPython, and Thonny seemed a good choice. Although there are some bugs, it is constantly being updated and improved.
It allows you to program your ESP32 and ESP8266 boards with MicroPython, and it is compatible with Windows, Mac OS X, and Linux. It even comes installed by default on the Raspberry Pi OS. Additionally, it’s easy to install, so you shouldn’t have problems with the installation process.
Alternatively, you may want to check the following compilation of MicroPython-compatible IDEs:
What is MicroPython?
MicroPython is a Python 3 programming language re-implementation targeted for microcontrollers and embedded systems. MicroPython is very similar to regular Python. Apart from a few exceptions, the language features of Python are also available in MicroPython. The most significant difference between Python and MicroPython is that MicroPython was designed to work under constrained conditions.
Because of that, MicroPython does not come with the entire pack of standard libraries. It only includes a small subset of the Python standard libraries, but it includes modules to easily control and interact with the GPIOs, use Wi-Fi, and other communication protocols.
Installing Thonny IDE
In this guide, we provide instructions to install Thonny IDE in different operating systems, read the section that fits your needs:
- A) Installing Thonny IDE – Windows PC
- B) Installing Thonny IDE – Mac OS X
- C) Installing Thonny IDE – Linux
Tip: Thonny IDE comes installed by default on Raspbian OS that is used with the Raspberry Pi board.
A) Installing Thonny IDE – Windows PC
To install Thonny on your Windows PC, follow the next instructions:
1. Go to https://thonny.org
2. Download the version for Windows and wait a few seconds while it downloads.
3. Run the .exe file.
4. Follow the installation wizard to complete the installation process. You just need to click “Next”.
5. After completing the installation, open Thonny IDE. A window as shown in the following figure should open.
B) Installing Thonny IDE – Mac OS X
Since Thonny IDE is open source and downloaded from the Internet, it’s not a verified app in the App Store. For security reasons, Mac OS X blocks unknown apps to run on your computer. Follow these next instructions to enable any downloaded software to run in your Mac.
1. Open the “System Preferences...” menu.
2. Open the “Security & Privacy” menu.
3. At the bottom left corner, click the lock icon to modify your “Security & Privacy” settings:
4. Type your username/password and click the “Unlock” button.
5. Finally, select the option “Allow apps downloaded from: Anywhere“.
That’s it, you can close that window.
To install Thonny on Mac OS X, follow the next instructions:
1. Go to https://thonny.org
2. Download the version for Mac OS X and wait a few seconds while it downloads.
3. Open the .dmg file.
4. Drag the “Thonny” application to your Desktop:
5. Thonny IDE is now installed and you can double-click to open it:
6. After the installation is completed, open Thonny IDE. A window as shown in the following figure should open.
C) Installing Thonny IDE – Linux
To install Thonny on your Linux computer, it depends on your Linux distribution and version, follow the next instructions for your system. First, we recommend installing these dependencies: python3, python3-pip, and python3-tk
If you’re in Ubuntu, you can install the Python dependencies like this:
sudo apt install python3 python3-pip python3-tk
After having Python3, pip3, and Python3 Tkinter, you can install Thonny IDE.
- Ubuntu (after running that command, you’ll need to press Enter again to install the software):
bash <(wget -O - https://thonny.org/installer-for-linux)
- Or you can install Thonny IDE with pip3:
sudo pip3 install thonny
- Fedora since 27:
sudo dnf install thonny
- Raspbian since Stretch (installed by default):
sudo apt install python3-thonny
After installing Thonny IDE and depending on your installation method, to open Thonny IDE:
- You either need go to the search bar and type “Thonny” to find it
- Or if you installed Thonny IDE using pip3, you can type in your terminal window:
Then, it should open Thonny IDE:
Flashing MicroPython Software
MicroPython isn’t flashed onto the ESP32 or ESP8266 boards by default. The first thing you need to do to start programming your boards with MicroPython is flash/upload/burn the firmware.
There are different ways in which you can do that. Thonny IDE comes with a tool that allows you to quickly install MicroPython firmware on your board. Another method is using esptool—check this tutorial to burn MicroPython firmware with esptool.py if you have trouble flashing the firmware with Thonny IDE.
Downloading MicroPython Firmware
Go to the MicroPython Downloads page: https://micropython.org/download/.
Select the type of board you’re using. Here are the quick links for “regular” ESP32 and ESP8266 boards:
You should see a similar web page (see figure below) with links to download .bin files. Download the latest release.
At the time of writing this article, the latest release is v1.17 (2021-09-02).bin for the ESP32 and ESP8266 boards. Don’t download the Nightly builds; those versions are not stable and are only recommended for advanced programmers.
The downloaded file will probably go to the Downloads folder. Continue reading to learn how to flash the firmware on your boards.
Flashing MicroPython Firmware using Thonny IDE
In this section, you’ll learn how to flash MicroPython firmware on your boards using Thonny IDE. Follow the next steps:
1) Connect your ESP32 or ESP8266 board to your computer.
2) Open Thonny IDE. Go to Tools > Options > Interpreter.
3) Select the interpreter you want to use accordingly to the board you’re using and select the COM port your board is connected to. Finally, click on the link Install or update firmware.
4) Select the port once again, and then click on the Browse button to open the .bin file with the firmware you’ve downloaded on the previous step. Select the options as shown in the picture below and finally click on Install.
After a few seconds, the installation should be completed.
Testing the Installation
Important: before testing the installation, your ESP32/ESP8266 board needs to be flashed with MicroPython firmware (see the previous step).
Connect the board to your computer using a USB cable. To test the installation, you need to tell Thonny that you want to run MicroPython Interpreter and select the board you are using.
1. Go to Tools > Options and select the Interpreter tab. Make sure you’ve selected the right interpreter for your board as well as the COM port.
You can also select the “Try to detect automatically” option, but only if you just have one board connected to your computer at a time. Otherwise, select the specific port for the board you’re using.
2. Thonny IDE should now be connected to your board and you should see the prompt on the Shell.
3. Type the command help() in the Shell and see if it responds back.
If it responded back, everything is working fine. Now, you can send a few more commands to test.
Send the following commands to light up the on-board LED
>>> from machine import Pin >>> Pin(2, Pin.OUT).value(1)
If you’re using an ESP8266, the logic to turn on the LED works the other way around, so you should send the following command instead:
>>> Pin(2, Pin.OUT).value(0)
The on-board LED should light up.
Then, turn off the led:
>>> Pin(2, Pin.OUT).value(0)
Congratulations! Your installation was successful!
Thonny IDE Overview
At this point, you should have:
- Thonny IDE installed on your computer
- ESP32/ESP8266 flashed with MicroPython firmware
Open Thonny IDE. There are two different sections: the Editor and the MicroPython Shell/Terminal:
- The Editor section is where you write your code and edit your .py files. You can open more than one file, and the Editor will open a new tab for each file.
- On the MicroPython Shell you can type commands to be executed immediately by your ESP board without the need to upload new files. The terminal also provides information about the state of an executing program, shows errors related with upload, syntax errors, prints messages, etc…
You can also customize Thonny IDE to show other useful tabs. Go to View and you can select several tabs that provide more information.
Something that could be very useful is the Variables tab. It lists all available variables in your program and their corresponding values.
Running Your First Script
To get you familiar with the process of writing a file and executing code on your ESP32/ESP8266 boards, we’ll upload a new script that simply blinks the on-board LED of your ESP32 or ESP8266.
Creating the main.py file on your board
1. When you open Thonny IDE for the first time, the Editor shows an untitled file. Save that file as main.py by clicking on the save icon. Select the “This computer” option. We recommend saving the file inside a folder with a name that identifies the project, for example Blink. The file must be called main.py. Otherwise, it will not work.
2. The Editor should now have a tab called main.py.
3. Copy the following code to the main.py file:
from machine import Pin from time import sleep led = Pin(2, Pin.OUT) while True: led.value(not led.value()) sleep(0.5)
Uploading the Script
1) Go to File > Save as… or click on the save icon. The following window will open.
2) Select MicroPython device.
3) Name the file main.py, otherwise, it won’t work. Notice that the window that pops up shows the files currently saved on the board filesystem. There should be a file called boot.py. That file is created by default when you burn MicroPython firmware.
4) Finally, click Ok to proceed.
5) Now, press the on-board RST/EN button so that the board restarts and starts running the code.
6) Now, your board should be blinking the blue on-board LED every 500 milliseconds.
The following figure shows the ESP32 on-board LED light up.
And the following figure shows the ESP8266.
Troubleshooting Tips for Thonny IDE
We’ve discovered some common problems and error messages that occur with Thonny IDE. Usually restarting your ESP with the on-board EN/RST button fixes your problem. Or press the Thonny IDE “Stop/Restart backend” button and repeat your desired action. In case it doesn’t work for you, read these next common errors and discover how to solve them.
Error #1: You get one of the following messages:
========================= RESTART ========================= Unable to connect to COM4 Error: could not open port 'COM4': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2) Check the configuration, select Run → Stop/Restart or press Ctrl+F2 to try again. (On some occasions it helps to wait before trying again.)
========================= RESTART ========================= Could not connect to REPL. Make sure your device has suitable firmware and is not in bootloader mode! Disconnecting.
========================= RESTART ========================= Lost connection to the device (EOF).
Unplug, and plug back your ESP board. Then, double-check that you’ve selected the right serial port in the Tools > Options > Interpreter > Port. Then, click the “Stop/Restart backend” button to establish a serial communication. You should now be able to upload a new script or re-run new code.
These errors might also mean that you have your serial port being used in another program (like a serial terminal or in the Arduino IDE). Double-check that you’ve closed all the programs that might be establishing a serial communication with your ESP board. Then, unplug and plug back your ESP board. Finally, restart Thonny IDE.
Error #2: Thonny IDE fails to respond or gives an Internal Error:
When this happens, you can usually close that window and it will continue to work. If it keeps crashing, we recommend restarting the Thonny IDE software.
Error #3: Thonny IDE hangs when pressing “Stop/Restart backend” button:
When you press the “Stop/Restart backend” button, you need to wait a few seconds. The ESP needs time to restart and establish the serial communication with Thonny IDE. If you press the “Stop” button multiple times or if you press the button very quickly, the ESP will not have enough time to restart properly and it’s very likely to crash Thonny IDE.
Error #4: Problem restarting your ESP board, running a new script, or opening the serial port:
Brownout detector was triggered
Or if the ESP keeps restarting and printing the ESP boot information:
ets Jun 8 2016 00:22:57 rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0018,len:4 load:0x3fff001c,len:4732 load:0x40078000,len:7496 load:0x40080400,len:5512
The “Brownout detector was triggered” error message or constant reboots means that there’s some sort of hardware problem. It’s often related to one of the following issues:
- Poor quality USB cable;
- USB cable is too long;
- Board with some defect (bad solder joints);
- Bad computer USB port;
- Or not enough power provided by the computer USB port.
Solution: try a different shorter USB cable (with data wires), try a different computer USB port or use a USB hub with an external power supply.
Important: if you keep having constant problems or weird error messages, we recommend re-flashing your ESP board with the latest version of MicroPython firmware.
Error #5: When I try to establish a serial communication with the ESP32/ESP8266 in Thonny IDE, I can’t get it to connect.
We think this is what’s happening: when you’re running a script in your board, sometimes it’s busy running that script and performing the tasks.
So, you need to try start the connection by pressing “Stop/Restart backend” button multiple times or restart the ESP to catch available to establish the serial communication.
Warning: don’t press the “Stop/Restart backend” button multiple times very quickly. After pressing that button, you need to be patient and wait a few seconds for the command to run.
If you’re running a script that uses Wi-Fi, deep sleep, or it’s doing multiple tasks, I recommend trying 3 or 4 times to establish the communication. If you can’t, I recommend re-flash the ESP with MicroPython firmware.
Error #6: debug tools are grayed out:
Thonny IDE debug tools aren’t available for MicroPython. The debug tools are only available for the Python Interpreter, so being grayed out is the expected behavior.
Thonny IDE is a great IDE to program the ESP32 and ESP8266 boards using MicroPython. It is compatible with Windows, Mac OS X, and Linux and it is easy to install.
We have an article with a compilation of several MicroPython IDEs compatible with the ESP32 and ESP8266 boards that you can check below:
More tutorials about MicroPython with ESP32 and ESP8266:
- MicroPython Programming Basics with ESP32 and ESP8266
- MicroPython with ESP32 and ESP8266: Interacting with GPIOs
- ESP32/ESP8266 MicroPython Web Server
Learn more about MicroPython with our eBook:
Thanks for reading.