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. (0x68
or0x69
)
- 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 bothdpsGyro
andaccelAngle
are 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.ino
float 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.