🛠 Inclinomètre sur la matrice : l'IMU LSM6DSV16X en I²C

Arduino UNO R4 WiFi · intermediaire · Schéma : IMG-uno-r4-wifi-intermediaire-2-ur4wifi-imu-matrice.svg — UNO R4 WiFi inclinée sur la tranche, puce IMU LSM6DSV16X surlignée près du connecteur Qwiic, bus I²C A4/A5 annoté SDA/SCL, matrice LED affichant une flèche qui suit l'inclinaison.

Objectives

Read a real sensor on the I²C bus: the board includes an onboard 6-axis inertial measurement unit (IMU) ST LSM6DSV16X (accelerometer + gyroscope) connected internally, and a Qwiic connector (I²C, 3.3 V) for plugging in sensors without soldering. You will read acceleration and map the tilt to the LED matrix — the first step toward a bubble level or a tilt-based game.

Hardware

Complete Code

#include <Arduino_LSM6DSV16X.h>
#include "Arduino_LED_Matrix.h"

ArduinoLEDMatrix matrix;

void setup {
  Serial.begin(115200);
  while (!Serial && millis < 5000) { }
  if (!IMU.begin) {
    Serial.println(F("IMU absente — vérifiez le bus I2C"));
    while (true) { }
  }
  Serial.print(F("Accro. min/max : ")); Serial.print(IMU.accelerationMinX);
  Serial.print(F(" / ")); Serial.println(IMU.accelerationMaxX);
  matrix.begin;
}

void loop {
  float ax, ay, az;
  if (IMU.readAcceleration(ax, ay, az)) {
    // ay > 0 : penché d'un côté, ay < 0 : de l'autre (axes carte).
    int col = (int)((ax + 1.0f) / 2.0f * 11.0f);   // -1..+1 -> 0..11
    col = constrain(col, 0, 11);
    int row = (int)((ay + 1.0f) / 2.0f * 7.0f);    // -1..+1 -> 0..7
    row = constrain(row, 0, 7);

    matrix.beginFrame;
    for (uint8_t c = 0; c < 12; c++)
      matrix.setColumn(c, c == col ? (1 << row) : 0);
    matrix.show;

    Serial.print(ax); Serial.print('\t'); Serial.print(ay);
    Serial.print('\t'); Serial.println(az);
  }
  delay(30);
}

Expected Verification

An LED moves on the matrix when you tilt the board along both axes; the Serial Plotter shows ax ≈ 0, ay ≈ 0, az ≈ 1 g when flat (library convention). Blow on it or tap the table: the gyroscope (not used here) would detect the vibration.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials