MPU6050 Light Library Documentation: Romain JL Fetick January 2021
MPU6050 Light Library Documentation: Romain JL Fetick January 2021
library documentation
Romain JL Fetick
January 2021
Abstract
The MPU6050 light library is made for your Arduino to communi-
cate with the MPU6050 device. It retrieves the MPU6050 acceleration,
gyroscopic and temperature measurements. The angles of the device are
computed from the raw measurements. The tilt angles are computed by
a complementary filter between acceleration and gyroscopic data, thus
providing quite a good accuracy of estimation.
Contents
1 Introduction 2
2 Mathematical expressions 2
2.1 Angle from gyroscopic measurement . . . . . . . . . . . . . . . . 2
2.2 Angle from accelerometric measurement . . . . . . . . . . . . . 3
2.3 Data fusion with complementary filter . . . . . . . . . . . . . . 4
3 Public attributes 4
5 Examples 7
1
1 Introduction
The MPU6050 delivers three angular speeds and three linear accelerations.
This raw data is directly accessible through the MPU6050 light library. More-
over the library performs basic angle computation from this data. Indeed an
integration of the angular speed gives the angles. However any small bias in
the angular speed measurement is integrated and leads to a severe drift of the
estimated angle. This must be corrected using another information, that is
provided by the accelerometer. The gravity is supposed to be the main com-
ponent of the acceleration, so a projection of the gravity on the MPU6050 axis
gives a rough (and noisy) estimate of the tilt angles of the device.
Merging the angles estimated from gyroscopic and accelerometric data pro-
vides a good estimation of the MPU6050 angles. A complementary filter per-
forms the merging of the two sources. For this library, the gyroscopic data is
weighted with a 0.98 factor, whereas the accelerometric data is weighted with
the complementary 0.02 factor. This factor can be fine tuned by the user. The
main hypothesises to ensure the consistency of the angle estimation are
• Small loop delay between to calls to the update( ) function, so the ap-
proximation made for the numerical integration of the angular speed
holds θg [i] ' θg [i − 1] + θ̇g [i] · dt
• Small linear accelerations (the gravity is the main one). This constraint
can be loosened since the complementary filter gives few confidence in
angles computed from acceleration (only 2% of confidence as default
value)
2 Mathematical expressions
The MPU6050 provides acceleration and gyroscopic measurement on the three
axis X,Y and Z of the device. These axis have to be differentiated from the
X0 , Y0 and Z0 reference (North-West-Zenith) axis.
2
Figure 1: Overview of the data accessible through the MPU6050 light library.
The raw data from the MPU6050 is the linear acceleration (green) and angular
speed (red) for the three axis. Computations allow to derive the angles of
the MPU6050. Angles computed from acceleration are noisy. The angles
computed from data fusion between accelerometric and gyroscopic data are
more accurate.
with a first rotation α (pitch) around the Earth Y0 axis, and then a rotation
β (roll) around the device X axis. Since the roll is around the MPU6050 axis,
we directly get the identity β = θx . It will be replaced in the following.
and
ay
= − tan θx (5)
az
3
2.2.2 Tilt angles
The tilt angles are given such as
ay
tan φx = sgn(az ) p (6)
a2x + a2z
and
ax
tan φy = p (7)
a2y + a2z
We note that the tilt angle around Y0 corresponds to the pitch of the device,
we directly get the identity φx = α.
3 Public attributes
The upsideDownMounting is the only accessible (public) attribute of the
MPU6050 class. Setting it to true allows to make the library to work properly
when the device is mounted upside down as if it was upside up. Default value:
false.
4
4.2 Configuration setters
setGyroOffsets( float x, float y, float z): void
Manually set gyroscope offsets if you already know them. Otherwise use the
method calcOffsets( ).
getGyroXoffset[,Y,Z]( ): float
Offset on the gyroscope around the X axis.
Units: data is given in degrees per second.
Note: similar functions exist on Y and Z axis.
5
4.5 Data getters
getTemp( ): float
Last measurement of the device temperature.
Units: data is given in Celsius degrees.
getAccX[,Y,Z]( ): float
Last measurement of the acceleration on the X axis.
Units: data is given as multiple of the gravity norm g = 9.81 m.s−2
Note: similar functions exist on Y and Z axis.
getGyroX[,Y,Z]( ): float
Last measurement of the angular speed around the X axis.
Units: data is given in degrees per second.
Note: similar functions exist on Y and Z axis.
getAccAngleX[,Y]( ): float
Estimated angle around the X axis, computed with the accelerometer data
(tilt of the gravity vector). This data is noisy, and is valid only for small linear
accelerations (the gravity is the major acceleration). It is better to use the
angle given by the getAngleX( ) function.
Units: data is given in degrees.
Note: a similar function exists on Y axis.
getAngleX[,Y,Z]( ): float
Estimated angle around the X axis. The angle is computed through a com-
plementary filter merging data from accelerometer and gyroscope integration.
This might be the best estimate of the angles provided by this library.
Units: data is given in degrees.
Note: similar functions exist on Y and Z axis.
6
5 Examples
The minimal code below shows how to initialise and retrieve some data from
the MPU6050 device.
#i n c l u d e ”Wire . h”
#i n c l u d e <MPU6050 light . h>
MPU6050 mpu( Wire ) ;
void setup ( ) {
Wire . b e g i n ( ) ;
mpu . b e g i n ( ) ;
mpu . c a l c O f f s e t s ( ) ;
}
void loop ( ) {
mpu . update ( ) ;
f l o a t a n g l e [ 3 ] = {mpu . getAngleX ( ) ,
mpu . getAngleY ( ) ,
mpu . getAngleZ ( ) } ;
f l o a t gyro [ 3 ] = {mpu . getGyroX ( ) ,
mpu . getGyroY ( ) ,
mpu . getGyroZ ( ) } ;
// p r o c e s s t h i s data f o r your needs . . .
// ( do something . . . )
}
7
6 Authors and license
• RFETICK (github.com/rfetick): modifications and documentation
The MPU6050 light library is provided under the MIT license. Please check
the LICENSE file included with the library for more information.