The MPU-6050 devices combine a 3-axis gyroscope and a 3-axis accelerometer on the same silicon die, together with an onboard Digital Motion Processor™ (DMP™), which processes complex 6-axis MotionFusion algorithms. The device can access external magnetometers or other sensors through an auxiliary master I²C bus, allowing the devices to gather a full set of sensor data without intervention from the system processor.
For precision tracking of both fast and slow motions, the parts feature a user-programmable gyro full-scale range of ±250, ±500, ±1000, and ±2000 °/sec (dps), and a user-programmable accelerometer full-scale range of ±2g, ±4g, ±8g, and ±16g. Additional features include an embedded temperature sensor and an on-chip oscillator with ±1% variation over the operating temperature range.
I bought a module called the GY511 – link later – which contained this sensor, a bmp180 and a HMC5883L sensor in one
Layout
Code
Here is a c++ example that uses the wiringPi library from https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/ . Save this as mpu6050a.c
[codesyntax lang=”cpp”]
#include <wiringPiI2C.h> #include <wiringPi.h> #include <stdio.h> #include <math.h> int fd; int acclX, acclY, acclZ; int gyroX, gyroY, gyroZ; double acclX_scaled, acclY_scaled, acclZ_scaled; double gyroX_scaled, gyroY_scaled, gyroZ_scaled; int read_word_2c(int addr) { int val; val = wiringPiI2CReadReg8(fd, addr); val = val << 8; val += wiringPiI2CReadReg8(fd, addr+1); if (val >= 0x8000) val = -(65536 - val); return val; } double dist(double a, double b) { return sqrt((a*a) + (b*b)); } double get_y_rotation(double x, double y, double z) { double radians; radians = atan2(x, dist(y, z)); return -(radians * (180.0 / M_PI)); } double get_x_rotation(double x, double y, double z) { double radians; radians = atan2(y, dist(x, z)); return (radians * (180.0 / M_PI)); } int main() { fd = wiringPiI2CSetup (0x68); wiringPiI2CWriteReg8 (fd,0x6B,0x00);//disable sleep mode printf("set 0x6B=%X\n",wiringPiI2CReadReg8 (fd,0x6B)); while(1) { acclX = read_word_2c(0x3B); acclY = read_word_2c(0x3D); acclZ = read_word_2c(0x3F); acclX_scaled = acclX / 16384.0; acclY_scaled = acclY / 16384.0; acclZ_scaled = acclZ / 16384.0; printf("My acclX_scaled: %f\n", acclX_scaled); printf("My acclY_scaled: %f\n", acclY_scaled); printf("My acclZ_scaled: %f\n", acclZ_scaled); printf("My X rotation: %f\n", get_x_rotation(acclX_scaled, acclY_scaled, acclZ_scaled)); printf("My Y rotation: %f\n", get_y_rotation(acclX_scaled, acclY_scaled, acclZ_scaled)); delay(100); } return 0; }
[/codesyntax]
Testing
compile the code by running this on the command line
gcc -o mpu6050a mpu6050a.c -lwiringPi -lm
Links
GY-511 LSM303DLHC Module E-Compass 3 Axis Accelerometer + 3 Axis Magnetometer Module Sensor