Here are notes on configuring & running micropython on an ESP32 device. I use the built in LED on my ESP32 Devkit v1 board to do a blink test.
Installing Micropython on the ESP32
Following this random nerd tutorial I installed esptool python library. First I setup a new virtual environment and activated it.
python -m venv ~/.virtualenvs/esptool
source ~/.virtualenvs/esptool/bin/activate
Then updated pip and installed esptool with pip
pip install --upgrade pip
pip install esptool
Now I can run esptool.py directly from the command line
(esptool) FlyerII:~ ben$ which esptool.py
/Users/ben/.virtualenvs/esptool/bin/esptool.py
Getting the Serial Port of the ESP32
Plugging in the ESP32 dev board to my computer creates a serial port that can be found in /dev. It can be tricky to know which port is for the device you just plugged in so I typically use this trick. With the device unplugged
ls /dev/tty* > dev.txt
Then plug in the device and run this
ls /dev/tty* > dev2.txt && diff dev.txt dev2.txt
And out pops the name of the serial port for the device that was just plugged in
4a5
> /dev/tty.usbserial-0001
Clear the ESP32 Memory
The micropython ESP32 page recommends clearing the flash memory when installing for the first time.
esptool.py --chip esp32 --port /dev/tty.usbserial-0001 erase_flash
Here is the successful output from erasing the flash
esptool.py v3.1
Serial port /dev/tty.usbserial-0001
Connecting...
Failed to get PID of a device on /dev/tty.usbserial-0001, using standard reset sequence.
....
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 84:cc:a8:2d:ff:8c
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 8.5s
Hard resetting via RTS pin...
If you get an error like the following, then try holding the BOOT button down while erasing or add a capacitor between the EN pin and ground.
esptool.py v3.1
Serial port /dev/tty.usbserial-0001
Connecting...
Failed to get PID of a device on /dev/tty.usbserial-0001, using standard reset sequence.
....._____....._____....._____....._____....._____....._____....._____
A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header
Flash the Firmware
I am installing the latest firmware from the micropython ESP32 page which currently is esp32-20210623-v1.16.bin. I saved the file to ~/proj/micropython/. Then I ran this to flash it.
esptool.py --chip esp32 --port /dev/tty.usbserial-0001 --baud 460800 write_flash -z 0x1000 proj/micropython/esp32-20210623-v1.16.bin
Here is the successful output of writing micropython to the flash.
(esptool) FlyerII:~ ben$
esptool.py v3.1
Serial port /dev/tty.usbserial-0001
Connecting...
Failed to get PID of a device on /dev/tty.usbserial-0001, using standard reset sequence.
.
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 84:cc:a8:2d:ff:8c
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00171fff...
Compressed 1510544 bytes to 976957...
Writing at 0x000886ca... (31 %)
Wrote 1510544 bytes (976957 compressed) at 0x00001000 in 24.2 seconds (effective 499.1 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
Running REPL with GNU Screen
The Micropython Quick Reference page has a lot of resources for getting started with ESP32 Micropython. I was able to run the REPL on the ESP32 through the serial port using GNU Screen using 115200 for baud rate.
screen /dev/tty.usbserial-0001 115200
and here is a quick test
x = 32
print("Hello from ESP{}".format(x))
Which outputs the following
Hello from ESP32
Blink
The ESP32 DevKit v1 module has a blue LED tied to pin 2. I was able to do a better REPL test and blink it with this.
import time
from machine import Pin
p2 = Pin(2, Pin.OUT)
for i in range(10):
p2.on()
time.sleep_ms(200)
p2.off()
time.sleep_ms(200)