Enable INT1 in LSMDSRX Sudden acceleration occurs interfaced with ESP32S3

// I am trying to enable the INT1 interrupt when sudden acceleration occurs. ESP32 interfaced with LSMDSRX via i2c //

#include <Wire.h>

#define LSM6DSR_ADDRESS 0x6B  // I2C address of the sensor
#define CTRL1_XL_REG 0x10     // Register address for accelerometer control
#define CTRL2_G_REG 0x11      // Register address for gyroscope control
#define ACCEL_DATA_REG 0x28   // Register address for accelerometer data (LSB)
#define GYRO_DATA_REG 0x22    // Register address for accelerometer data (LSB)
#define INT1_CTRL_REG 0x0D    // Register address for INT1_CTRL

#define INT1_PIN 34  // This could be any digital pin on the ESP32 for INT1


byte receivedData[6];

int16_t accelX;
int16_t accelY;
int16_t accelZ;
double x3;
double y3;
double z3;
#define minVal -8315
#define maxVal 8315

void setup() {
  Wire.begin(4, 5);      // Initialize I2C communication
  Serial.begin(115200);  // Start serial communication for debugging
  initializeSensor();
  pinMode(INT1_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(INT1_PIN), handleLSMInterrupt, RISING);
}

void loop() {
  readAndPrintSensorData();
}

void handleLSMInterrupt() {
  readAndPrintSensorData();
}

void initializeSensor() {
  // Configure accelerometer settings
  Wire.beginTransmission(LSM6DSR_ADDRESS);
  Wire.write(CTRL1_XL_REG);


  // Set the accelerometer scale to ±2g (you can choose ±4g, ±8g, or ±16g as well)
  Wire.write(0x18);

  // Enable the accelerometer data ready interrupt on INT1 pin
  Wire.write(0x01 << 3);  // Bit 3 corresponds to accelerometer data ready interrupt

  Wire.endTransmission();
}

void readAndPrintSensorData() {
  Wire.beginTransmission(LSM6DSR_ADDRESS);
  Wire.write(GYRO_DATA_REG);
  Wire.endTransmission();

  Wire.requestFrom(LSM6DSR_ADDRESS, 6, false);  // Request 6 bytes (X, Y, Z accelerometer data)
  for (int i = 0; i < 6; i++) {
    receivedData[i] = Wire.read();
  }
  accelX = (receivedData[1] << 8) | receivedData[0];  // Combine LSB and MSB
  accelY = (receivedData[3] << 8) | receivedData[2];
  accelZ = (receivedData[5] << 8) | receivedData[4];

  int xAng = map(accelX, minVal, maxVal, -90, 90);
  int yAng = map(accelY, minVal, maxVal, -90, 90);
  int zAng = map(accelZ, minVal, maxVal, -90, 90);
  x3 = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
  y3 = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
  z3 = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
  Serial.print("X:");
  Serial.println(x3);

}


I need suggestion to correct this code

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.