#include <Wire.h>
#include <Adafruit_LSM9DS1.h>
#include <Adafruit_Sensor.h>

Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1();

// ESP32 I2C pins (default)
#define SDA_PIN 21
#define SCL_PIN 22

void setup() {
  Serial.begin(115200);
  delay(1000);
  
  // Initialize I2C with ESP32 pins
  Wire.begin(SDA_PIN, SCL_PIN);
  
  Serial.println("LSM9DS1 Test");
  
  // Initialize the sensor
  if (!lsm.begin()) {
    Serial.println("Failed to find LSM9DS1 chip");
    while (1) { 
      delay(10); 
    }
  }
  Serial.println("LSM9DS1 Found!");
  
  // Setup sensor ranges
  lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_2G);
  lsm.setupMag(lsm.LSM9DS1_MAGGAIN_4GAUSS);
  lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_245DPS);
  
  Serial.println("Sensor configured!");
}

void loop() {
  // Read sensor data
  lsm.read();
  
  // Get sensor events
  sensors_event_t accel, mag, gyro, temp;
  lsm.getEvent(&accel, &mag, &gyro, &temp);
  
  // Print accelerometer data (m/s^2)
  Serial.print("Accel X: "); Serial.print(accel.acceleration.x, 2);
  Serial.print(" Y: "); Serial.print(accel.acceleration.y, 2);
  Serial.print(" Z: "); Serial.print(accel.acceleration.z, 2);
  Serial.print(" m/s^2 | ");
  
  // Print gyroscope data (rad/s)
  Serial.print("Gyro X: "); Serial.print(gyro.gyro.x, 2);
  Serial.print(" Y: "); Serial.print(gyro.gyro.y, 2);
  Serial.print(" Z: "); Serial.print(gyro.gyro.z, 2);
  Serial.print(" rad/s | ");
  
  // Print magnetometer data (gauss)
  Serial.print("Mag X: "); Serial.print(mag.magnetic.x, 2);
  Serial.print(" Y: "); Serial.print(mag.magnetic.y, 2);
  Serial.print(" Z: "); Serial.println(mag.magnetic.z, 2);
  
  delay(100);
}
```

// **Connections:**
// GY-LSM9DS1  →  ESP32
// VCC         →  3.3V
// GND         →  GND
// SDA         →  GPIO 21
// SCL         →  GPIO 22