IMUs (inertial measurement unit) are useful to many projects such as self-balancing robots and quadcopters. As part of the quadcopter project I will be sharing with you how I connect and use a 10DOF GY80 Arduino sensor, a popular Chinese made IMU. This sensor uses I2C connection with the Arduino. There are four sensors on this board: a gyroscope (L3G4200D), an accelerometer (ADXL345), a Magnetometer (HMC5883L) and a Barometer & Temperature sensor (BMP085). Like I said, this IMU is so popular, there are tons of documents and articles on the internet about it.
In this post I will be mainly playing around with the accelerometer, and will cover other sensors on the GY80 later in another post. I will be using the Arduino Uno as an example, it would be similar with other boards.
How to connect the GY80 Arduino IMU
How to get accelerometer data from the GY80 IMU
There are many people have been writing Arduino codes for this IMU, and there are even libraries available for these sensors. You can find the accelerometer ADXL345 library here. This is an example code how to use the ADXL345 library. It takes sensor measurements, calculate a human friendly value (in this case roll and pitch values) and output them to serial port.
const float alpha = 0.5;
double fXg = 0;
double fYg = 0;
double fZg = 0;
double pitch, roll, Xg, Yg, Zg;
acc.read(&Xg, &Yg, &Zg);
//Low Pass Filter to smooth out data
fXg = Xg * alpha + (fXg * (1.0 – alpha));
fYg = Yg * alpha + (fYg * (1.0 – alpha));
fZg = Zg * alpha + (fZg * (1.0 – alpha));
//Roll and Pitch Equations
roll = (atan2(-fYg, fZg)*180.0)/M_PI;
pitch = (atan2(fXg, sqrt(fYg*fYg + fZg*fZg))*180.0)/M_PI;
Data Visualization – Is the data correct?
Once we are getting data from this sensor, we can visualize it by using a 3D model. This is an example I have done. The program I used are written in Processing.
You might notice when I place the sensor horizontally leveled, the cube doesn’t actually stay that way. I don’t know if this is a hardware fault, I might try and add an offset to it. Also when I turn it to certain point, the cube seems to go randomly to other directions. As you can see, the denominator of the pitch equation is always positive, therefore you can only get outputs between [-90 to 90] degree range. But the roll equation provides [-180 to 180] degree range. It is important to take into account that when the pitch angle is 90 degree, the roll axis is aligned with the gravity vector, so the roll angle cannot be measured anymore. This problem is called Gimbal Lock.
I will talk about this a bit more and how to solve this in later posts.