To program an ESP32 board with MicroPython we need an IDE that can burn code to the microcontroller. Here in this guide, we will be covering the complete installation procedure of Thonny IDE and uploading our very first program of LED blinking in ESP32 board using MicroPython.
What is MicroPython
MicroPython is written in C and it’s a software implementation for Python 3 mainly targeting embedded system applications. However, it cannot fully run the python 3 standard libraries. MicroPython is a variant of Python and specifically designed for embedded systems. Difference between MicroPython and Python programming language is that MicroPython works under constraint conditions. That is why MicroPython doesn’t fully support Python standard programming libraries.
Installing Thonny IDE
To download and install Thonny IDE follow the given instructions.
Step 1: Go to thonny.org.
Step 2: Download the Thonny IDE version for windows and wait until downloads complete.
Step 3: Once the Thonny IDE is downloaded run the .exe installer file. Click install and go with default settings.
Step 4: After installation is finished open Thonny IDE. Here we can see two windows Editor and Shell/Terminal.
Editor: Editor contains all the .py files here we can create more than one file.
Shell: MicroPython contains the commands which are executed immediately independent of other files or libraries in the terminal. Shell/Terminal also shows us information about executed commands, board condition, syntax error and messages.
How to install MicroPython Firmware to ESP32
By default, MicroPython is not flashed into the ESP32 board. So, the first thing before starting programming ESP32 boards is to flash/upload the firmware on ESP32.
There are different methods we can use but we will continue with a tool of Thonny IDE that allows you to quickly install MicroPython in an ESP32 board.
Two steps are involved in installing the MicroPython Firmware to ESP32 using Thonny IDE:
1: Download MicroPython Firmware
2: Flashing MicroPython Firmware using Thonny IDE
1: Download MicroPython Firmware
Go to the MicroPython Firmware Download page. Select the board for which we need to download firmware. Following window will appear. Download the latest release for firmware and make sure not to download the night builds version as they more target advanced programmers.
Once download is completed most probably it will be available inside the download folder.
2: Flashing MicroPython Firmware using Thonny IDE
As of now we have downloaded MicroPython firmware. Now we will install it on the ESP32 board using the Thonny IDE tool. Follow the steps given below:
Step 1: Connect ESP32 board with PC and open Thonny IDE. Go to: Tools>Options
Step 2: A new window will open here select Interpreter. Select the board and COM port at which it is connected here in our case ESP32 board is connected at COM10. To check COM port open device manager and look under COM & LPT section.
After selecting the board and COM port click Install or update MicroPython.
Step 3: Now confirm the COM port and select the firmware file we just downloaded by clicking the Browse button. Click install to begin firmware installation in ESP32 board.
Step 4: Once the installation is successful type help() in shell/terminal to confirm installation. On successful installation below highlighted message will appear.
Program ESP32 using MicroPython
Now as installation is completed, we will test the MicroPython code with ESP32 board using Thonny IDE. Follow below steps:
Step 1: Open a new file in Thonny IDE editor use the below given code.
from time import sleep
led = Pin(2, Pin.OUT)
while True:
led.value(not led.value())
sleep(0.5)
This code will blink the built in LED connected to pin 2 of ESP32 with delay of 0.5 sec.
Step 2: Before we can run the file, we need to save it to the ESP32 board. Go to: File>Save.
Step 3: Select MicroPython device.
Step 4: Save the file with name main.py and click OK.
Our first MicroPython file is successfully created now we will upload it to the ESP32 board and see the result.
How to Upload MicroPython Script to ESP32
Click the Run button at top or press F5 to start running the uploaded code in ESP32.
A blue LED will start blinking. The LED will turn ON for 0.5 sec.
The LED will stop for 0.5 sec. This cycle continues until the script is stopped.
To stop the program, click the Stop/Restart button as shown below or press Ctrl+F2.
We have successfully tested the Thonny IDE working with an ESP32 board using the MicroPython code.
Conclusion
MicroPython is software re-implementation of Python 3 programming language. It’s not the same as Python, however it still supports multiple libraries and can control GPIO pins of different microcontrollers easily. To program ESP32 with MicroPython an IDE is required. Here this article covers the Thonny IDE installation with ESP32 board.