MicroPython setup on ESP32 with macOS (M1 Mac)
Published: 2022-10-09 08:18:08

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqeu4xnqw1xoak8qep3u.png)

## 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)
```

Install esptool

```
$ pip install esptool
```

Check esptool installation

```
$ which esptool.py
/opt/homebrew/bin/esptool.py
```

### (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...
```

### (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
```


![Screenshot for screen command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/igfmfyd3zb9l4nnox93i.png)



### (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...
```

### (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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tdrywj546bn7tuvg9lrb.png)


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...
```

### (6) Connect to ESP32 with serial connection

```
$ screen /dev/cu.usbserial-0001 115200
```

![Screenshot ESP32 console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bdn0411myc5410y6z6d.png)

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

## Reference

[MacでESP32のLチカ（MicroPython/ターミナル版） - Qiita](https://qiita.com/naohiro2g/items/588548c6b1ad8d7545f4)