Flashing MicroPython Firmware onto an ESP32


Categories: , , ,

  • ToDO: Add header image with board and micropython logo

This is a little diversion from Rust on the STM32. My nephew has been doing some Python at school, and also had a project in mind for an ESP8266/ESP32. I had a couple of spares, so it’s time to flash MicroPython onto them and have a play.

It is worth noting that using microPython on a board like the ESP32 is not a well polished experience. It may be worth going for a more expensive board that was built with microPython in mind, or going for the even more polished CircuitPython.

Setting up your host PC

  • Essential: Install Python 3.x 64bit Windows - you’re host PC uses various Python tools to flash microPython to the board, upload your code, and perform other tasks
  • Recommended: Install Putty. You need a way to communicate with serial ports. Putty is one way to do it, and is commonly used for a variety of tasks, so it is worth getting used to its quirky UI. Use the 64bit MSI (“Windows Installer”)
  • Recommended: Install Visual Studio Code Because you don’t want to be writing code in the REPL, or Notepad!
  • Recommended Install Windows Terminal. Because it is a lot more user friendly than the 30 year old Command Prompt.
  • Optional: Python Virtual Environments are very useful, as they allow you to create independent environments for different projects, each with their own sets of packages. Read about Virtual Environments here. I’ll be using venv in this tutorial.

Flash MicroPython onto the ESP32

  • Download the ESP32 firmware. I used the latest stable one based on ESP-IDF v3.x, and save or copy it to your working folder.
    • Note: by using the stable version of MicroPython, we don’t need the extra step of “disabling debug output”
  • Find the COM port of your ESP32
    • Open Device Manager (right click the Windows Logo and choose Device Manager)
    • Expand Ports (COM & LPT)
    • Plug in you ESP32. You should see a new COM port appear, and then disappear when you remove it. Make a note of the COM port, e.g. COM3 Finding the ESP32's COM port in Device Manager
  • Erase the deviceh, then upload new firmware (change COM port number as appropriate)
    • Hold down the Boot button on the ESP32 board as you hit Enter on the following command, and keep it pressed until you see something useful happening. This may or may not be needed for the next step as well.
esptool.py --port COM3 erase_flash
esptool.py --chip esp32 --port COM3 write_flash -z 0x1000 esp32-idf3-20191220-v1.12.bin
  • If the above commands run without error then MicroPython should be installed on your board!

Reference (and additonal info if things go wrong): Getting started with MicroPython on the ESP32

Using Putty to Connect to the REPL Prompt

After initial setup, the only way to get a REPL prompt is over a serial port. This is where Putty comes in. Putty can be used for various types of connection to remote devices and servers, but we are just interested in the simple case of a Serial connection.

  1. Select the Serial radio button
  2. Change Serial line to that used by your ESP32 board. Mine was COM3
  3. Change the Speed (also known as baud rate) to 115200
  4. Optional: You can give these setting a name (in Saved Sessions), and Save it for future use

Creating a Putty serial connection

Once connected you should get a REPL prompt in your Putty session.

You can mess around in REPL getting WiFi to work, and this is good for testing stuff out, but you’re going to want those WiFi settings to run when the board starts up, so for this you need to write a boot.py and be able to upload it.

The tutorial at docs.micropython.org are excellent. From this point I would skip straight to tutorial/network_basics to learn how to do stuff with Wifi. They do everything in REPL, but if you want something to happen at every boot of the device, you should add it to boot.py

NEXT: Setting up your dev environment and writing some code

Other References