Easy-to-use, minimal and lightweight Arduino library for MPU6050s
Quick and easy to download, set up and use for any MPU6050 project. Reads gyroscope, accelerometer and temperature data. Trigonometry, integration, formulae and complementary filters are used to calculate angles to precision.
You should get a small message to indicate successful download. Now close the Arduino IDE and reopen for the library for the library to appear.
You can now use the library and its examples by either using the examples by navigating through File > Examples > mpu6050 > [EXAMPLE FILE], or using the documentation below.
Begin with #include <mpu6050.h> and use documentation below or use example code.
For full example code, check the examples/ folder.
When a function returns an I2C error code, here is the reference:
DATA_TOO_LONG_FOR_TRANSMIT_BUFFER = 1,
ADDRESS_TRANSMITaa_NACK = 2,
DATA_TRANSMIT_NACK = 3,
OTHER_ERROR = 4,
TIMEOUT = 5- Parameters:
uint8_t addressโ The address of the sensor. (0x68or0x69)
- Returns: I2C error code or 0 (success).
- Parameters:
uint8_t addressโ The address of the sensor.float gX, gY, gZโ Variables passed by reference to store gyroscope values for X, Y, and Z axes.
- Returns: I2C error code or 0 (success).
- Parameters:
uint8_t addressโ The address of the sensor.float aX, aY, aZโ Variables passed by reference to store acceleration values for X, Y, and Z axes.
- Returns: I2C error code or 0 (success).
- Parameters:
uint8_t addressโ The address of the sensor.float tempโ Variable passed by reference to store temperature value for chip temperature
- Returns: I2C error code or 0 (success).
- Parameters:
float rawGX, rawGY, rawGZโ Variables passed that already store raw gyroscope values for X, Y, and Z axes.float dpsGX, dpsGY, dpsGZโ Variables passed by reference to store processed gyroscope values in dps (degrees per second) for X, Y, and Z axes.
- Returns: 0 (success)
- Parameters:
float rawAX, rawAY, rawAZโ Variables passed that already store raw accelerometer values for X, Y, and Z axes.float gForceAX, gForceAY, gForceAZโ Variables passed by reference to store processed accelerometer values in g force for X, Y, and Z axes.
- Returns: 0 (success)
- Parameters:
float dpsGX, dpsGY, dpsGZโ Variables passed that already store dps gyroscope for X, Y, and Z axes.float pitch, roll, yawโ Variables passed by reference to store gyroscope roll, yaw and pitch values.
- Returns: 0 (success)
- Parameters:
uint8_t addressโ The address of the sensor.double gyroOffsetX, gyroOffsetY, gyroOffsetZโ Variables passed by reference to store gyroscope offset for X, Y and Z axes.
- Returns: 0 (success)
- Parameters:
uint8_t addressโ The address of the sensor.double accelOffsetX, accelOffsetYโ Variables passed by reference to store accelerometer offset for X and Y axes
- Returns: 0 (success)
Uses trigonometry to find pitch and roll from accelerometer values.
- Parameters:
float gForceAX, gForceAY, gForceAZโ Variables passed that already store accelerometer g force for X, Y and Z axes.float roll, pitchโ Variables passed by reference to store accelerometer roll and pitch values.
- Returns: 0 (success)
-
Parameters:
โ ๏ธ Important: ensure that bothdpsGyroandaccelAngleare values of the same axis-
float dpsGyroโ Variable passed that already stores dps value from gyroscope. -
float accelAngleโ Variable passed that already stores pitch or roll found from accelerometer values with trigonometry. -
float alpha- Variable passed that already stores alpha value. The alpha value is how much the gyroscope values in the complementary filter are weighted. Most people use 0.95 - 0.98.
A higher alpha value means:
Advantages Disadvantages quicker responsiveness more susceptible to gyro bias if offsets are inaccurate. less noise drifts more over time float deltaTime- Variable passed that already stores delta time value. The delta time value is the sampling rate. In other words, how much time it takes for one loop of the Arduino code to run. This is best done dynamically although it is not mandatory. An example of dynamic delta time calculation can be found infiltered-angles.inofloat filteredAngle- Variable passed by reference to store filtered pitch or roll values.
-
-
Returns: 0 (success)
No reason for this not to work on all Arduino architectures unless it doesn't support I2C. It has been tested successfully with:
- Arduino Uno
- Arduino Nano
- Arduino Nano Every
- "AZ-Delivery" Nano
- MPU6050 (GY-521 breakout board)
- Arduino IDE 2.3.6
If you get this working on another architecture, feel free to pull request or share your setup in the discussions tab to expand its tested compatability.
This project is licensed under the MIT License - see the LICENSE file for details.





