Introduction
M5StickCのサンプルプログラムを動かしてみます.
Updates!!
- 【2022/12/28】
- ボタンのサンプルプログラム追加
- ディスプレイのサンプルプログラム追加
- ジャイロセンサーのサンプルプログラム追加
- RTCのサンプルプログラム追加
Coming soon
- [ ] WiFi通信
Environment
- Windows11
- M5StickC
Quick Start
ライブラリマネージャーからM5StickC
をダウンロードします.
サンプルプログラム
Button
押されたボタンに応じてA
,B
の文字を画面に追加していきます.
ソースコード
M5.BtnA.wasReleased()
でボタンが押されたかを判定します.
これで押されたボタンに応じて画面に表示する文字を変更します.
examples\Basics\Button\Button.ino
#include <M5StickC.h>
/* After M5StickC is started or reset
the program in the setUp () function will be run, and this part will only be
run once.
*/
void setup() {
M5.begin(); // Initialize the M5StickC object.
M5.Lcd.setTextColor(
YELLOW); // Set the font color to yellow.
M5.Lcd.setRotation(3);
M5.Lcd.println(
"Button example"); // The screen prints the formatted string and wraps
// the line.
M5.Lcd.println("Press button B for 700ms");
M5.Lcd.println("to clear screen.");
M5.Lcd.setTextColor(RED);
}
/* After the program in setup() runs, it runs the program in loop()
The loop() function is an infinite loop in which the program runs repeatedly
*/
void loop() {
M5.update(); // Read the press state of the key.
if (M5.BtnA
.wasReleased()) { // If the button A is pressed.
M5.Lcd.print('A');
} else if (M5.BtnB.wasReleased()) { // If the button B is pressed.
M5.Lcd.print('B');
} else if (M5.BtnB.wasReleasefor(
700)) { // The button B is pressed for 700ms.
M5.Lcd.fillScreen(
BLACK); // Set BLACK to the background color.
M5.Lcd.setCursor(0, 0);
}
}
デモ動画
デモ動画はこちら

M5StickCのサンプルプログラムを動かしてみる(ボタン編)
押されたボタンに応じて`A`,`B`の文字を画面に追加していきます.
Display
画面の色を変化させたり,文字を表示したりしてみます.
ソースコード
examples\Basics\Display\Display.ino
#include <M5StickC.h>
/* After M5StickC is started or reset
the program in the setUp () function will be run, and this part will only be
run once.
*/
void setup() {
M5.begin();
// Lcd display
M5.Lcd.fillScreen(WHITE);
delay(500);
M5.Lcd.fillScreen(RED);
delay(500);
M5.Lcd.fillScreen(GREEN);
delay(500);
M5.Lcd.fillScreen(BLUE);
delay(500);
M5.Lcd.fillScreen(BLACK);
delay(500);
// text print.
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 10);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("Display Test!");
// draw graphic.
delay(1000);
M5.Lcd.drawRect(15, 55, 50, 50, BLUE);
delay(1000);
M5.Lcd.fillRect(15, 55, 50, 50, BLUE);
delay(1000);
M5.Lcd.drawCircle(40, 80, 30, RED);
delay(1000);
M5.Lcd.fillCircle(40, 80, 30, RED);
delay(1000);
}
/* After the program in setup() runs, it runs the program in loop()
The loop() function is an infinite loop in which the program runs repeatedly
*/
void loop() {
// rand draw
M5.Lcd.fillTriangle(random(M5.Lcd.width() - 1), random(M5.Lcd.height() - 1),
random(M5.Lcd.width() - 1), random(M5.Lcd.height() - 1),
random(M5.Lcd.width() - 1), random(M5.Lcd.height() - 1),
random(0xfffe));
}
M5.Lcd.fillScreen
色をフルスクリーンで変える場合はこちらのM5.Lcd.fillScreen
を使用して変えます.
// Lcd display
M5.Lcd.fillScreen(WHITE);
delay(500);
M5.Lcd.fillScreen(RED);
delay(500);
M5.Lcd.fillScreen(GREEN);
delay(500);
M5.Lcd.fillScreen(BLUE);
delay(500);
M5.Lcd.fillScreen(BLACK);
delay(500);
M5.Lcd.printf
M5.Lcd.printf
で文字を表示します.
// text print.
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 10);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("Display Test!");
M5.Lcd.draw
M5.Lcd.drawRect
で四角を,M5.Lcd.drawCircle
で円を描画します.
// draw graphic.
delay(1000);
M5.Lcd.drawRect(15, 55, 50, 50, BLUE);
delay(1000);
M5.Lcd.fillRect(15, 55, 50, 50, BLUE);
delay(1000);
M5.Lcd.drawCircle(40, 80, 30, RED);
delay(1000);
M5.Lcd.fillCircle(40, 80, 30, RED);
デモ動画
デモ動画はこちら
- YouTube
YouTube でお気に入りの動画や音楽を楽しみ、オリジナルのコンテンツをアップロードして友だちや家族、世界中の人たちと共有しましょう。
ジャイロセンサー
ジャイロセンサーの値を表示したりしてみます.
ソースコード
#include <M5StickC.h>
float accX = 0.0F;
float accY = 0.0F;
float accZ = 0.0F;
float gyroX = 0.0F;
float gyroY = 0.0F;
float gyroZ = 0.0F;
float pitch = 0.0F;
float roll = 0.0F;
float yaw = 0.0F;
/* After M5StickC is started or reset
the program in the setUp () function will be run, and this part will only be
*/
void setup() {
M5.begin();
M5.IMU.Init(); // Init IMU. 初始化IMU
M5.Lcd.setRotation(3);
M5.Lcd.setCursor(40, 0);
M5.Lcd.println("IMU TEST");
M5.Lcd.setCursor(0, 10);
M5.Lcd.println(" X Y Z");
M5.Lcd.setCursor(0, 50);
M5.Lcd.println(" Pitch Roll Yaw");
}
/*****************************************
M5.IMU.getGyroData(&gyroX,&gyroY,&gyroZ);
M5.IMU.getAccelData(&accX,&accY,&accZ);
M5.IMU.getAhrsData(&pitch,&roll,&yaw);
M5.IMU.getTempData(&temp);
*****************************************/
/* After the program in setup() runs, it runs the program in loop()
The loop() function is an infinite loop in which the program runs repeatedly
*/
void loop() {
float temp = 0;
M5.IMU.getGyroData(&gyroX, &gyroY, &gyroZ);
M5.IMU.getAccelData(&accX, &accY, &accZ);
M5.IMU.getAhrsData(&pitch, &roll, &yaw);
M5.IMU.getTempData(&temp);
M5.Lcd.setCursor(0, 20);
M5.Lcd.printf("%6.2f %6.2f %6.2f o/s\n", gyroX, gyroY, gyroZ);
M5.Lcd.printf(" %5.2f %5.2f %5.2f G\n\n\n\n", accX, accY, accZ);
M5.Lcd.printf(" %5.2f %5.2f %5.2f\n", pitch, roll, yaw);
M5.Lcd.printf("Temperature : %.2f C", temp);
delay(100);
}
デモ動画
デモ動画はこちら

M5StickCのサンプルプログラムを動かしてみる( ジャイロセンサー編)
ジャイロセンサーの値を表示したりしてみます.ttps://hamaruki.com/2022/12/28/m5stickc_iot_002/
RTC(real-time clock)
RTCの値を表示したりしてみます.
ソースコード
#include <M5StickC.h>
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
/* After M5StickC is started or reset
the program in the setUp () function will be run, and this part will only be
run once. */
void setup() {
M5.begin();
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(40, 0, 2);
M5.Lcd.println("RTC TEST");
RTC_TimeTypeDef TimeStruct;
TimeStruct.Hours = 18; // Set the time.
TimeStruct.Minutes = 56;
TimeStruct.Seconds = 10;
M5.Rtc.SetTime(&TimeStruct); // and writes the set time to the real time
RTC_DateTypeDef DateStruct;
DateStruct.WeekDay = 3; // Set the date.
DateStruct.Month = 3;
DateStruct.Date = 22;
DateStruct.Year = 2019;
M5.Rtc.SetData(&DateStruct);
}
/* After the program in setup() runs, it runs the program in loop()
The loop() function is an infinite loop in which the program runs repeatedly
*/
void loop() {
M5.Rtc.GetTime(&RTC_TimeStruct); // Gets the time in the real-time clock.
M5.Rtc.GetData(&RTC_DateStruct);
M5.Lcd.setCursor(0, 15);
M5.Lcd.printf("Data: %04d-%02d-%02d\n", RTC_DateStruct.Year,
RTC_DateStruct.Month, RTC_DateStruct.Date);
M5.Lcd.printf("Week: %d\n", RTC_DateStruct.WeekDay);
M5.Lcd.printf("Time: %02d : %02d : %02d\n", RTC_TimeStruct.Hours,
RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);
delay(500);
}
デモ動画
デモ動画はこちら

M5StickCのサンプルプログラムを動かしてみる( RTC編)
RTCの値を表示したりしてみます.
コメント