Software on the host
ESPTools and Adafruit AMPY are required. Both can be installed using pip3. The recommended version of Python on the host is Python 3.7
pip3 install esptool --upgrade pip3 install adafruit-ampy --upgrade
Firmware for the ESP32 module
MicroPython for ESP32. The latests firmware (compatible with ESP) when writing can be found at https://micropython.org/resources/firmware/esp32-20220117-v1.18.bin
First of all erase your ESP32 completely by running the following command. Where /dev/ttyUSB0 is the Virtual COM port created by your board.
$ esptool.py --chip esp32 -p /dev/ttyUSB0 erase_flash esptool.py v2.8 Serial port /dev/ttyUSB0 Connecting.... Chip is ESP32D0WDQ6 (revision 1) Features: WiFi, BT, Dual Core, Coding Scheme None Crystal is 40MHz MAC: 24:0a:c4:0c:94:78 Uploading stub... Running stub... Stub running... Erasing flash (this may take a while)... Chip erase completed successfully in 1.8s Hard resetting via RTS pin...
Then, write the flash with the download image (esp32-idf4-20191220-v1.12.bin is this example).
$ esptool.py --chip esp32 -p /dev/ttyUSB0 write_flash -z 0x1000 esp32-idf4-20191220-v1.12.bin esptool.py v2.8 Serial port /dev/ttyUSB0 Connecting.... Chip is ESP32D0WDQ6 (revision 1) Features: WiFi, BT, Dual Core, Coding Scheme None Crystal is 40MHz MAC: 24:0a:c4:0c:94:78 Uploading stub... Running stub... Stub running... Configuring flash size... Auto-detected Flash size: 4MB Compressed 1408512 bytes to 894711... Wrote 1408512 bytes (894711 compressed) at 0x00001000 in 78.9 seconds (effective 142.9 kbit/s)... Hash of data verified. Leaving... Hard resetting via RTS pin...
After successfully flashing your ESP32 module, connect it to a Serial Monitor at 115200 baud. I like to use the embedded serial monitor that Arduino IDE has.
Perform a hard reset and you should see a console output like the one below:
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) flash read err, 1000 Falling back to built-in command interpreter. OK >ets Jun 8 2016 00:22:57 rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0018,len:4 load:0x3fff001c,len:4988 load:0x40078000,len:10404 load:0x40080400,len:5680 entry 0x400806bc [0;32mI (519) cpu_start: Pro cpu up. [0m [0;32mI (519) cpu_start: Application information: [0m [0;32mI (519) cpu_start: Compile time: Dec 20 2019 07:56:38 [0m [0;32mI (523) cpu_start: ELF file SHA256: 0000000000000000... [0m [0;32mI (529) cpu_start: ESP-IDF: v4.0-beta1 [0m [0;32mI (534) cpu_start: Starting app cpu, entry point is 0x40083014 [0m [0;32mI (0) cpu_start: App cpu up. [0m [0;32mI (544) heap_init: Initializing. RAM available for dynamic allocation: [0m [0;32mI (551) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM [0m [0;32mI (557) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAM [0m [0;32mI (563) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM [0m [0;32mI (569) heap_init: At 3FFBDB5C len 00000004 (0 KiB): DRAM [0m [0;32mI (575) heap_init: At 3FFCC8A0 len 00013760 (77 KiB): DRAM [0m [0;32mI (582) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM [0m [0;32mI (588) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM [0m [0;32mI (594) heap_init: At 40099FB8 len 00006048 (24 KiB): IRAM [0m [0;32mI (601) cpu_start: Pro cpu start user code [0m [0;32mI (619) spi_flash: detected chip: generic [0m [0;32mI (619) spi_flash: flash io: dio [0m [0;32mI (620) cpu_start: Chip Revision: 1 [0m [0;33mW (621) cpu_start: Chip revision is higher than the one configured in menuconfig. Suggest to upgrade it. [0m [0;32mI (632) cpu_start: Starting scheduler on PRO CPU. [0m [0;32mI (0) cpu_start: Starting scheduler on APP CPU. [0m MicroPython v1.12 on 2019-12-20; ESP32 module with ESP32 Type "help()" for more information. >>>
Integrated Development Environment
Any Python IDE may be useful for programming in MicroPython (or CircuitPython if you are using an Adafruit compatible board). I personally recommend using Mu (https://codewith.mu/). Mu is programmed in Python, so it works on Windows, Linux and macOS. It also has a portable (pen drive friendly version) which it makes it ideal for students that uses university computers.
Testing the module can be done using the Python REPL that be have already flashed. For example, importing the machine package can give us access to getting and settings properties of our board. For example, the Huzzah32 (ESP32) board runs at 240MHz, but when flashing MicroPython the clock is set at 160Mhz. That can be check running:
MicroPython v1.12 on 2019-12-20; ESP32 module with ESP32 Type "help()" for more information. >>> import machine >>> machine.freq() 160000000 >>>
Increasing the CPU frequency back to 240MHz can be done calling the same function as shown below:
>>> machine.freq(240000000) # set frequency back to 240MHz I (1604452) pm_esp32: Frequency switching config: CPU_MAX: 240, APB_MAX: 240, APB_MIN: 240, Light sleep: DISABLED >>> machine.freq() 240000000 >>>