My first 6 DOF IMU Sensors Fusion Implementation: ADXL345, ITG3200, Arduino and Processing

Last updated on Mon, 2012-07-23 09:09. Originally submitted by fabio on 2010-11-22 21:36.

When you have created two breakout boards for the ADXL345 accelerometer and the ITG3200 gyroscope and you have those two nice sensors what you can do? Simple: create an implementation of an Attitude sensor fusion which runs with those chips!

So, I started documenting myself and reading lot of stuff on IMUs (inertial measurement units - composed by an accelerometer and a gyroscope) and MARG sensor (an IMU which also has a magnetometer to sense heading).

One really good article you should read to get started with IMUs is A Guide To using IMU (Accelerometer and Gyroscope Devices) in Embedded Applications available on http://www.starlino.com/ . Also from the same website there is a nice implementation of the theory from the IMU guide: you can find it on Arduino code for simplified Kalman filter. Using a 5DOF IMU.

So, I took the algorithm above and converted it to be used with the ADXL345 and the ITG3200. I came out with two implementations both with parts in Arduino and Processing code (see the attachments to this page below).

IMPORTANT: If you use the IMU Digital Combo Board from Sparkfun you will need to change the address of the Gyroscope from 0x69 in my code to 0x68.

The first implementation simply reads the raw accelerometer and gyroscope values on the Arduino while the Processing code (running on the PC) will compute the sensor fusion algorithm, produce the orientation vector and siplay a nicely oriented cube.

In other implementation all the sensor fusion logic is implemented in Arduino code so it will run embedded in the microcontroller. Looks like Arduino can coupe with that without any problems! So, once the orientation is computed is all sent to the PC where the Processing application will display incoming data and the oriented cube.

You can see a demonstration in the following video:

All the code is available in the attachments below. You can see the circuit in the picture below. Note that these are custom made breakout boards, your pin configurations will vary depending on the schematics of your breakout board. If you have questions just leave a comment below.

The processing programs need to be executed on the Processing IDE, available for download from http://processing.org/
Remember to adjust the serial port configuration in the Processing code to match your connection to the Arduino in your system. Here I use /dev/ttyUSB9, if you are under windows you'll probably have to use something like COM3 or something like that. See in the Arduino IDE under Tools->Serial Port to get the exact value you have to use.

IMU6DOF_Arduino_connections.jpg

AttachmentSize
Arduino Code IMU 6DOF - ADXL345 ITG3200 on chip filter15.03 KB
Arduino Code IMU 6DOF ADXL345 ITG320013.64 KB
Processing Code 37.56 KB
Processing code for on chip version38.77 KB

Both on one chip

Submitted by Dr. Mostafa Hajian (not verified) on Fri, 2014-10-17 12:19.

Dear Fabio,

Thank you for all your efforts.

Examining your setup to measure the data, I notice that you are using two chips. I am using “6 Degrees of Freedom IMU Digital Combo Board - ITG3200/ADXL345” from sparks where both sensors are integrated in one PCB. See the following website:

https://www.sparkfun.com/products/10121

As I run your program I do not get any data as I can imagine.

Could you please tell me how should I change your Arduino program to still get the data?
Note as you have mentioned on your blog to change the address from 0x69 to 0x68, does not work either.

Regards
Mostafa

Extremely educational post!

Submitted by Marion46 (not verified) on Mon, 2014-10-27 22:45.

Extremely educational post! There is a great deal of data here that can help any business begin with a fruitful informal communication fight!
lglldzmwn

Download files

Submitted by Richard (not verified) on Sun, 2014-03-02 03:57.

First I want to thank you for all your geat work and for sharing with all of us.

Second, I am having a problem getting this to run. The files for Processing and Ardino appear to be th same files. I dowloaded and unarchived all your files. I am rying to use the "on chip" version but the files you have listed for Arduino have .pde not .ino and they look identical to the Processing file.

On the processing side I get this error

javax.media.opengl.GLException: Unable to set pixel format 3 for device context

And have not been able to run your progam. I will keep working on it but can you vrify the archive files above to download your code are correct?

Thank you
Richard

Opening processing FIle

Submitted by Zeeshan Haider (not verified) on Mon, 2014-03-03 11:12.

Just rename the downloaded .PDE files to open them in processing software

Downloading Code

Submitted by Anonymous (not verified) on Fri, 2013-11-15 20:59.

Hello

I am struggling to open your code in the Arduino software. PLease can you advise how it should be opened.

Many thanks

James

pls help with arduino code

Submitted by reaper (not verified) on Mon, 2013-09-23 17:37.

Pls, Can anyone help me ,in How to arrange IMU code along with motor's and quadcopter's code..in arduino.
if possible ,provide whole code for quadcopter..

any help is greatly appreciated...

Difficulty opening file

Submitted by Anonymous (not verified) on Mon, 2013-09-23 10:42.

Hi everyone, I am having difficulty opening the files which seems kinda weird since so many people were successful.

After unzipping the folders with Winzip, the extensions of the unzipped files are .tar_ which neither my arduino nor processing can open it. The only file that can be opened successfully is the Processing code for on chip version.

Anyone can lend a helping hand?

Gimbal-lock

Submitted by Anonymous (not verified) on Sun, 2013-09-22 15:58.

Hi Fabio,
I've tried this shield, that should have the same sensor you used in this post.
It seemed to work pretty well, but I discovered that there is the gimbal-lock problem. You can see here in action at the end of this video. Have you experienced/solved this problem? Thanks a lot

Sorry, this is the video:

Submitted by Anonymous (not verified) on Sun, 2013-09-22 15:59.

Sorry, this is the video: https://vimeo.com/75144307

very good guide how to use

Submitted by Anonymous (not verified) on Tue, 2013-07-16 19:59.

very good guide how to use IMU, gyro and accelerometer sensors. I add this tutorial on my article where a long list with sensors and tutorials about how to interface and programming accelerometer, gyro and IMU sensors http://www.intorobotics.com/accelerometer-gyroscope-and-imu-sensors-tuto...

I`m trying to use this

Submitted by Przemek (not verified) on Wed, 2013-05-22 16:34.

I`m trying to use this code(Arduino Code IMU 6DOF - ADXL345 ITG3200 on chip filter), but on processing cube turns just randomly. Had any of you experienced such a issues?
thank you for help;)

Oriented cube

Submitted by pablopaolus (not verified) on Wed, 2013-05-22 12:15.

Hello,

First of all, thank you for sharing your great work. I'm developing an IMU using a PIC along with Sparkfun IMU - 6DOF ITG3200/ADXL345. I've succesfully ported your code and I'd like to display an oriented cube like yours but using Labview, instead of Processing... Any idea of how to do it starting from RwEst values? I'm not able to understand how you do it in Processing, but I'm looking for something similar.

Thank you very much.

Accelerometer sensitivity

Submitted by Anonymous (not verified) on Sun, 2013-04-21 01:32.

for accelerometer algorithm is

R_x/y/z = (AdcR_x/y/z * Vref / 1023 – VzeroG) / Sensitivity

I am having problem with value of sensitivity(mv/g).for +/-2g value is 478.5mV/g.But what is the value for +/-4g,+/-8g,+/-16g.I dont event know how to calculate accelerometer sensitivity in mv/g.can you please help.

Calibration

Submitted by Mark (not verified) on Tue, 2013-04-16 22:29.

Hi Fabio,

I'm trying to use your code and for some reason when I update the offsets for the breakout board I have nothing changes. I set the offsets to zero and oriented it with z facing downwards, had a look at the serial monitor, and then noted the offsets so I could put them in and get zero for x and y when it's in this orientation. They don't read zero, but read the same number that they were when the offsets were zero. I saved after the update and uploaded the code again, so I don't know what I'm missing.

I just saw that those offsets

Submitted by Mark (not verified) on Tue, 2013-04-16 22:40.

I just saw that those offsets are for the gyroscope. I corrected that and now I'm just going to add in my own terms to calibrate the accelerometer.

Accelerometer data

Submitted by Anonymous (not verified) on Thu, 2012-12-27 16:30.

Hi Fabio

Very nice guide btw!

I am using the combined IMU Board in combination with an Arduino Pro Mini and tried to run your programms on it.
The problem is that i dont get data from the accelerometer, whereas the gyroscope sends different values and seems ok. The serial monitor just shows me -512,-512,511 as accelerometer data and does not change. Do you have an idea what the reason for this could be? Is it possible that only the accelerometer is broken?

Hi there, I have the 6 DOF

Submitted by Anonymous (not verified) on Wed, 2012-11-14 22:08.

Hi there,
I have the 6 DOF Combo Board from Sparkfun. Occasionally the ITG3200 stops working. Any idea why or anybody with the same problem?

Hi, i am trying to use this

Submitted by Juan David Escobar (not verified) on Mon, 2012-11-05 20:16.

Hi, i am trying to use this code for my imu3000 of 6 DOF that gives data by i2c to my arduino uno r3, I have a code that gives me each data but I don’t know what do I have to do to change In your code so I can see it in the simulation of processing? And I install processing but I don’t know what do I have to open with all the folders that you attach

and what version of arduino

Submitted by Juan David Escobar (not verified) on Mon, 2012-11-05 23:23.

and what version of arduino software are you using?

How can I used it in a dspic30f

Submitted by Juan David (not verified) on Mon, 2012-10-29 00:33.

Hello I am using and imu-3000 that outputs data of 3axis gyro and 3 accel with i2c I want to ask you if is more harder to use this imu with a dsp30f4011 and programmed with the software mikroC

Hi, I'm not familiar with

Submitted by fabio on Mon, 2012-10-29 09:23.

Hi, I'm not familiar with that development environment, so I don't really have an idea.

Quaternion

Submitted by Arvind Sanjeev (not verified) on Tue, 2012-10-16 19:48.

Hello, I am a big fan, I just wanted to know whether you are using the quaternion method for state estimation in your code, if so in which part? Is it at the part where you combine the accelerometer and gyroscope values?

No, this version of the code

Submitted by fabio on Tue, 2012-10-16 20:48.

No, this version of the code doesn't use the quaternion approach. You can have a look at the FreeIMU library if you are interested in that.

Using two IMUs

Submitted by Sergio (not verified) on Thu, 2012-10-11 10:55.

Dear Fabio.

Thank you very much for this post, it´s really useful.
I´m working on a bit more complex kalman filter, by using different sensors: 2 IMU and an inclinometer....
I´m using two IMU Digital Combo Board from Sparkfun, but actually I only need to use the Gyroscopes of each IMU.

The problem I have is that I don´t know which pin of the Arduino I have to plug the SCL and SDA from the IMU.

As you say, I need to change the address of the Gyroscope from 0x69 to 0x68.Regarding the inputs to Arduino, the SDA and SCL are plug to A4 and A5 respectively. This works if I use only 1 IMU.

But if I need two IMUs:

what is the address of the Gyroscope for the second IMU?
which inputs of Arduino I have to connect the SCL and SDA?
where can I define these inputs?

Thank you very much,

Sergio

In order to connect two

Submitted by fabio on Thu, 2012-10-11 12:17.

In order to connect two ITG3200 on the same I2C bus you need to modify the AD0 pin on the ITG3200. Unfortunately, the breakout board you are using doesn't have configurable address for the ITG3200.

An option may be to use an I2C multiplexer or using an IMU which has configurable sensor address, my FreeIMU offers this possibility as you have pads on the bottom which allows to change addresses of the sensors.

calibration.

Submitted by mandark (not verified) on Fri, 2012-09-21 11:01.

Hello Fabio...how can i get zero reading on accelerometer raw values when its flat...and zer0 for gyro when not moving..? Do you have a the code for calibrating the sensors zer0..? Thank you.

How.?

Submitted by Mandark (not verified) on Tue, 2012-09-25 05:13.

How can i use these file extensions... The .py,.m,.ui etc..! Shame on me.

Please kindly help

Submitted by DavidS (not verified) on Tue, 2012-08-28 06:16.

Hello Everyone.

I'm very much a newbie to all of this. And I'm having a bit of difficulty -

I've uploaded the sketch successfully to my Arduino Uno. I've installed Processing and hit Run of the pde. I don't see a square, neither do I see any movement.

I looked at the serial readout from the Arduino directly and all I'm seeing is a lot of strange characters in succession. What am I possibly doing wrong?

Thank you so much for your kind help!

Have you modified the

Submitted by fabio on Thu, 2012-08-30 12:10.

Have you modified the Processing code so that it uses the correct Serial interface?

Thank you for the reply

Submitted by DavidS (not verified) on Thu, 2012-08-30 22:32.

Thank you for the reply Fabio!

I have - and I'm receiving this error: readBytesUntil() byte buffer is too small for the 358 bytes up to and including char 10

Accelerometer calibration

Submitted by Alessandro (not verified) on Thu, 2012-08-09 00:37.

Hi Fabio,
thank you for providing these amazing posts and for sharing your knowledge!
The following question could be a bit off-topicm I don't know...
I found one of your post in the page http://code.google.com/p/adxl345driver/issues/detail?id=1 in which you link a document concerning accelerometer calibration
http://www.st.com/stonline/products/literature/an/17353.pdf.
Because the link is no more available, can you send me that document?
Thanks

Alessandro

We implemented it in

Submitted by fabio on Thu, 2012-08-09 14:42.

We implemented it in http://www.varesano.net/blog/fabio/drift-free-attitude-and-heading-estim...

That calibration datasheet can be found googling for "AN3192".

Hi, So i got the sparkfun

Submitted by Sep (not verified) on Mon, 2012-07-23 08:47.

Hi, So i got the sparkfun version and I uploaded the files with chip filter, where serial screen gives me hex numbers, and with processing, it looks like your second part of video, it shakes too much, first part was pretty smooth, so I thought to use the file that doesnt have chip filter, but i ran into Sprintf problem down there! can you help me! thank you

I just updated the On Chip

Submitted by fabio on Mon, 2012-07-23 09:20.

I just updated the On Chip version with a fixed code for Processing which should give much better results. Please try that.

What sprintf problems are you having?

I resolved that problem, so

Submitted by Sep (not verified) on Thu, 2012-07-26 07:33.

I resolved that problem, so I'm going to put this in an airplane, I tried the on chip, which both are at 19200 baud, so when i checked the RwAcc is the same at RwEst, meaning the gyro and accelerometer together are still giving the same result, and it works fine but when i shake it, it is not stable like yours, it does weird stuff. You're look really stable! what should I do because I feel like I'm a little lost!

Thank you very much for the reply!

If you see the older

Submitted by fabio on Thu, 2012-07-26 08:43.

If you see the older comments, you'll see that this was a bug in the code which had RwEST=RwACC .. the bug should be fixed now in the new code linked above. Please try it.

So i downloaded both the

Submitted by Sep (not verified) on Fri, 2012-07-27 08:05.

So i downloaded both the processing and arduino on chip code again, and when i run it, its stable when i shake it, unlike before but its kinda slow and unresponsive, as in if i tilt the sensor a little, pitching, the cube will do a roll first and then go to the angle of the pitch the sensor is at!

Help about using processing

Submitted by Mahmoud (not verified) on Fri, 2012-07-13 14:47.

Hi Fabio, thanks for this great blog. Actually I face a problem using processing. does proccessing use the same language of arduino which is (c) as I wrote the code using Arduino and can't implement it in processing. I'd be very grateful if u could help me

The Processing code needs to

Submitted by fabio on Fri, 2012-07-13 15:36.

The Processing code needs to be executed in the Processing IDE which you have to download from http://www.processing.org/

Dear Fabio I managed to get a

Submitted by HK (not verified) on Thu, 2012-07-12 10:25.

Dear Fabio

I managed to get a FreeIMU sensor.

I managed to upload the code successfully using arduino UNO.

However, I wonder why there are no readings in serial monitor?

Is it because of the sensor problem or my connection is loose?

Hi thanks for getting a

Submitted by fabio on Thu, 2012-07-12 13:44.

Hi thanks for getting a FreeIMU! If you have a v0.4.3 board, you don't have to use the code in this page. You have to use the FreeIMU library.

Strange peak

Submitted by Nessios (not verified) on Thu, 2012-07-12 09:07.

Hi Fabio,
I'm working with an ITG3005 and ADXL345, got them working together on an Arduino (my own libs), combined the values in a Kalman filter ... so far so good, all data ok, but ... every x seconds, randomly, i get a gyro peak, even when the board is steady.

Did you ever experience that ?

Great work by the way, you got me motivated to develop my own quadcopter ;)

No, I never experienced

Submitted by fabio on Thu, 2012-07-12 09:50.

No, I never experienced that.. I would suggest checking for stable power to the sensors and for overflow in the sensor values reading.

Good luck!

Gyro data zero position offset correction

Submitted by Ernie Prevost (not verified) on Mon, 2012-07-09 22:07.

Hi Fabio,

I have been doing a bit more work with your program, converting it to work on the Beagleboard & Raspberry Pi and I noticed another part of the code I would like to ask you about. In the code for the ‘on chip filter’ version, in the subroutine ‘getGyroscopeData’ the 3 lines of code:

result[0] = ((buff[2] << 8) | buff[3]) + g_offx;
result[1] = ((buff[4] << 8) | buff[5]) + g_offy;
result[2] = ((buff[6] << 8) | buff[7]) + g_offz;

should the g_offx, g_offy, g_offz values be added (as here) or subtracted?

Ernie

Code Please

Submitted by Mihai (not verified) on Sun, 2013-03-03 12:39.

Hi Ernie,

Is it possible to share with us the code for linux, it would greatly help newbies like me :D.

That really depends on your

Submitted by fabio on Tue, 2012-07-10 10:26.

That really depends on your gyroscope chip. Ideally, when offsets get summed with the raw values, you should get near zero readings when the gyro is held still.

Thanks Fabio I take your

Submitted by Ernie Prevost (not verified) on Tue, 2012-07-10 12:53.

Thanks Fabio

I take your point.

Ernie

Estimated values from the Arduino

Submitted by Ernie (not verified) on Thu, 2012-06-28 22:17.

Hi Fabio,

This is a very useful blog. I have used your code with some success, but I have one question about the process code for the ‘on chip filter’ version. When it reads the data from the Arduino it reads the estimated values using the following code:

RwEst[0] = decodeFloat(inputStringArr[0]);
RwEst[1] = decodeFloat(inputStringArr[1]);
RwEst[2] = decodeFloat(inputStringArr[2]);

Should the input string not be from indexes 11, 12 & 13? The above code will set the same values as RwAcc[x].

Thanks Ernie

Hey Ernie! Nice catch! I

Submitted by fabio on Fri, 2012-06-29 07:47.

Hey Ernie! Nice catch! I wonder how nobody noticed this before?

Are you able to test and provide a fixed code? I don't currently have any ADXL345/ITG3200 board around..

Thanks!

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a personal or company website insert its address in the form http://www.example.com/ .
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <small> <del> <img> <h2> <h3> <h4> <b> <video> <sub> <sup>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • You may insert videos with [video:URL]
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options