ESP32-C3へのmicropythonインストール手順

■基本
・esptoolを使う。
・Windowsがわかりやすい。

■出典
出典1:Seeed Studio XIAOESP32C3とMicroPython
https://wiki.seeedstudio.com/ja/xiao_esp32c3_with_micropython/
 

出典2:esptoolによるESP32のMicroPythonファームウェアの書き込み

https://www.microfan.jp/2023/04/esp32-esptool-mycropython/



■手順(以下は出典1および出典2からの抜粋)
 

1:esptoolの準備(出典1から)
・先にPowerShellでpip自体をアップデートしておく。
   python -m pip install -U pip
・esptoolの有無、有ればそのバージョンを確認
   pip list
・新バージョン有無確認
   pip list --outdated
・esptoolの新バージョンがあれば更新
   pip install -U esptool

●出典1の方法ではちゃんと動かせなかった。

(pathが関係するのだろうが、わずらわしい)


1:esptoolの準備(出典2から)


2:ファームウェアの準備(出典1、2とも同じ)
・micropython.org(公式)ページを開く
   https://micropython.org/download/ESP32_GENERIC_C3/
・ファームウェアダウンロード
   バイナリファイル(.bin)をダウンロードする。
   ファイル名の例:ESP32_GENERIC_C3-20260406-v1.28.0
   ファイル(.bin)がダウンロードフォルダに入る。
   ファイルをデスクトップなど別フォルダへ移動し、

   そのフォルダの中でターミナルを開く。
・ESP32-C3接続
   WindowsPCにUSBケーブル(tyoeC)でESP32-C3を接続する。
・シリアルポート確認
   デバイスマネジャーの「ポート(COMとLPT)」にて

   シリアルポート「COM●」を確認する。

   今回はCOM3となった。


   または、ターミナルで「mode」にて一覧表示される。
・ESP32-C3のフラッシュをイレーズ
   esptool.py --chip esp32c3 --port COM● erase_flash
・ファームウェアを書き込む(フラッシング)
   esptool.py --chip esp32c3 --port COM● --baud 460800 write_flash -z 0x0 ESP32_GENERIC_C3-20260406-v1.28.0
・Thonnyで開発開始


・出典1の手順は煩わしい。どのみちThonnyを使うから、
 出典2の「5 ThonnyのPython利用」が一番簡単でわかりやすい。

 

以下、実際の記録(出典2のとおりにやったらできた)

 

Thonnyにおいて、インタープリータをPC本体のPython3に切り替え、

Thonnyのシステムシェルを開いて、下記のように進める。

【前提】

・Thonnyは下記で動作している。(Thonnyのシステムシェルで表示される)

   C:\Users\***\デスクトップ\マイコン開発\PICO\MicroPython

・ファームウェアのbinファイルはデスクトップに置いてある。

 

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

①esptoolのインストール

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

C:\Users\***\デスクトップ\マイコン開発\PICO\MicroPython>pip install esptool

Defaulting to user installation because normal site-packages is not writeable

Requirement already satisfied: esptool in c:\program files (x86)\thonny\lib\site-packages (4.7.0)

Requirement already satisfied: cryptography>=2.1.4 in c:\program files (x86)\thonny\lib\site-packages (from esptool) (43.0.1)

Requirement already satisfied: bitstring>=3.1.6 in c:\program files (x86)\thonny\lib\site-packages (from esptool) (4.2.3)

Requirement already satisfied: PyYAML>=5.1 in c:\program files (x86)\thonny\lib\site-packages (from esptool) (6.0.2)

Requirement already satisfied: intelhex in c:\program files (x86)\thonny\lib\site-packages (from esptool) (2.3.0)

Requirement already satisfied: pyserial>=3.0 in c:\program files (x86)\thonny\lib\site-packages (from esptool) (3.5)

Requirement already satisfied: reedsolo<1.8,>=1.5.3 in c:\program files (x86)\thonny\lib\site-packages (from esptool) (1.7.0)

Requirement already satisfied: ecdsa>=0.16.0 in c:\program files (x86)\thonny\lib\site-packages (from esptool) (0.19.0)

Requirement already satisfied: bitarray<3.0.0,>=2.9.0 in c:\program files (x86)\thonny\lib\site-packages (from bitstring>=3.1.6->esptool) (2.9.2)

Requirement already satisfied: cffi>=1.12 in c:\program files (x86)\thonny\lib\site-packages (from cryptography>=2.1.4->esptool) (1.17.1)

Requirement already satisfied: six>=1.9.0 in c:\program files (x86)\thonny\lib\site-packages (from ecdsa>=0.16.0->esptool) (1.16.0)

Requirement already satisfied: pycparser in c:\program files (x86)\thonny\lib\site-packages (from cffi>=1.12->cryptography>=2.1.4->esptool) (2.22)

 

[notice] A new release of pip is available: 23.0.1 -> 26.1.2

[notice] To update, run: python.exe -m pip install --upgrade pip

 

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

②esptoolの動作確認

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

C:\Users\***\デスクトップ\マイコン開発\PICO\MicroPython>python -m esptool

esptool.py v4.7.0

usage: esptool [-h]

               [--chip {auto,esp8266,esp32,esp32s2,esp32s3beta2,esp32s3,esp32c3,esp32c6beta,esp32h2beta1,esp32h2beta2,esp32c2,esp32c6,esp32h2,esp32p4}]

               [--port PORT] [--baud BAUD] [--before {default_reset,usb_reset,no_reset,no_reset_no_sync}]

               [--after {hard_reset,soft_reset,no_reset,no_reset_stub}] [--no-stub] [--trace]

               [--override-vddsdio [{1.8V,1.9V,OFF}]] [--connect-attempts CONNECT_ATTEMPTS]

               {load_ram,dump_mem,read_mem,write_mem,write_flash,run,image_info,make_image,elf2image,read_mac,chip_id,flash_id,read_flash_status,write_flash_status,read_flash,verify_flash,erase_flash,erase_region,merge_bin,get_security_info,version}

               ...

 

esptool.py v4.7.0 - Espressif chips ROM Bootloader Utility

 

positional arguments:

  {load_ram,dump_mem,read_mem,write_mem,write_flash,run,image_info,make_image,elf2image,read_mac,chip_id,flash_id,read_flash_status,write_flash_status,read_flash,verify_flash,erase_flash,erase_region,merge_bin,get_security_info,version}

                        Run esptool.py {command} -h for additional help

    load_ram            Download an image to RAM and execute

    dump_mem            Dump arbitrary memory to disk

    read_mem            Read arbitrary memory location

    write_mem           Read-modify-write to arbitrary memory location

    write_flash         Write a binary blob to flash

    run                 Run application code in flash

    image_info          Dump headers from a binary file (bootloader or application)

    make_image          Create an application image from binary files

    elf2image           Create an application image from ELF file

    read_mac            Read MAC address from OTP ROM

    chip_id             Read Chip ID from OTP ROM

    flash_id            Read SPI flash manufacturer and device ID

    read_flash_status   Read SPI flash status register

    write_flash_status  Write SPI flash status register

    read_flash          Read SPI flash content

    verify_flash        Verify a binary blob against flash

    erase_flash         Perform Chip Erase on SPI flash

    erase_region        Erase a region of the flash

    merge_bin           Merge multiple raw binary files into a single file for later flashing

    get_security_info   Get some security-related data

    version             Print esptool version

 

options:

  -h, --help            show this help message and exit

  --chip {auto,esp8266,esp32,esp32s2,esp32s3beta2,esp32s3,esp32c3,esp32c6beta,esp32h2beta1,esp32h2beta2,esp32c2,esp32c6,esp32h2,esp32p4}, -c {auto,esp8266,esp32,esp32s2,esp32s3beta2,esp32s3,esp32c3,esp32c6beta,esp32h2beta1,esp32h2beta2,esp32c2,esp32c6,esp32h2,esp32p4}

                        Target chip type

  --port PORT, -p PORT  Serial port device

  --baud BAUD, -b BAUD  Serial port baud rate used when flashing/reading

  --before {default_reset,usb_reset,no_reset,no_reset_no_sync}

                        What to do before connecting to the chip

  --after {hard_reset,soft_reset,no_reset,no_reset_stub}, -a {hard_reset,soft_reset,no_reset,no_reset_stub}

                        What to do after esptool.py is finished

  --no-stub             Disable launching the flasher stub, only talk to ROM bootloader. Some features will not be

                        available.

  --trace, -t           Enable trace-level output of esptool.py interactions.

  --override-vddsdio [{1.8V,1.9V,OFF}]

                        Override ESP32 VDDSDIO internal voltage regulator (use with care)

  --connect-attempts CONNECT_ATTEMPTS

                        Number of attempts to connect, negative or 0 for infinite. Default: 7.

 

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

③esptoolによるESP32C3のID等確認

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

C:\Users\***\デスクトップ\マイコン開発\PICO\MicroPython>python -m esptool flash_id

esptool.py v4.7.0

Found 1 serial ports

Serial port COM3

Connecting....

Detecting chip type... ESP32-C3

Chip is ESP32-C3 (QFN32) (revision v0.4)

Features: WiFi, BLE, Embedded Flash 4MB (XMC)

Crystal is 40MHz

MAC: 58:*********:c8

Uploading stub...

Running stub...

Stub running...

Manufacturer: 20

Device: 4016

Detected flash size: 4MB

Hard resetting via RTS pin...

 

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

④esptoolによるESP32C3のフラッシュメモリ消去

 (シリアルポートは指定しなくてもesptoolが自分で見つけにいってくれた)

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

C:\Users\***\デスクトップ\マイコン開発\PICO\MicroPython>python -m esptool erase_flash

esptool.py v4.7.0

Found 1 serial ports

Serial port COM3

Connecting...

Detecting chip type... ESP32-C3

Chip is ESP32-C3 (QFN32) (revision v0.4)

Features: WiFi, BLE, Embedded Flash 4MB (XMC)

Crystal is 40MHz

MAC: 58:8c:81:a1:0a:c8

Uploading stub...

Running stub...

Stub running...

Erasing flash (this may take a while)...

Chip erase completed successfully in 15.0s

Hard resetting via RTS pin...

 

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

⑤esptoolによるESP32C3へのファーム書き込み

 (ここでもシリアルポートをesptoolが自分で見つけにいってくれた)

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

C:\Users\***\デスクトップ\マイコン開発\PICO\MicroPython>python -m esptool write_flash -z 0 C:/Users/kabay/デスクトップ/ESP32_GENERIC_C3-20260406-v1.28.0.bin

esptool.py v4.7.0

Found 1 serial ports

Serial port COM3

Connecting...

Detecting chip type... ESP32-C3

Chip is ESP32-C3 (QFN32) (revision v0.4)

Features: WiFi, BLE, Embedded Flash 4MB (XMC)

Crystal is 40MHz

MAC: 58:8c:81:a1:0a:c8

Uploading stub...

Running stub...

Stub running...

Configuring flash size...

Flash will be erased from 0x00000000 to 0x001a5fff...

Compressed 1725008 bytes to 1035498...

Wrote 1725008 bytes (1035498 compressed) at 0x00000000 in 12.8 seconds (effective 1081.1 kbit/s)...

Hash of data verified.

 

Leaving...

Hard resetting via RTS pin...

 

C:\Users\***\デスクトップ\マイコン開発\PICO\MicroPython>

 


■XIAO ESP32-C3 ピンアサイン

利用可能なGPIO:2,3,4,5,6,7,8,9,10,20,21
基板の下辺にある3つは、左からBOOTボタン、アンテナ端子、RESETボタン

 


■Network接続初期設定の例
import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True)       # activate the interface
wlan.scan()             # scan for access points
wlan.isconnected()      # check if the station is connected to an AP
wlan.connect('ssid', 'key') # connect to an AP
wlan.config('mac')      # get the interface's MAC address
wlan.ifconfig()  # get the interface's IPv4 addresses

■接続関数の例
def do_connect():
    import network
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('ssid', 'key') #replace the ssid and key
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())

■UART
ESP32C3のハードウェアUARTは1つのみ。
TX:GPI021
RX:GPI020

■SPI
ソフトウェア SPI(ビットバンギング使用)はすべてのピンで動作する。
machine.SoftSPI クラス経由でアクセスされる。


■I2C
1:ソフトウェアI2C(ビットバンギングを使用)
すべての出力可能なピンで動作する。
machine.SoftI2C クラスを介してアクセスされる。
コード例
from machine import Pin, SoftI2C
i2c = SoftI2C(scl=Pin(7), sda=Pin(6), freq=100000)
i2c.scan()              # scan for devices
i2c.readfrom(0x3a, 4)   # read 4 bytes from device with address 0x3a
i2c.writeto(0x3a, '12') # write '12' to device with address 0x3a
buf = bytearray(10)     # create a buffer with 10 bytes
i2c.writeto(0x3a, buf)  # write the given buffer to the peripheral

2:ハードウェアI2C
信号    GPIO    Pin
SCL    GPIO7    D5
SDA    GPIO6    D4

以上