Introduction
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.
Updated: This tutorial is current and regularly maintained.
We've experimented with several IDEs to program the ESP32 and ESP8266 boards using MicroPython, and Thonny IDE is our preference.
It allows you to program your ESP32, ESP8266, Raspberry Pi Pico, and other boards with MicroPython. It is compatible with Windows, Mac OS X, and Linux. It 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.
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. Follow the section for the operating system you're using.
Tip: Thonny IDE comes installed by default on the Raspberry Pi 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:
- Go to https://thonny.org
- Download the Installer for Windows and wait a few seconds while it downloads.
- Run the .exe file.
- Follow the installation wizard to complete the installation process. You just need to click "Next".
- After completing the installation, open Thonny IDE. A window should open ready for programming.



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.
- Open the "System Preferences..." menu.
- Open the "Security & Privacy" menu.
- At the bottom left corner, click the lock icon to modify your "Security & Privacy" settings.
- Type your username/password and click the "Unlock" button.
- 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:
- Go to https://thonny.org
- Download the version for Mac OS X and wait a few seconds while it downloads.
- Open the .dmg file.
- Drag the "Thonny" application to your Desktop.
- Thonny IDE is now installed and you can double-click to open it.
C) Installing Thonny IDE – Linux
To install Thonny on your Linux computer, it depends on your Linux distribution and version. 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:
bash <(wget -O - https://thonny.org/installer-for-linux)
Or install with pip3:
sudo pip3 install thonny
Fedora since 27:
sudo dnf install thonny
Raspbian since Stretch:
sudo apt install python3-thonny
After installing Thonny IDE, to open it:
- You either need go to the search bar and type "Thonny" to find it
- Or if you installed using pip3, you can type in your terminal:
thonny
Flashing MicroPython Firmware
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. That's the method we'll use in this tutorial.
Note: Flashing your boards with MicroPython is reversible. This means that after you flash the ESP32/ESP8266 with MicroPython, you can still use Arduino IDE in the future. You just need to upload a code using the Arduino IDE to your board.
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:
- Connect your ESP32 or ESP8266 board to your computer.
- Open Thonny IDE. Go to Tools > Options > Interpreter.
- 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 MicroPython.
- Then, select the port once again, the board you're using, and the variant. It will automatically pick up the most recent MicroPython firmware version. Finally, you can click Install.
- After a few seconds, the installation should be completed. If you're using an ESP32, you may need to press the BOOT button for a few seconds after clicking the Install button.
- When the installation is completed, you can close the window. You should get a message on the Shell, and at the bottom right corner, it should have the Interpreter it's using and the COM port.



Troubleshooting: If it doesn't recognize the device, and you have a message saying "no backend" in the bottom-right corner, it may be the case that it is not detecting the COM port automatically. If that's the case, go to Tools > Options > Interpreter and select the COM port manually. After that, it should recognize the MicroPython device.
The REPL
You should also see the >>> symbol (if you don't, click on the Stop icon at the top).
This symbol represents the MicroPython REPL (Read-Eval-Print-Loop) prompt. This symbol means you're interacting with MicroPython (the board with MicroPython firmware installed) in interactive mode in real time. This simply means that you can enter and execute commands directly.
Testing the Installation
Important: Before testing the installation, your ESP32/ESP8266 board needs to be flashed with MicroPython firmware (see the previous step).
Type help() in the Shell after the prompt >>> and see if your device responds.

It should print some useful information about programming your board using MicroPython. You can test other commands. For example, type the following:
>>> print('Hello')

It should print Hello on the Shell right after you enter the command.
You can also run the following commands successively on the Shell after the prompt (>>>) one at a time to turn on the ESP32/ESP8266 built-in LED.
>>> from machine import Pin
>>> led = Pin(2, Pin.OUT)
If you're using an ESP32, turn the onboard LED as follows:
>>> led.value(1)
If you're using an ESP8266, the built-in LED works with inverted logic. So, you turn it on as follows:
>>> led.value(0)

The on-board LED should light up.
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.
- In 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 to the uploading process, syntax errors, prints messages, etc.
Running Your First Script – Blinking an LED
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.
When you open Thonny IDE for the first time, the Editor shows an untitled file. Copy the following script to that file:
from machine import Pin
from time import sleep
led = Pin(2, Pin.OUT)
while True:
led.value(not led.value())
sleep(0.5)

Running the Script
To run that script on your board, simply click on the Run icon in Thonny IDE.
The on-board LED will start blinking.
To stop the execution of the program, you can hit the STOP button or simply press CTRL+C.
Running your Code Automatically
Just running the file with Thonny doesn't copy it permanently to the ESP32 or ESP8266 filesystem. This means that if you unplug it from your computer and apply power to the board, nothing will happen because it doesn't have any MicroPython file saved on its filesystem.
The Thonny IDE Run function is useful to test the code, but if you want to upload it permanently to your board, you need to create and save a file to the board's filesystem.
Uploading Code to the ESP32/ESP8266 Boards
Stop the execution of the previous program by clicking on the Stop button if you haven't already.
- With the code copied to the file, click on the Save icon or go to File > Save as…
- Then, select MicroPython device.
- Name the file
main.py, otherwise, it will not run automatically on the ESP32 or ESP8266. - Finally, click Ok to proceed.

Important: When you name a file main.py, the ESP will run that file automatically on boot (when it starts). If you call it a different name, it will still be saved on the board filesystem, but it will not run automatically on boot.
- Now, press the on-board RST/EN button so that the board restarts and starts running the code.
- Now, your board should be blinking the blue on-board LED every 500 milliseconds.
After saving the file, you can remove and apply power again to the board, or you can even power it using a different power supply that is not your computer.
Notice that the board will automatically start blinking the LED when you apply power to it. This means the main.py file was successfully uploaded to the board and that it is running that file automatically.
Congratulations! You've just written and uploaded your first MicroPython script to your ESP32/ESP8266 board.
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: Unable to connect to COM port
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.
These errors might also mean that you have your serial port being used by 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.
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: Brownout detector was triggered
This 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.
Error #5: Can't establish serial communication
When you're running a script on your board, sometimes it's busy running that script and performing the tasks. So, you need to try starting the connection by pressing "Stop/Restart backend" button multiple times or restarting the ESP to make it 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.
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.
Wrapping Up
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.
This tutorial covered:
- Installing Thonny IDE on different operating systems
- Flashing MicroPython firmware to ESP32/ESP8266
- Testing the installation and running your first script
- Uploading code permanently to your board
- Common troubleshooting tips
With Thonny IDE and MicroPython, you're now ready to start building amazing projects with your ESP32 and ESP8266 boards!
