ESP32: MicroPython support

The objective of this post is to explain how to flash the MicroPython firmware to the ESP32 and test it with a simple “Hello World” program.


Introduction

The objective of this post is to explain how to flash the MicroPython firmware to the ESP32 and test it with a simple “Hello World” program.

MicroPython is a implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimized to run on microcontrollers [1]. Fortunately, as there was a port of MicroPython for the ESP8266, there is also one for the ESP32, as can be seen here.

Although we can build the project ourselves (instructions here), the easiest way to start playing with MicroPython is downloading the pre-built firmware and upload it to the board. This will be the approach followed on this tutorial.

This tutorial was made on Windows. The ESP32 board used was this one.


Software needed

In order to follow this tutorial, we need an installation of Python on our machine. This tutorial was created using Python 2.7, but it should work for higher versions.

We will also need a software called esptool to flash our ESP32 with the MicroPython binary. At the time of writing, the stable version of the tool is in 1.x, which doesn’t support the ESP32 yet [2]. So, we can’t install it via pip or other Python package managers, and we will need to do it manually.

So, we will have to download the latest esptool release from here. The version used in this tutorial was v2.0beta2.

After downloading the esptool source code, unzip it to a folder. You should have a setup.py file there. Assuming that Python is already installed, navigate to the unzipped folder and enter the following command:

python setup.py install

This should install the esptool on your system. If you prefer, you can check the manual installation instructions from the repository of the tool.

Finally, we will needed the binary file with the MicroPython software. You can get it from here. Download it and save it on a folder, since we will need it latter.

 


The procedure

First, we will need to erase the flash of the ESP32. To do so, just give the command bellow on the command line. You will need to change the “COM5” by the serial port where your ESP32 is connected.

esptool.py --port COM5 erase_flash

After this, we will need to upload the binary to the ESP32 with the command bellow. Again, you will need to change COM5 with your ESP32 COM port. Also, you will need to change the pathToYourFile/BinaryFile with the path where your binary is and the name of the file. On windows, an easy trick is dragging and dropping the file on the command line.

esptool.py --port COM5 --baud 460800 write_flash --flash_size=detect 0 pathToYourFile/binaryFile

After this, you should have MicroPython enabled on the ESP32 board. We will confirm it in a minute.


Testing the installation

To test the installation, we will need an additional tool to interact with the Python prompt. Although we can use other software that allows to establish serial connections, we will use Putty for this tutorial. You can download it here.

So, after downloading the .exe file from the previous link, just open it and the Putty GUI should popup. There, on the connection type, choose “Serial”, on the Serial line put the serial port of the ESP32 (in my case, COM5) and put a speed of 115200. Check figure 1 where these configurations are highlighted.

Putty connect to ESP32

Figure 1 – Connecting to the ESP32 MicroPython prompt with Putty.

After specifying all the configurations, hit open. If everything was correctly configured, you should now get a window like the one in figure 2, with the MicroPython prompt available.

ESP32 MicroPython prompt

Figure 2 – ESP32 MicroPython prompt.


Hello World program

To finish this tutorial, we will print a simple “Hello World” message. So, write the following command and hit enter:

print ("Hello World From ESP32 MicroPython")

You should get something similar to figure 3. In that example, I’ve sent the command twice.

ESP32 MicroPython Hello World

Figure 3 – ESP32 MicroPython “Hello World”.

You can now start playing with Python on the ESP32.


Final notes

I would like to leave a special thanks to the team behind the ESP32 MicroPython port, which promptly made the binaries available after a request I’ve made on the Github page. Also, thanks for making this awesome tool available for everyone!


References

[1] https://micropython.org/

[2] https://github.com/espressif/esptool

Advertisements

25 Replies to “ESP32: MicroPython support”

  1. thanks for the tutorial!

    do you know how to enable webrepl on esp32 with micropython installed this way?

    when i try to run “import webrepl_setup” via the com terminal i’m getting “”no module named ‘webrepl_setup'”

    Liked by 1 person

    1. That’s weird, they removed the binaries indeed. I will try to check with MicroPython’s team, but probably it is something temporary.

      I’ve reached to them on the following GitHub issue:
      https://github.com/micropython/micropython-esp32/issues/91

      If I have some feedback, I will share here. Nevertheless, if you do not want to wait, I can share the binary I’ve originally downloaded from there (I think I still have it somewhere in my PC). It is a month old, but it’s the version I’ve been using in tutorials.

      Like

  2. Quick comment if anyone runs into the following error.

    After flashing the binary file as described above, I got the following error repeating into infinity on PuTTy:
    rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    flash read err, 1000
    Falling back to built-in command interpreter.
    OK

    It appeared to be resolved by changing the flashing command to:
    esptool.py –port COM5 –baud 460800 write_flash –flash_size=detect 0x1000 pathToYourFile/binaryFile

    Liked by 1 person

  3. hello,
    i have tried to install the esptool (i have tried it with version 2.1, 2.3 and 2.5) on my pc as esplained in the tutorial. I navigated to the folder and wrote the command “python setup.py install”.

    It seems like the code is running and it s trying to install the tool but at the end of the installation an error occurs. Error Message:
    error: Could not find suitable distribution for Requirement.parse(‘ecdsa’)

    Does someone know what the problem is? I would appreciate any suggestions.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s