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 with Raspbian OS for Raspberry Pi. Additionally, it’s easy to install, so you shouldn’t have problems with the installation process.
Note: we’ve also experimented with uPyCraft IDE and it worked fine for us. However, some readers had trouble using uPycraft IDE. So, here we provide another great IDE to program your ESP32/ESP8266 boards using MicroPython.
If you want to use uPyCraft IDE instead, follow one of the installation tutorials below:
- Install uPyCraft IDE – Windows PC Instructions
- Install uPyCraft IDE – Mac OS X Instructions
- Install uPyCraft IDE – Linux (Ubuntu 16.04) Instructions
Downloading and Flashing MicroPython Firmware
Unlike other boards, MicroPython isn’t flashed onto the ESP32 or ESP8266 by default. That’s the first thing you need to do to start programming your boards with MicroPython: flash/upload the firmware.
Follow the next tutorial to flash MicroPython firmware on your board using esptool.py:
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”.
Download our Free eBooks and Resources
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:
Testing the Installation
Important: before testing the installation, your ESP32/ESP8266 board needs to be flashed with MicroPython firmware.
Connect the board to your computer using an 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.
2. Choose MicroPython on a generic device.
3. Then, select your device serial port (recommended).
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.
4. Thonny IDE should now be connected to your board and you should see the prompt on the Shell.
5. 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. Simply, click the save icon and name the file main.py.
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
Go to Device and select Upload current script as main script.
It should write the following on the Shell.
Note: uploading the code as main script will save the current file with the name main.py on the ESP, even if you have saved it in your computer with a different name. The same happens for the boot.py file.
Important: when the ESP restarts, first it runs the boot.py file and afterwards the main.py.
After uploading the script, press the ESP EN/RESET button.
The ESP on-board LED should be blinking.
Congratulations, you’ve successfully uploaded a new script to your ESP32/ESP8266 using Thonny IDE.
List Files Saved on ESP32/ESP8266
To list all files saved on the ESP board, type in the Shell:
Show File Content
To show the file content, use %cat / followed by the file path. For example:
Alternatively, to show the boot.py and main.py scripts, you can also go to Device and select one of the options: Show device’s main script or Show device’s boot script.
To upload a file with a unique name to your ESP using Thonny IDE, follow these next steps:
- Create a new file
- Save it in your computer with the exact name that you want, for example “umqttsimple.py”
- Go to Device > Upload current script with current name
- The file should be saved on the ESP with the name “umqttsimple.py”
- You can list the files stored in your ESP board with the command %lsdevice:
That’s it! The umqttsimple.py file was uploaded to your board. You can see the file content using the %cat /umqttsimple.py command:
Removing/Deleting File from ESP
At the moment, Thonny IDE doesn’t have a command to remove a file from the ESP. So, in order to remove/delete all files completely from your ESP, you need to re-flash it with MicroPython firmware.
Note: you can upload a blank script to the ESP board to remove/delete code.
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 pressing the Thonny IDE “Stop/Restart backend” button and repeating 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: Flash/Upload MicroPython Firmware to ESP32 and ESP8266.
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 easy to install.
We have other tutorials to program the ESP32 and ESP8266 boards with MicroPython using uPyCraft IDE. If you want to experiment with uPyCraft IDE, you can take a look at some of the following tutorials:
- Install uPyCraft IDE (Windows, Mac OS X, Linux Ubuntu 16.0.4)
- Getting Started with MicroPython on ESP32 and ESP8266
- Flash/Upload MicroPython Firmware with uPyCraft IDE to ESP32 and ESP8266
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.