You don't convert the g values to degrees per seconds.

Besides linear acceleration the accelerometer also measures the static acceleration of the gravity force.

Take two perpendicular accelerometer axis and allign one with the direction of gravity. the first axis will now measure an acceleration of 1g, while the other one measures 0g. Now if you rotate the the accelerometer by 45 degrees. Both axis will now return you the same acceleration. rotate 45 degrees further and the first axis now show 0g and the second axis 1g.

You can now see that with the ratio between the two perpendicular accelerometer axis you can determine the pitch or roll angle

This angle is very stable when the accelerometer is not being moved, however if it's moved it tends to contain alot of noise and error. Therefore it is combined with the angle derived from the gyroscope. It gives you the best of both worlds. The accelerometer angle compensated for the drift caused by the integration of the angular velocity of the gyroscope.

This is the code i used earlier for a project. Here i use a full gyro range of 1000 degrees/s (see mpu6050 datasheet) hence the 32.8 sensitivity. delta_t is calculated using the third method i gave in my earlier post. I have chosen a filter coëfficiënt of 0.98.

float P_CompCoeff= 0.98;

void ComplementaryFilter(int ax,int ay,int az,int gy,int gz) {

long squaresum=(long)ay*ay+(long)az*az;

pitch+=((-gy/32.8f)*(delta_t/1000000.0f));

pitchAcc =atan(ax/sqrt(squaresum))*RAD_TO_DEG;

pitch =P_CompCoeff*pitch + (1.0f-P_CompCoeff)*pitchAcc;

}

This can give you very smooth results but it also has a downside, The response is quite slow with a low samplerate. With high frequency movements the returned angle might not reach it's real maximum value. At a samplerate of 50 Hz this is already noticable at oscillations of 2 Hz. In most of the examples online examples they tend to ignore this even when there is a quite a simple solution. In the available examples they take a measurement, run it through the complimentary filter, return the value,and then they delay for lets say 20ms to achieve a samplerate of 50 Hz.

The better thing to do is actually to take measurements as fast as possible (delete the delay) and then only store a measurement when more then 20000 microseconds have passed (50 Hz). This way the complimentary filter

will be using a smaller dt for more accurate results and the respons is alot faster since the complimentary filter now processes data close to a 1000 Hz. To calculate the time passed since the last stored measurement you can just sum all the delta_t values (calculated with the third method i gave in the earlier post) , when the sum is larger then 20000 microsconds you store the current measurement and reset the sum to zero.