Go Down

Topic: Guide to gyro and accelerometer with Arduino including Kalman filtering (Read 776241 times) previous topic - next topic


Have you remembered to connect 3.3 Volt to the VREF pin on the Arduino? :)


I used this guide: http://wiring.org.co/learning/basics/gyrolisy300al.html
I connected the VCC on the gyro to the Arduino 3.3v pin.

Is that what you ment ?


No you have to connect 3.3V to the VREF/AREF pin on the Arduino.



Hey, thanks alot for you replays i learn alot of new stuff and you are being very helpfull :)

So in my Gyro i have VCC & GND pin, i connected the VCC to the Arduino 3.3v pin and the GND to one of the GND pins on the Arduino.
And i connected the OUT pin of the gyro to the Analog0 pin on the Arduino.

I use this external Power Supply

As you can see i'm giving 12v to the Arduino, and the Fixed volts are set to 3.3v and connected stright to the AREF pin and one of the GND pins on the arduino.

Now i changed my sensitivity back to 1.023, and added
Code: [Select]
analogReference(EXTERNAL); to the
Code: [Select]
setup() in my code.

You can see the purple black and red goes to the gyro, the big connector that connected to the 7,8,9 digitals is a Servo controller, and the  2 pins from the external supply to the AREF & GND.

Here is the code, every one milliseconds i call this:
Code: [Select]
void gyroTest()
   if(gyroInit) {
     finalGyroAngle = calculateGyroAngle(0);

Code: [Select]
float calculateGyroAngle(int pin)
   gyroAdc = calibrateGyro(0);
   if(abs(gyroAdc - gyroZero) <= Threshold)
     gyroAdc = gyroZero;
   gyroRate = (gyroAdc-gyroZero)/sensitivity;
   unsigned long t = millis();
   dtime = t - stime;
   stime = t;
   return gyroRotation;

void InitGyro(int pin)
 Serial.println("Calibrating Gyro...");
 sensitivity = 1.023; //0.692454545; //0.716454545;
 gyroAdc = 0;
 gyroRate = 0;
 gyroRotation = 0;
 gyroAngle = 0;
 gyroZero = calibrateGyro(pin);
 Serial.print("Gyro Calibrated: ");

int calibrateGyro(int pin)
 long resultGyro = 0;
 for(int i=0;i<100;i++)
   resultGyro += analogRead(pin);
 resultGyro = resultGyro/100;
 return resultGyro;

Am i doing everything ok ?


Glad to see you figured it out :)
What are you going to use the setup for? Controlling the servo or something like that?



Hey, i still didn't solve it :P

I'll explain why the setup for and what is my problem.
As you see i have a WiFi shield connected, i send him commands to spin the servo that is connected to a arm that the gyro is on so the gyro spins with it.

I want to tell the servo to spin 90 degrees, and then go back to 0 degrees, and do that in a loop.
That's why i need the Gyro for, to get the degrees.

My problem is that after 2-3 time it goes to 90 then 0 the drifting goes crazy and it goes to 110 then -20...
So my problam still isn't solved.

I posted all my setup so you can see if i'm doing everything right, i hope you can help me figure it out.


Sorry, I didn't see the last line in your post :)

Hmm, it seams like you do everything correct.

But hey, why do you need a gyro to tell the degrees? You don't need that for a servo. Just send it the correct pulse, and it will go to the requested position!!

See this page for more information: http://www.arduino.cc/en/Reference/Servo


Hey :)

I have a continues Servo... and after doing all of this, my gyro is still like 95% accurate ...
i need 99% atleast..

I just think i'm gonna get an Absolut Angle sensor and connect it to the servo..

Thanks for the help anyway, you were very nice and helpfull :)


Why not just use a ordinary Servo then?
Thank you for the kinds words :)




I am using an Arduino UNO and a Sparkfun 6 DOF Razor IMU.  I am hoping to adapt it to control roll and pitch for a quadrotor stabilization system.  I used your post as a guide to setup the wiring and used the version 3 code to get it going.  My problem is I get about 2-3 lines of actual data and then it just scrolls jibberish.  I double checked the wiring and I have not changed anything in the code.  Could it be that I am using the http://www.sparkfun.com/products/10010 IMU with the high pass filters removed.

I would like to apologize in advance, I have a lot of RC experience and a mechanical engineering background but programming and debug has never been my forte.

All I want to do is read in the roll and pitch degrees and then convert that to a servo command to send to a brushless motor as an increase in thrust to compensate.

Thanks for any help you can provide.



It is exactly the same IMU as mine. So it should be almost plug n' play. Triple check your wires, or try resetting the high pass filter, by pulling the HP pin high (see the datasheet page 7 http://www.sparkfun.com/datasheets/Sensors/IMU/lpr530al.pdf). Not sure if it would work, as the description says that it it removed, as you said.

You do not have to apologize :) We have all been noobs once :D



I also have the board with sensors removed and the program works great, if you say that you can read a few lines and then it just scrolls jibberish it might be the baud rate. 98% of the time that i see just jibberish on the Serial monitor is because the baud rate in it is not the same as the one on the program check that the baud rate on the script is the same as in the serial monitor (bottom right). If that doesnt work then do as Lauszus says check your wires, maybe download the program again and make sure not to erase anything important on it. If that doesnt work then im sorry to say that your board might be damaged (its painful i know :( ...) good luck


Thank You Cristo!  I had the baud rate set at 9600 baud (default setting I assume as this is the first time I have used the program) and when I changed it to 115,200 baud it worked!!

Thank you Lauszus for the quick reply.  Now for my next task, creating the control function to provide stabilization for a quadcopter.... does anybody have a good starting point now that my IMU is working?

Thanks again!



In fact im doing the exact same thing, although i gotta warn you this sensor might not be the best choise it is too sensitive to vibrations and its not the fastest one there is but if you wana try it join the club lol, also i think is better to use DCM rather than Kalman unless you use expanded Kalman or something like that so you can go the 360. I hope you are taking someone elses program Aeroquad or Multiwiii as your program creating your own takes a long long time and it will never work as good as their programs. I had to do it since it is a project for school but if you like programing and that sort of stuff try using one of those programs which are already working and just add some crazy stuff to make it automated like a GPS where you can set the route or stuff like that.

If you still feel like doing this then i can send you as many links as you want the first one is a DCM code for the same board we are using although i dont know why but i havent been able to make it work maybe you have more luck with it: http://voidbot.net/razor-6dof.html if you have any luck let me know.

Also if you want to learn the basics on all the stuff you need to create the program take a look at my posts:



The DIY is better since there is a guy that answered all my questions.

Good luck and i hope you are making this with someone else otherwise is a lot of work  If you are doing this as a hobby dont try it just use the programs there are you will enjoy it much more and save a lot of money building structures...


No, I would love to use existing program. I am not a good enough programmer to make one myself.  I looked at the Aeroquad but it doesn't work for my board (Ardunio Uno).  I know this won't be the best setup but right now I am just looking to make a basic IMU with Roll and Pitch stabilization.  I can add better functionality later.  Just want to play with this in my backyard maybe with an on board camera or something.


I remember those days when i thought it was that simple, i have 3 weeks to make this quadcopter fly and right know it only hovers i have a good idea of what might help it though. I hope ill get it this weekend.

The Aeroquad can use the UNO it just uses less sensors (accelerometer and  gyroscope) just buy whatever they say and you will have it working
In there you see that there are 3 different configurations one with the mega one with the uno and the other with the mini just follow the instructions and be careful.

If you dont want to use the aeroquad code just use the multiwii thats what almost everyone in RCgroups use also they have a nice one where you can use the wii control sensors could make this really cheap.

But if you want to be certain that it will work i would recomend aeroquad

PS i havent tried any of those programs personally, it would brake my heart to see my quad fly with another program.

Go Up