 # Arduino Nano 33 IoT: Angles from IMU

Hi Friends!
I am writing here to share with you my code to get the Roll and Pitch with Arduino Nano 33 IoT. Until now, I have used the sensor MPU6050. But I decided not to use anymore since I discovered that Arduino has incorporated an IMU in the same board using the official LSM6DS3 library.
I have different personal reasons for this change, but the most important for me has been to avoid use non official libraries and wires, so it is more compact.
I am not a professional software developer, so I am sure that the code could be improved, but it works fine.
I hope that it would be useful for you:

``````#include <Arduino_LSM6DS3.h>

double X, Y, Z;
float gx, gy, gz;
float ax, ay, az;
double Fx, Fy, Fz;
double t, d;

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

void loop() {

if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
t = pow(IMU.gyroscopeSampleRate(), -1);
}

X = atan(gy / sqrt(pow(gx, 2) + pow(gz, 2))) * 57.2957795130823;
Y = atan(gx / sqrt(pow(gy, 2) + pow(gz, 2))) * 57.2957795130823;
Z = atan(gz / sqrt(pow(gy, 2) + pow(gx, 2))) * 57.2957795130823;

if (millis() - d >= 2) {
Fx = 0.98 * (Fx + t * ax) + 0.02 * X;
Fy = 0.98 * (Fy + t * ay) + 0.02 * Y;
Fz = 0.98 * (Fz + t * ay) + 0.02 * Z;
d = millis();
}

Serial.println(Fz);
}
``````