DEV Community

Tomoyuki KOYAMA
Tomoyuki KOYAMA

Posted on

MicroPython setup on ESP32 with macOS (M1 Mac)

Hardware devices

  • ESP32
    • ESP32-WROOM-32D
  • MacBook Air 2020
    • OS: macOS 12.0.1
    • CPU: Apple M1
    • RAM: 16GB
  • USB Type-C → USB Type-A Adapter
    • DST-C02WH

Hardware Environment

Setup guides

(1) Install esptool

Check pip version

$ pip --version
pip 21.3.1 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
Enter fullscreen mode Exit fullscreen mode

Install esptool

$ pip install esptool
Enter fullscreen mode Exit fullscreen mode

Check esptool installation

$ which esptool.py
/opt/homebrew/bin/esptool.py
Enter fullscreen mode Exit fullscreen mode

(2) Discover ESP32

esptool automatically tries to connect to the serial port from "v2.4".

$ esptool.py flash_id
esptool.py v3.2
Found 3 serial ports
Serial port /dev/cu.wlan-debug
/dev/cu.wlan-debug failed to connect: [Errno 16] could not open port /dev/cu.wlan-debug: [Errno 16] Resource busy: '/dev/cu.wlan-debug'
Serial port /dev/cu.usbserial-0001
Connecting..............
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting........................
Detecting chip type... ESP32
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 4c:eb:d6:75:49:9c
Uploading stub...
Running stub...
Stub running...
Manufacturer: 5e
Device: 4016
Detected flash size: 4MB
Hard resetting via RTS pin...
Enter fullscreen mode Exit fullscreen mode

(3) Connect to ESP32 with serial port

You can connect to ESP32 with serial port.
The destination tty device is displayed on the command result esptool.py flash_id.
For example, the destination tty device is /dev/cu.usbserial-0001 in the above result.
The baudrate is set 115200

screen /dev/cu.usbserial-0001 115200
Enter fullscreen mode Exit fullscreen mode

Screenshot for screen command

(4) Erase flash memory data in ESP32

$ esptool.py --chip esp32 --port /dev/cu.usbserial-0001 erase_flash
esptool.py v3.2
Serial port /dev/cu.usbserial-0001
Connecting..........
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 4c:eb:d6:75:49:9c
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 12.8s
Hard resetting via RTS pin...
Enter fullscreen mode Exit fullscreen mode

(5) Write firmware to ESP32

Download firmware and write the firmware to ESP32

https://micropython.org/download/esp32/

Download IDF 3.x that supports BLE and Wi-Fi

Screenshot for firmware download

Write firmware as below

$ esptool.py --chip esp32 --port /dev/cu.usbserial-0001 --baud 460800 write_flash -z 0x1000 ~/Downloads/esp32-idf3-20210202-v1.14.bin
esptool.py v3.2
Serial port /dev/cu.usbserial-0001
Connecting..............
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 4c:eb:d6:75:49:9c
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00161fff...
Compressed 1445632 bytes to 925476...
Wrote 1445632 bytes (925476 compressed) at 0x00001000 in 22.8 seconds (effective 507.9 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
Enter fullscreen mode Exit fullscreen mode

(6) Connect to ESP32 with serial connection

$ screen /dev/cu.usbserial-0001 115200
Enter fullscreen mode Exit fullscreen mode

Screenshot ESP32 console

When you want to exit the console, type this keycode: Ctrl + ACtrl + K .

Reference

MacでESP32のLチカ(MicroPython/ターミナル版) - Qiita

Top comments (0)