How to use GY80 Arduino – ADXL345 Accelerometer

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.

GY80_all-sensors-name-position-location 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

You can either use 3.3V or 5V power supply, because I think there is a voltage regulator on board. Connect the CL and XX just like you do with other I2C devices with the Arduino. Gy80-arduino-connection

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.

[sourcecode language=”cpp”]

#include “Wire.h”
#include “ADXL345.h”

const float alpha = 0.5;

double fXg = 0;
double fYg = 0;
double fZg = 0;

ADXL345 acc;

void setup()
{
acc.begin();
Serial.begin(9600);
delay(100);
}

void loop()
{
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;

Serial.print(pitch);
Serial.print(” “);
Serial.println(roll);

delay(50);
}

[/sourcecode]

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.

14 thoughts on “How to use GY80 Arduino – ADXL345 Accelerometer

  1. Edward Kimble

    I’m not exactly sure what is going on with github, but with some searching around, there is a zip file there which has both the cpp and the .h file. I was told those should be stored in the library folder that is in the same folder as your sketches. Then, if there is an update to Arduino.exe and etc. you won’t lose it.

    Reply
  2. Zoltan

    Hi everyone. I’ve just started to make a self balancing robot. To do this i use a gy-80 IMU. But i do not get any data from the sensor and I do not know what is the problem. Please help me! I tried to run the I2C scanner but the program can’t find any I2c devices. What is the problem? Is my imu dead?

    Please answer me or email me! Thanks for your help!

    Reply
  3. ravazz

    hi everyone, i’m having some problems with my accelerometer and the arduino uno: everyone says that i have to use “sensortest” example to see the value of the accelerometer, the problem is that i can’t use “sensortest” because arduino IDE doesn’t read the libraries (#include #include ). i red too many guides but still does not work.
    please help me,
    thanks.

    Reply
    1. Marcus Amancio

      Hi, I just started working with this sensor. If you are still havin trouble meybe I can help you, my skype is hiper1991, bye

      Reply
  4. abdul rehmam

    Oscar bro i want to know that how do you give formula to pitch and roll in the code . i am new. plz help me.
    Thanks in advance.

    Reply
  5. JHON

    Brother , i want to know that if three sensors are present. then how will be they give output? my mean to say that are every sensor having its own terminal? through which we can get different inputs from IMU and give to different bits of controller.
    please help me.

    Reply
    1. Dana

      I am just learning, but think this code is asking for the ADXL345.h library which you will have to google for. It will also have a ADXL345.cpp file that is needed. Make sure that these two files are in one ADXL345 folder thats in your library folder. If you get the same error again while compiling then your sketch still cant find the ADXL345.h file. Be sure to restart your arduino programmer so the programmer knows you added something new to your library. When you get this type of error it usually means are messing something. Dont give up. When you arent making mistakes you arent learning anything new.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you Robot? *

I only check blog comments once or twice a week, if you want a quick reply you can post your question on this forum IntoFPV.com... You might get a faster response from me there (multirotor related only).