Calculate the angle of inclination with the IMU integrated in the arduino nano rp2040

Hello, I have recently purchased an Arduino rp 2040 card for a project. found. I have at my disposal as value the values ​​of acceleration and angular acceleration on the 3 axes and i want to have the angle of the arduino. if anyone has a solution in any form I take and thank you in advance.

For static tilt measurements, just use the accelerometer. The formula is very simple, and is given in this tutorial.

1 Like

Thanks, i will try it tomorow, if i have probleme i will sent another message. :+1:t3:

i try it and it's work so, thanks

share sketch?

u want the sketch to calculate the angle?

yes, specifically for RP2040

/* IMU.begin() - initializes the library.
IMU.accelerationSampleRate() - reads the sampling rate in Hz.
IMU.accelerationAvailable() - checks if there's data available from the IMU.
IMU.readAcceleration(Ax, Ay, Az) - reads the accelerometer, and returns the value of the x y and z axis.
IMU.gyroscopeSampleRate() - reads the sampling rate in Hz.
IMU.gyroscopeAvailable() - checks if there's data available from the IMU.
IMU.readGyroscope(Gx, Gy, Gz) - reads the accelerometer, and returns the value of the x y and z axis.

/\ all the imu fonctions

*/


#include <Arduino_LSM6DSOX.h>

float Ax, Ay, Az;
float Gx, Gy, Gz;
float angle;
double roll = 0.00;
double pitch = 0.00;
void setup() {
  Serial.begin(9600);


  while (!Serial)
    ;

  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1)
      ;
  }

  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println("Hz");
  Serial.println();

}

void loop() {
//read accelerometer data
  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(Ax, Ay, Az);

    Serial.println("Accelerometer data: ");
    Serial.print(Ax);
    Serial.print('\t');
    Serial.print(Ay);
    Serial.print('\t');
    Serial.println(Az);
    Serial.println();
  }
//Calculation of the different inclinations (pitch, roll)
  roll = atan2(Ay, Az) * 57.3;
  pitch = atan2((-Ax), sqrt(Ay * Ay + Az * Az)) * 57.3;

//Print all the different accélration
  Serial.println("Roll: ");
  Serial.print(roll);
  Serial.println();

  Serial.println("Pitch: ");
  Serial.print(pitch);
  Serial.println();
  delay(1000);
}

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