0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5StampS3 でIMU(とLED制御)

Last updated at Posted at 2024-04-21

M5StampS3が安かったので自腹で買ってみた。
ついでにIMU(MPU6886)もお小遣いで買ってつなげてみる。

M5Stampは何種類かあるが、M5StampS3はシリアル変換ICも載っていて使い勝手が良い。
(この前にM5StampC3Uを間違えて買ってシリアル変換ICが無くてちょっと微妙だった)

Grove端子はついていないので、自分でGrove端子を買ってきてはんだ付けが必要。
写真のように「GND, 5V, 13, 15」が接続されるようにはんだ付け。
こうしてつけると、WireのI2Cとして使えるようになる。

IMG_20240421_093530~2.jpg

Grove端子は狭ピッチのIO穴にはんだ付けが必要で、ちょっと難しい。
固定器具あった方がイライラしないではんだ付けできます。

で、お試しコード。
Aruduino IDEで、以下を設定。

【準備】

  1. 「基本設定 > 追加のボードマネージャのURL」に以下を追加
    https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/arduino/package_m5stack_index.json

  2. ボードマネージャから「M5Stack」を検索し、「M5Stack by M5Stack」をインストール
     2024/4/21時点のバージョンは「2.1.1」
    M5stampS3を選ぶ

  3. ライブラリマネージャから「FastLED」を検索しインストール
     2024/4/21時点のバージョンは「3.6.0」

  4. ライブラリマネージャから「I2C_MPU6886」を検索しインストール
     2024/4/21時点のバージョンは「1.0.0」
    ※M5StampS3純正のIMUライブラリはないので有志の方の互換ライブラリ

以下サンプルコード
LED制御しつつ、IMUを周期的に読んでシリアルに加速度のみ出力。

受信側はTeratermのログ機能などで拾って保存しておく。

m5stamp_imu_led.ino.c
#include <Arduino.h>
#include <FastLED.h>
#include "I2C_MPU6886.h"

I2C_MPU6886 imu(I2C_MPU6886_DEFAULT_ADDRESS, Wire);

#define PIN_BUTTON 0
#define PIN_LED    21

CRGB led;
uint8_t led_ih             = 0;

void setup() {
    Serial.begin(115200);
    Serial.println("StampS3+IMU+LED");

    pinMode(PIN_BUTTON, INPUT);

    FastLED.addLeds<WS2812, PIN_LED, GRB>(&led, 1);

    Wire.begin(13, 15);
    imu.begin();

    Serial.printf("imu.whoAmI() = 0x%02x\n", imu.whoAmI());

}

void loop() {
    float ax, ay, az;
    imu.getAccel(&ax, &ay, &az);
    Serial.printf("%d,%f,%f,%f\n", millis(), ax, ay, az);

    led = CHSV(led_ih, 255, 255);
    FastLED.show();
    led_ih++;
    delay(5);
}
実際のデータ
time[ms], ax, ay, az
209476,-0.006836,-0.118408,-0.919922
209484,-0.006348,-0.115234,-0.897217
209492,-0.002197,-0.116699,-0.914307
209500,-0.000977,-0.123779,-0.914307
209508,-0.001709,-0.122070,-0.919678
209516,0.002441,-0.123047,-0.913574
209524,-0.014893,-0.122803,-0.906738
209532,0.010010,-0.125000,-0.927979
209540,-0.001465,-0.122803,-0.897949
209548,-0.009277,-0.136230,-0.907227
209556,-0.001953,-0.117188,-0.919434
209564,0.005615,-0.124268,-0.909668
209572,0.002197,-0.115479,-0.923096
209580,-0.009521,-0.117920,-0.904541
209588,0.010498,-0.108887,-0.925293
209596,0.010010,-0.105469,-0.916016
209604,-0.012939,-0.130859,-0.902344
209612,-0.000244,-0.122559,-0.925049
209620,-0.006348,-0.119141,-0.908691
209628,-0.001465,-0.117920,-0.906982
209636,0.001221,-0.120850,-0.901367
209644,-0.016113,-0.106689,-0.927246
209652,-0.002930,-0.122559,-0.917725
209660,0.001709,-0.115723,-0.896973
209668,0.000488,-0.115479,-0.929199
209676,-0.002686,-0.133057,-0.922607
209684,-0.001709,-0.122559,-0.909180
209692,0.006836,-0.109619,-0.910645
209700,0.003174,-0.116943,-0.918457
209708,0.057861,-0.113281,-0.903076
209716,-0.012207,-0.110840,-0.910400
209724,-0.017822,-0.122314,-0.912354
209732,-0.055908,-0.122070,-0.911865
209740,0.009033,-0.106689,-0.918701
209748,0.000244,-0.119141,-0.907227
209756,-0.013672,-0.120117,-0.905762
209764,0.002441,-0.116699,-0.919678
209772,-0.001221,-0.114014,-0.920166
209780,-0.048340,-0.105225,-0.907959
209788,0.001221,-0.129395,-0.913086
209796,-0.011719,-0.118652,-0.925293
209804,-0.010742,-0.102539,-0.913574
209812,0.005371,-0.120361,-0.925781
209820,-0.001221,-0.123535,-0.923096
209828,-0.014160,-0.115967,-0.906250
209836,-0.011963,-0.131592,-0.914307
209844,-0.001465,-0.119141,-0.914062
209852,-0.007324,-0.124268,-0.911621
209860,-0.006592,-0.121826,-0.902100
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?