MicroPython

Getting Started MicroPython (Python) with Thonny IDE and ESP32

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.

Graphical user interface, application Description automatically generated

Step 2: Download the Thonny IDE version for windows and wait until downloads complete.

Graphical user interface, text, application, chat or text message Description automatically generated

Step 3: Once the Thonny IDE is downloaded run the .exe installer file. Click install and go with default settings.

Graphical user interface, text Description automatically generated

Step 4: After installation is finished open Thonny IDE. Here we can see two windows Editor and Shell/Terminal.

Graphical user interface, application Description automatically generated

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

Graphical user interface, application, Word Description automatically generated

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.

Graphical user interface, text, application, email Description automatically generated

Step 4: Once the installation is successful type help() in shell/terminal to confirm installation. On successful installation below highlighted message will appear.

Graphical user interface, text, application Description automatically generated

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 machine import Pin

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.

Graphical user interface, text, application Description automatically generated

Step 2: Before we can run the file, we need to save it to the ESP32 board. Go to: File>Save.

Graphical user interface, application Description automatically generated

Step 3: Select MicroPython device.

Graphical user interface, application, PowerPoint Description automatically generated

Step 4: Save the file with name main.py and click OK.

Graphical user interface, application Description automatically generated

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.

Graphical user interface, text, application Description automatically generated

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.

Graphical user interface, text, application Description automatically generated

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.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.