# Arduino Nano 33 IoT LSM6DS3: get gyro angle in degrees

I have an Arduino Nano 33 IoT and I want to get the angle of it using the built-in gyro but I can't seem to find any example code that does this.

The default gyro and accelerometer example code doesn't help. Is it even possible to do this?

• a gyro detects the rate of angular change, not an absolute angle ... an accelerometer can measure an angle with respect to gravity Oct 31, 2020 at 22:39
• I tried looking for both but there is no code that does it. Could you help me find it? Nov 1, 2020 at 15:44
• All I can find is changing numbers but no angles. Nov 1, 2020 at 15:44

You wil have to calculate the angles. Some trigonometry is required, and for the gyroscopes, also some integration. You can read about it here, and in many other places.

You will get the best results if you combine the data from the gyroscopes and the accelerometers using a complementary filter, a Kalman filter, or an other algorithm. These are also easily googled.

There is some example code for the LSM6DS3 IMU on the Nano 33 IoT here on Github.

• This is the best solution. This filter is a lot faster than the MadgwickAHRS! Amazing! Jan 15, 2021 at 15:35

You are supposed to use a filter library on top of the output of the gyro as well as the accelerometer data (both is used!) to get angles ... https://www.arduinolibraries.info/libraries/madgwick

Here is a working example that prints the pitch of the sensor from +90 to -90 degrees:

//#include <SPI.h>
//#include <Wire.h>
#include <Arduino_LSM6DS3.h>
#include <Math.h>

float pitchFilteredOld;
const float sensorRate = 104.00;

void setup() {
Serial.begin(9600);
while (!Serial);
if(!IMU.begin())  {
Serial.println("Failed to initialize IMU!");
while (1);
}
filter.begin(sensorRate);
Serial.println("Setup complete!");
}
void loop() {
float xAcc, yAcc, zAcc;
float xGyro, yGyro, zGyro;