プログラミング素人のはてなブログ

プログラミングも電気回路も専門外の技術屋の末端が勉強したことや作品をアウトプットするブログ。コードに間違いなど見つけられたら、気軽にコメントください。 C#、Python3、ラズパイなど。

M5Stackに入門しました

elchikaのハードウェア作品投稿キャンペーンでM5Stack他をもらいました

f:id:s51517765:20210424155448j:plain
elchika.com

M5Stackを使ってみたいな、と思っていたので真っ先にこれを選択。あとはちょっといいテスターが欲しかったのでデジタルマルチメーターと、何にでも使えそうな秋月電子の商品券にしました。

早速M5Stackに入門

raspberrypi.mongonta.com
セットアップはこちらを参考にしました。
ESP32はセットアップ済みなのでボード情報はインストール済みで、デバイスドライバ(CP210X Driver)のインストールとライブラリの追加でOKでした。

デバイスドライバのインストール
shop.m5stack.com
・ボード情報のインストール
f:id:s51517765:20210424155952j:plain
・M5Stackののライブラリのインストール
f:id:s51517765:20210424160042j:plain

あとは、ボードとしてESP32 Arduino ¥ M5Stack-CoreoESP32を選択します。
f:id:s51517765:20210424161405p:plain

動かしてみる

Buttonのサンプルコードにちょっと手を加えたものですが、コンパイルから動くところまでやってみました。


ESP32系?はArduinoよりかなり時間がかかりますね。

まとめ

M5Stackはボタンとディスプレイがついていていいですね。
それにしてもelchikaさん太っ腹。
つぎはobnizのコンテストが開催されています(こちらはパス)。
elchika.com

#include <M5Stack.h>
// The setup() function runs once each time the micro-controller starts
void setup() {
  // init lcd, serial, but don't init sd card
  M5.begin(true, false, true);

  /*
    Power chip connected to gpio21, gpio22, I2C device
    Set battery charging voltage and current
    If used battery, please call this function in your project
  */
  M5.Power.begin();
  M5.Lcd.clear(BLACK);
  M5.Lcd.setTextColor(YELLOW);
  M5.Lcd.setTextSize(3);
  M5.Lcd.setCursor(65, 10);
  M5.Lcd.println("Button example");
  M5.Lcd.setCursor(3, 35);
  M5.Lcd.println("Press button B for 700ms");
  M5.Lcd.println("to clear screen.");
  M5.Lcd.setTextColor(RED);
}

void showBattery() {
  int BatteryLv = M5.Power.getBatteryLevel();

  if (BatteryLv < 40)  M5.Lcd.setTextColor(RED);
  else   M5.Lcd.setTextColor(GREEN);
  M5.Lcd.setTextSize(3);
  M5.Lcd.print(BatteryLv);
  M5.Lcd.print("[");
  for (int i = 0; i < 5; i++) {
    if (i * 20 < BatteryLv) M5.Lcd.print("#");
    else M5.Lcd.print(" ");
  }
  M5.Lcd.println("]");
  M5.Lcd.setTextColor(YELLOW);
  M5.Lcd.setTextSize(5);
}
int rotate = 0;
void loop() {
  // update button state
  M5.update();

  // if you want to use Releasefor("was released for"), use .wasReleasefor(int time) below
  if (M5.BtnA.wasReleased() || M5.BtnA.pressedFor(1000, 200)) {
    M5.Lcd.print("AAA");
  } else if (M5.BtnB.wasReleased() || M5.BtnB.pressedFor(1000, 200)) {
    M5.Lcd.print("BBB");
  } else if (M5.BtnC.wasReleased() || M5.BtnC.pressedFor(1000, 200)) {
    M5.Lcd.clear(BLACK);
    //  M5.Lcd.print(rotate);
    M5.Lcd.print("EEE");
    M5.Lcd.setRotation(rotate++);
    if (rotate > 3)rotate = 0;

  } else if (M5.BtnB.wasReleasefor(700)) {
    M5.Lcd.clear(BLACK);
    M5.Lcd.setCursor(0, 0);
    showBattery();
  }
}

M5Stack Basic

M5Stack Basic

  • メディア: おもちゃ&ホビー