M5Atomの使い方
https://scrapbox.io/files/609475cff2f5f9001cac46bc.png
M5 Atomが使えるまでの手順
Step:1 Arduino Studioをインストール
ボードマネージャの追加
↑を追加後にボードマネージャからM5 で検索(詳細はクイックスタート参照)
M5StickCかEsp32PICOを指定
M5 Atomeを指定
フォルダを指定してのドライバインストールを2回繰り返す必要があります(伝統)
Step:2 各種ライブラリのインクルード
ライブラリの追加
M5Atom
fastLED
Step:3 サンプルプログラムの実行
※M5Atom liteを使う時の注意点
センサ入力(アナログ入力/AD変換):analogRead()
G32,G33のみOK
Wifi,Bluetoothを使用していないときはG25,G26もOKの
モータ制御(DA変換,PWM出力): dacWrite()
G25,G26のみOK
LEDのPWM調光(ledcWrite() )はすべてのポートでOK
digitalRead(),digitalWrite()もすべてのポートでもOK
サンプルプログラム
M5Atom でLチカ①
code: Blink.ino
#define LED_BUILTIN 25 //LED_BUILTINにポートG25 定義 void setup()
{
M5.begin(true, true, true); // 初期化
delay(10); //10msec 待機
pinMode(LED_BUILTIN, OUTPUT); //LED_BUILTIN(ポートG25)を出力に設定
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
M5Atom でLチカ② ボタンで点滅速度が変化
code: Blink.ino
#define LED_BUILTIN 25 //LED_BUILTINにポートG25 定義 void setup()
{
M5.begin(true, true, true);
delay(10);
pinMode(LED_BUILTIN, OUTPUT); //LED_BUILTIN(ポートG25)を出力に設定
}
uint8_t FSM = 0;
void loop()
{
if (M5.Btn.wasPressed())
{
FSM++;
if (FSM >= 4)
{
FSM = 0;
}
}
switch (FSM)
{
case 0:
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
break;
case 1:
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
break;
case 2:
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
break;
case 3:
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
break;
default:
break;
}
delay(50);
M5.update();
}
フォトリクレクタ読み取り①
code: M5Atom_PhotoReflector.ino
/*
AnalogReadSerialを参考
*/
void setup()
{
M5.begin(true, true, true);
delay(10);
}
void loop() {
// read the input on analog pin 33:
int sensorValue = analogRead(33);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
フォトリクレクタ読み取り②
code: M5Atom_PhotoReflector2.ino
/*
AnalogInOutSerialを参考
*/
const int analogInPin = 33; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 25; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup()
{
M5.begin(true, true, true);
delay(10);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
// analogWrite(analogOutPin, outputValue); // print out the value you read: 各自書き換えましょう
// print the results to the Serial Monitor:
Serial.print(sensorValue);
Serial.print(",");
Serial.println(outputValue);
delay(1); // delay in between reads for stability
}
実習で使用するフォトリフレクタ
https://gyazo.com/71c7de59f100448c29cdb7f29fbd171b