imu_lsm6dsox.ino

Arduino Nicla Vision · File : imu_lsm6dsox.ino · Language : Arduino · Use case : Lire accélération et gyroscopes de l’IMU

The Nicla Vision 6-axis LSM6DSOX IMU opens in three lines with the Arduino_LSM6DSOX library: acceleration in g and angular rate in °/s, ready for shock detection, tilt or gesture. The entry point of any motion project.

Hardware

An Arduino Nicla Vision (STM32H7 M7+M4, 23×23 mm) with GC2145 camera, LSM6DSOX IMU, PDM mic, ToF (VL53Lx), ESP32 Wi-Fi/BLE module and RGB LED (LEDR/G/B). The board alone; the IMU is soldered on board. A USB cable for the 115200 serial console.

Wiring

No wiring: the LSM6DSOX sits on the board internal I2C bus, invisible from outside.

How it works

IMU.begin() detects the chip; reads are non-blocking (accelerationAvailable then readAcceleration). The 5 Hz serial sampling keeps the console readable.

Running it

Programmed from the Arduino IDE (ArduinoCore-mbed, NICLA_VISION target) over USB-C; Camera, PDM, Arduino_LSM6DSOX, WiFiNINA and ArduinoBLE libraries provided.

Variants

Compute tilt via atan2 on ax/az, detect shock by a norm threshold, or stream the 6 axes over BLE to a phone.

imu_lsm6dsox.ino

// IMU LSM6DSOX embarquée (accéléro + gyro 6 axes).
// Bibliothèque : Arduino_LSM6DSOX (Gestionnaire de libraries).

#include <Arduino_LSM6DSOX.h>

void setup() {
  Serial.begin(115200);
  while (!Serial) { ; }
  if (!IMU.begin()) {
    Serial.println("IMU absente — vérifier la carte");
    while (1) { ; }
  }
  Serial.print("Accéléro : "); Serial.print(IMU.accelerationSampleRate());
  Serial.println(" Hz");
}

void loop() {
  float ax, ay, az, gx, gy, gz;

  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(ax, ay, az);   // en g (1 g = 9.81 m/s²)
  }
  if (IMU.gyroscopeAvailable()) {
    IMU.readGyroscope(gx, gy, gz);      // en degrés/s
  }

  static unsigned long t = 0;
  if (millis() - t > 200) {
    t = millis();
    Serial.print("ax="); Serial.print(ax);
    Serial.print(" ay="); Serial.print(ay);
    Serial.print(" az="); Serial.print(az);
    Serial.print(" gz="); Serial.println(gz);
  }
}

← Back to the board