## 37 comments on “Arduino IMU: Pitch & Roll from an Accelerometer”

1. Thanks. I may need to use this technique some time.

2. Very clear presentation! Just a comment that there is a noticeable delay between you moving the accelerometer and the cube image moving. This could significantly detract from the agility of a robot or game player using this system. Considering that the mechanics processing required is negligible, this leaves the sensor latency, smoothing filter and graphics processor as possible sources of delay. Could this lag be reduced by assuming continuity using something like Taylor series prediction?

• I would have to look into it, but it probably can be done if you assume continuity. For most applications I can think of, it wouldn’t be very useful though.

3. hey buddy.. here’s my contribution, how about calling it imuino, you could use the emu bird as a logo or am I going too far with it?

4. Hi , i have the exact same sensor (you can buy it for 20bucks on dx.com) , but i didnt start to work on it yet , but your tutorial will be really useful, thank you .
i have a question thou ,
can you get SWAY SURGE and HEAVE “like” data , by subtracting the angle from the gyro to data of the accelerometer , and use the result to get translation (or more like the position relative to the starting point), with doing something like x+=x; y+=y; z+=z; (in the loop) with the result ?
? (not sure if i am clear , i am a beginner).
you could use something with DOF in your lib name (Degree Of Freedom) .
Cheers

• Thanks! Well, the gyro doesn’t really give you an angle but an angular rate. Giving that you don’t have translational speed, nor is it calculated anywhere, you can’t really measure translation. What you are asking falls more within the grounds of an Inertial Navigation System (INS), and in itself is very a complicated problem, but an inaccurate approximation could be done by integrating over time the acceleration.

• i am getting confused , (it is quite out of my league but it is so fascinating!)
the accelerometer gives the acceleration (relative to gravity) (if not translating , gives roll and pitch)
the gyro gives the angular rate (=angular velocity=speed of rotation ; different from the acceleration of the rotation ?) (if integrated gives the orientation ?)
but then the magnetometer (understand compass) gives the orientation ,right ?

so all the youtube videos of people doing the Euler thing (orientation tracking) with 6DOF sensor , are basically doing the work of the magnetometer ?

if i want to do the integration over time (extract translation and find position) (solving the shifting with a regular manual reset/recalibration) , i would need to remove the pitch yaw roll from the accelerometer data resulting only in the translation acceleration , right ?

do you want we continue this “talk” on a forum ? i don t want to “spam” your comment space.

• First of all, the accelerometer gives yo the acceleration relative to free-fall, so can actually extract a “gravity” vector from it when at rest, that may have a component on the x, y and z axis. Since the gravity vector es pointing up because it’s not really gravity but the acceleration that counters it, you can only extract tilt from it, i.e. pitch and roll.

The gyroscope gives you the angular rate, which I believe it’s equivalent to angular velocity, certainly not to acceleration. So if you integrate over time you can obtain the orientation, but not quite, it’s something I’ll talk on the next post about the Arduino IMU.

Magnetometers measure magnetic field in each axis, so you can “sense” the earths magnetic field and calculate the orientation relative to the magnetic north (yaw), but there are techniques to calculate pitch and roll also. I will talk about it in future posts.

I don’t really know about the videos you’re talking about, but calculating precise orientation usually requires a combination of sensors.

Regarding the translation measurement, you can extract the gravity vector from the accelerometer readings, so all you have left is the acceleration you need for calculating the translation. But you must remember that integrating over time accumulates a noticeable error and, in this case, you must integrate twice (acceleration->speed, speed->translation).

5. There is a gyro on that board too, mix it with the accelerometer. Combine the outputs with a complementary filter first. Fool with the crossover points while graphing the outputs, adjust until you get an acceptable error from the fastest response you want to perform in. After that, you can start playing with extended kalman filters, they do a great job of mixing the best way possible, but they might run a little slow.

Nice job.

6. Hi everybody, could you please tell me about formula of yaw estimation ?
I mean that, there is written that to get roll from adxl345 is arctan(-Gx/Gz), so what about yaw ?
I hope understand later, thanks.

7. Hi, i have a problem with my sensor, it seems that it only shows one value for pitch and for roll..

This is my output in the Arduino Serial
-34.09:135.67

I only get to print something when SDA and SCL is disconnected, when I connect the wires, the serial stops.

Any idea why? Is my gyro damaged?

I am using an Arduino Mega

• Well, if you could tell us more about what you are doing and using and how it’s all connected…

• I am using the library that you posted here. All I want is to make it work and display the pitch and roll.. it is connected to 5V-GND and the SDA and SCL to pins 20/21 in the Arduino MEGA

8. Nice post! Really liked it! Helped me a lot. Congrats!
Just one thing: isnt the Low-Pass filter equation with a sign error? Shouldnt it be alpha*x + (1-alpha)*y ?

9. Hi found this very interesting I’m going to convert this to python and give it a go in the next couple of days it actually looks pretty simple. Any chance of getting the next part up on the gyro?

10. Yes, I will! I’ve just had two pretty busy months and haven’t had much time. Thanks for following and please be patient.

• Thought i’d post a quick message I implemented your code in python on my Raspberry Pi and am seeing very good results. I do get some odd noise on occasion which will give me a 2g spike (in a +-4g range) on the Z and X axis but never Y! Not sure why. I’m currently writing out to a txt file and then importing the results into excel for graphing. Now i’m happy my code is working ok i’m going to start creating a real time GUI. Not bad for my first bit of programming.

• I’m happy it worked, thanks for writing! I hope in less than a month I’ll be able to write the next step on this project. I’m actually two/three steps ahead, but I don’t find the time!

11. Great job and a great post. Did you ever post your source code for the cube visualization? I’m very interested in doing something similar, so any guidance would be appreciated.

12. This is the perfect site for anyone who would like to understand
this topic. You understand so much its almost tough to argue with you (not that I actually
will need to…HaHa). You definitely put a brand new spin on a
topic which has been written about for years. Wonderful stuff, just great!

13. You have observed very interesting points !
ps nice site. “Formal education will make you a living self-education will make you a fortune.” by Jim Rohn.

14. Hi, Nice project, I have the exact same sensor and want to start playing with it.
I have been to your github and downloaded the zip, I renamed the arduino folder as suggested, opened it in arduino , and it wont compile,
I get the following message :
“pitch_roll:20: error: ‘ADXL345′ does not name a type
pitch_roll.cpp: In function ‘void setup()’:
pitch_roll:24: error: ‘acc’ was not declared in this scope
pitch_roll.cpp: In function ‘void loop()’:
pitch_roll:33: error: ‘acc’ was not declared in this scope

do you know what it going wrong ? from my knolwege of arduino the ACC function is not being recognised, but I have the folder correctly named with all files inside it and in the library folder,

thanks for any help , I look forward to playing with this

• You need to put the renamed arduino directory in your workspace under another directory called libraries. If this directory doesn’t exist you should create it. Then open the example and try again. Cheers.