installing micropython on an esp32


I’ve had a couple of ESP32 dev boards knocking around for years now but thought I’d pick one up in lockdown as a fun little project to see if I can make something cool ahead of COVID permitting EMF in 2021.

This afternoon I sat down to install Micropython on the board . This isn’t the first time I’ve done this and probably won’t be the last so I thought I’d write up how to do it.

These steps were taken on Ubuntu 20.04 as I tend to find doing microcontroller things is easier on Linux than on MacOS or Windows.

First things find out which serial port your dev board is connected to by running ls /dev/ttyUSB* a couple of times while plugging and unplugging the board to see which serial port appears and disappears. In my case it was /dev/ttyUBS3 but it will vary depending on your machine.

Once you’ve identified the port the board is connected to you need to flash it with Micropython. I did this with esptool which you can install with

$ pip install esptool

To flash the board you’ll first need to erase it’s flash, hold down the boot button on the board and run

$ esptool.py --chip esp32 --port /dev/ttyUSB3 erase_flash

Replacing the /dev/ttyUSB3 value with whichever port your esp32 board is connected to.

If you get permissions errors on ubuntu you may need to add you user to the tty and dialout groups sudo usermod -a -G tty,dialout $USERNAME.

Then download the latest stable version of Micropython for the esp32, this can be found on the Micropython website here. and holding the boot button again run

$ esptool.py --chip esp32 --port /dev/ttyUSB3 --baud 460800 write_flash -z 0x1000 esp32-20190125-v1.10.bin

Again replace the --port value with the relevant one for you and change esp32-20190125-v1.10.bin to the path where you downloaded the latest stable version of Micropython.

Congratulations should everything have worked smoothly you have flashed micropython to your board!

Micropython has an interactive python shell you can access over serial. You can connect to it on Linux using Picocom. Install Picocom with

$ sudo apt install picocom

And connect to the board using

$ Picocom /dev/ttyUSB3 -b115200

The REPL is fun but what we all really want is to be able to upload code from our dev machines and run it. For that the best tool I’ve found is ampy. Install it with pip

$ pip install adafruit-ampy

And then run code with

$ Ampy --port /dev/ttyUSB3 run $FILE

Replacing $FILE with your micropython script.

On the board I bought there is an onboard LED attached to pin 2 so I was able to run the following script to get it to blink.

import machine
import time

pin = machine.Pin(2.machine.PIN.OUT)
i = 0

while i < 5:
    pin.on()
    time.sleep(0.5)
    pin.off()
    i += 1

With that you should be ready to go build whatever nightmare wifi enabled creations you’ve been dreaming up. Enjoy!


Comments

comments powered by Disqus