Refresh

This website oscarliang.com/use-gy80-arduino-adxl345-accelerometer/ is currently offline. Cloudflare's Always Online™ shows a snapshot of this web page from the Internet Archive's Wayback Machine. To check for the live version, click Refresh.

How to use GY80 Arduino – ADXL345 Accelerometer

by Oscar

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.

Some of the links on this page are affiliate links. I receive a commission (at no extra cost to you) if you make a purchase after clicking on one of these affiliate links. This helps support the free content for the community on this website. Please read our Affiliate Link Policy for more information.

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.

Leave a Comment

By using this form, you agree with the storage and handling of your data by this website. Note that all comments are held for moderation before appearing.

14 comments

Edward Kimble 2nd April 2016 - 10:42 pm

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
Zoltan 25th January 2016 - 9:27 pm

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
DHYANA 22nd December 2015 - 8:07 am

Hello
My accelerometer values are not changing… I dont know wat to do.. Please do help me!!!!

Reply
Dan 10th September 2015 - 4:44 am

Can you share the processing program?
Thanks,
Dan

Reply
ravazz 12th March 2015 - 5:59 pm

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
Marcus Amancio 7th May 2015 - 7:33 pm

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
udara herath 10th March 2015 - 5:06 pm

How to use Processing software. Can you please help me?

Reply
Oscar 10th March 2015 - 8:17 pm

google tutorials mate.

Reply
abdul rehmam 2nd March 2015 - 11:06 am

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
Walter 19th November 2014 - 3:14 am

Any pull-up resistors required with this board?

Reply
JHON 22nd September 2014 - 1:18 am

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
Oscar 22nd September 2014 - 2:05 pm

they are all connected to the Arduino using i2c. The arduino reads inputs from them depends on their address.

Reply
abhishek chowdhury 4th July 2014 - 11:29 am

while compiling this code i got this error… “class ADXL345 has no member named begin”

Reply
Dana 27th July 2014 - 6:55 am

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