RobTillaart / GY521

Arduino library for GY521 accelerometer- gyroscope a.k.a. MCU-6050
MIT License
38 stars 16 forks source link
acceleration arduino gyro

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

GY521

Arduino library for I2C GY521 accelerometer-gyroscope sensor a.k.a. MCU-6050.

Description

Experimental library for GY521 a.k.a. MCU-6050.

Library is work in progress, in fact it is extracted and extended from an old project. It needs to be tested a lot more.

See changelog.md for latest updates.

0.6.1

Improved calibrate() to support any angle.

0.6.0

Fixed a bug in calibration function, making previous versions obsolete.

0.5.0 Breaking change

Version 0.5.0 introduced a breaking change. You cannot set the pins in begin() any more. This reduces the dependency of processor dependent Wire implementations. The user has to call Wire.begin() and can optionally set the Wire pins before calling begin().

Examples

Related

Breakout board

From left to right

pin pinName description notes
0 VCC +5V
1 GND ground
2 SCL I2C clock
3 SDA I2C data
4 XDA auxiliary data see datasheet
5 XCL auxiliary clock see datasheet
6 AD0 address
7 INT interrupt

Address

AD0 connected to GND => 0x68 AD0 connected to VCC => 0x69

Calibration (short version for now)

Since version 0.5.2 the library has a build in void calibrate(times) function which can be called instead of manual calibration. This function overwrites the values of axe aye aze gxe gye gze. calibrate() must be called after setAccelSensitivity(as) and setGyroSensitivity(gs).

Note the calibrate() function takes time, depending on the number of times.

Since version 0.6.1 the calibrate function is extended with optional parameters so the sensor can be calibrated in any angle. bool calibrate(times, angleX = 0, angleY = 0, inverted = false)

Manual calibration

  1. load and run calibration example
    it shows a header containing 6 numbers and 10 lines of 8 numbers
  2. wait until the middle 6 of the longer lines stabilize (should all be 0)
  3. copy the 6 numbers above the axe aye aze as these are the numbers needed.

Interface

#include "GY521.h"

Constructor

Calibrate

Ideal the function calibrate() should continue until it is stable (how to define) for n reads. Drawback is that this would make the duration unpredictable.

New since 0.6.1 (experimental)
The optional parameters float angleX = 0, float angleY = 0 should be between -90 .. +90. These can be used if the sensor is not lying flat during calibration. The optional parameter bool inverted = false should be set to true if the sensor is upside down.

Throttle

Read

Set before read

Actual read

Since version 0.3.8 the read() and readGyro() function is updated to keep the range of getPitch(), getRoll() and getYaw() in the range 0..359.999 degrees. (Issue #36). Problem is that with continuous rotation in a same direction internal variables will overflow and new movements (angles) will get lost as insignificant digits.

In version 0.4.0 the normalization of pitch, roll and yaw is fixed and made conditional. (Issue #42).

Calls after read

Note that multiple calls will return the same value. One must explicitly call read() to get new values.

Experimental Pitch Roll and Yaw

Pitch Roll and Yaw is work in progress and should not be used for projects yet.

Digital Low Pass Filter

See datasheet P13-reg26

Mode Acc bandwidth delay Gyro bandwidth delay Fs
0 260 Hz 0.0 256 Hz 1.0 8 kHz
1 184 Hz 2.0 188 Hz 1.9 1 kHz
2 94 Hz 3.0 98 Hz 2.8 1 kHz
3 44 Hz 4.9 42 Hz 4.8 1 kHz
4 21 Hz 8.5 20 Hz 8.3 1 kHz
5 10 Hz 13.8 10 Hz 13.4 1 kHz
6 5 Hz 19.0 5 Hz 18.6 1 kHz

Generic Register Access

Read the register PDF for the specific value and meaning of registers.

Documents

Error codes

Error code value notes
GY521_OK 0 not an error
GY521_THROTTLED 1 not an error
GY521_ERROR_READ -1
GY521_ERROR_WRITE -2
GY521_ERROR_NOT_CONNECTED -3

Sensitivity Acceleration

The strength of Earth's gravity varies with latitude (equator = 0°, poles = 90°). The standard value for gravity (gn) is 9.80665 m/s^2 (often 9.81 m/s^2) At the equator the gravity (ge) is 9.78033 m/s^2.

The library provides the constant GRAVITY = 9.80655

value Acceleration m/s2 notes
0 2 g 19.6131 default
1 4 g 39.2262
2 8 g 78.4524
3 16 g 156.9048

Sensitivity Gyroscope

unit dps = degrees per second.

value Gyroscope radians/sec notes
0 250 dps 4.36332313 default
1 500 dps 8.72664626
2 1000 dps 17.45329252
3 2000 dps 34.90658504

Operation

See examples, use with care.

Future

There is no intention to implement getters and setters for all registers. However if one specific is needed, please open an issue.

Must

Should

Could

Wont

Support

If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.

Thank you,