🛠 Inclinomètre sur la matrice : l'IMU LSM6DSV16X en I²C
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
- 1 Arduino UNO R4 WiFi + USB-C cable
- Arduino_LSM6DSV16X library (IDE Library Manager)
- Optional: a Qwiic module (environmental sensor, OLED) to test the external bus.
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
IMU.beginfails in a loop: the IMU is on the internal I²C bus; if you have tinkered with A4/A5 (bus shared with the SDA/SCL pins), the bus might be blocked or pulled in the wrong direction.- Expecting values in m/s² or g without reading the library documentation: the official examples specify the unit — check before calibrating.
- Comparing floats with ==: noisy acceleration never lands exactly on 0; use ranges or
constrain. - Plugging a 3.3 V Qwiic module into 5 V on A4/A5: Qwiic modules operate at 3.3 V, and power is taken from the dedicated pin on the connector.
- Interrupting I²C communications while the matrix is refreshing: both peripherals share resources; if the external I²C scan is silent, disconnect your wires from A4/A5 first.
Other tutorials for this board
- Premier Blink sur la UNO R4 WiFi : la première UNO 32 bits
- Apprivoiser la matrice LED 12x8 intégrée
- Toucher capacitif : touchRead sans composant
- Se connecter au WiFi et lire l'heure NTP (WiFiS3)
- Serveur web embarqué : piloter la matrice depuis le navigateur
- Horloge persistante : RTC à pile + resynchronisation NTP
Code examples for this board
clignotante.inoArduinobouton-poussoir.inoArduinodac-analogique.inoArduinoentree-analogique.inoArduinopwm-respiration.inoArduinoservo-balayage.inoArduino