2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

M5Stamp C3U をMicroPythonでLチカしてみた

Last updated at Posted at 2022-01-31

スイッチサイエンスで販売している、M5Stack社のM5Stamp C3UにMicroPython(v1.18)を入れて、内蔵LEDを点灯(Lチカ)させた状況を紹介します。
 (ファームウェアは、MicroPythonホームページから.binファイルをダウンロードして使用した)

環境

Windows 10 (21H2)
M5Stamp C3U Mate

ファームウェアのダウンロード

MicroPythonホームページ(https://micropython.org/ )のDOWNLOADから'ESP32-C3 with USB'のページに入り、Firmwareをダウンロードする。
 (私の場合'esp32c3-usb-20220117-v1.18.bin'をダウンロードした)

ファームウェアの書込み

ダウンロードページにも記載がありますが、esptool.pyを使用して書き込みを行います。
 (esptoolが入ってない場合は、別途インストールが必要です)

WindowsパソコンとM5Stamp C3UをUSBケーブルで接続する時、M5Stamp C3Uのプログラマブル物理ボタンを押しながら接続するとダウンロードモードで起動し、Windows側でCOMデバイスとして認識される。

・チップの情報取得(flash_id)
    COMx : COMデバイス名(COM4等に置き換える)

esptool.py.exe --chip esp32c3 --port COMx -b 460800 flash_id

・フラッシュの削除(erase_flash)

esptool.py.exe --chip esp32c3 --port COMx -b 460800 erase_flash

・フラッシュの書き込み(write_flash)

esptool.py --chip esp32c3 --port COMx --baud 460800 write_flash -z 0x0 esp32c3-usb-20220117-v1.18.bin

・書き込み確認
Tera Term等でCOMxに接続、シリアルポートのスピードを460800に設定して、M5Stamp C3Uのリセットボタンを押すと、MicroPythonの起動メッセージが表示される。

MicroPython v1.18 on 2022-01-17; ESP32C3 module with ESP32C3
Type "help()" for more information.
>>>

Lチカ(その1 ← 失敗)

ATOM EchoでLチカしたコードのGPIOピンを変更して実行すると、エラーが出てLED点灯しない。

from machine import Pin
from neopixel import NeoPixel
pin = Pin(2, Pin.OUT)
np = NeoPixel(pin, 1)
np[0] = (128, 0, 0)
np.write()

Tera Term画面上に、以下のエラー表示がされる。

E (33842) rmt: rmt_set_gpio(530): RMT CHANNEL ERR
E (33842) rmt: rmt_config(686): set gpio for RMT driver failed
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "neopixel.py", line 1, in write
OSError: (-258, 'ESP_ERR_INVALID_ARG')

Lチカ(その2 ← 成功)

MicroPythonドキュメントページでRMTチャンネルについての記載を確認し、1行目と4行目を追加したら、カラーLEDが赤色に点灯できた。

import esp32
from machine import Pin
from neopixel import NeoPixel
esp32.RMT.bitstream_channel(0)
pin = Pin(2, Pin.OUT)
np = NeoPixel(pin, 1)
np[0] = (128, 0, 0)
np.write()

※考察:bitstream_channel()の初期値は3になっていたが、ESP32-C3のRMTは2チャンネルなので 'RMT CHANNEL ERR'が発生したのか?

Lチカ(その3)

赤色に点灯だけでは、さみしいので色が変化するサンプルも作ってみた。

main.py
import time
import esp32
from machine import Pin
from neopixel import NeoPixel

def main():
    esp32.RMT.bitstream_channel(0)    # RMTチャネルを設定
    pin = Pin(2, Pin.OUT)    # NeoPixel 駆動のための GPIO 2 を出力に設定
    np = NeoPixel(pin, 1)    # 1ピクセル用の NeoPixel ドライバーを GPIO 2 で作成

    # RGB各値の合計が255になるように
    for n in range(0, 15):
        for cnt_r in range(0, 15):
            for cnt_g in range(0, 15 - cnt_r):
                cnt_b = 15 - cnt_r - cnt_g
                np[0] = (cnt_r * 16, cnt_g * 16, cnt_b * 16)
                np.write()
                time.sleep(0.1)
        time.sleep(1)

    np[0] = (0, 0, 0)       # 第1ピクセルを黒に設定
    np.write()              # 全ピクセルにデータ書込み

if __name__ == '__main__':
    main()

今後

M5Stamp C3Uに温度センサーを繋いで、連続的に温度変化を記録できるようにしたいと思っています。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?