This guide was first posted at the Arduino forum, and can be found here: http://arduino.cc/forum/index.php/topic,58048.0.html

I just reposted it, so even more will get use of it. The code can be found here:

Hallo everybody
I recently bought this analog 6DOF (six degrees of freedom) IMU board (http://www.sparkfun.com/products/10010) from watterott.com. It uses three gyros and three accelerometers to calculate angles in three dimensions.

I looked a while for some code online and how to connect with them. After many hours of research I succeeded of making af precise measurement of angles in two directions. I decided to write a short guide for fellow electronic enthusiasts.
The main purpose of this guide is to teach others how to get some useful data from their IMU or just a gyro or accelerometer. The code for Arduino can be found at github: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter. It should be pretty easy to implement my code to your own sensor. I will not describe all the details about the theory behind, instead you can look at the sources for more info.

Before you begin you have to connect the IMU as follows:

Acc_Gyro          Arduino
3.3V        <—>   3.3V    
GND        <—>   GND
Gx4 X      <—>   AN0
Gx4 Y      <—>   AN1
Gx4 Z      <—>   AN2
Acc X      <—>    AN3  
Acc Y      <—>    AN4  
Acc Z      <—>    AN5  

Also connect 3.3V to the AREF pin on the Arduino for more accuracy.
It is VERY important that you do not connect the sensor to 5V - this will destroy the sensor.

Now your are ready for reading some data from the sensor.

To communicate with the sensor is straightforward:
The gyro measures degrees per second while the accelerometer measures acceleration (g's) in three dimensions. Both outputs the measurements as a analog signal.
To get these translated into degrees you have to do some coding:

The gyro
First you have to translate quids (a number from 0-1023) into something useful (this is for a ADC with a 10 bit resolution, for example this should be 4095 (2^12-1=4095) for 12 bit ADC). To do this I just use this simple equation:
gyroRate = (gyroAdc-gyroZero)/sensitivity - where gyroAdc are the readed value from our sensor, gyroZero is the value when it is stationary (this is done in the code - look in the "Setup" section) while sensitivity is the sensitivity found in the datasheet, but translated into quids.

If you look in the two gyros datasheets (http://www.sparkfun.com/datasheets/Sensors/IMU/lpr530al.pdf and http://www.sparkfun.com/datasheets/Sensors/IMU/LY530ALH.pdf) you will see that the sensitivity is 3.33mV/deg/s for the 4xOUT. To translate these into quids is pretty easy: sensitivity/3.3*1023.
So in this example I get:

NB: to translate mV to V simple just divide by one thousand.

The final equation will look like this:
gyroRate = (gyroAdc-gryoZero)/1.0323

The result will come out as degrees per second. To translate this into degrees you have to know the exact time since the last loop. Fortunately, the Arduino got a simple command to do so: millis(). By using that, one can calculate the time difference (delta time) and thereby calculate the angle of the gyro. The final equation will look like this:
gyroAngle += gyroRate*dtime/1000

Unfortunately, the gyro drifts over time. That means it can not be trusted for a longer timespan, but it is very precise for a short time. This is when the accelerometer comes in handy. It does not have any drift, but it is too unstable for shorter timespan. I will describe how to combine these measurements in a while, but first I will describe how to translate the readings from the accelerometer into something useful.

The accelerometer
The accelerometer measures the acceleration (g's) in three dimensions. To translate the analog readings into degrees you simply need to read the axis and to subtract the zero offset like so:
accVal = accAdc-accZero

Where accAdc is the analog reading and accZero is the value when it reads 0g - this is calculated in the start of the code, look in the "Setup" section. The zero value can also be found in the datasheet: http://www.sparkfun.com/datasheets/Components/SMD/adxl335.pdf. You will see that the zero voltage at 0g is approximately 1.5V, to translate this into quids, you again have to use this equation: zeroVoltage/3.3*1023.
So in this example I get:

You can then calculate the pitch and roll using the following equations:
pitch = atan2(accYval, accZval)+PI
roll = atan2(accXval, accZval)+PI

Atan2 has a output range from -π to π (see http://en.wikipedia.org/wiki/Atan2), I simply add π, so the range it converted to 0 to 2π.
To convert it from radians to degrees we simply multiply the result by 57.295779513082320876798154814105 - this is predefined in the Arduino IDE as RAD_TO_DEG.

Kalman filter
As I explained earlier the gyro is very precise, but tend to drift. The accelerometer is a bit unstable, but does not drift. You can calculate the precise angle by using something called a Kalman filter. A detailed guide on how it's implemented can be found at my blog: http://blog.tkjelectronics.dk/2012/09/a-practical-approach-to-kalman-filter-and-how-to-implement-it/.

If you want to use something a bit more simple, you can use what's called a Complementary Filter. It is pretty easy to understand and the math is much simpler, because it only works in one step.
For example the equation could look like this:
angle = 0.98 *(angle+gyro*dt) + 0.02*acc - you can fine tune the numbers to whatever you like. Just remember that the sum must be 1.
For me the result from the Complementary Filter was very close (or almost the same) as the one calculated by the Kalman filter.

You have now learned (hopefully) how to get analog data from IMU and translate it to something useful. I have attached my own code for my 6DOF IMU (http://www.sparkfun.com/products/10010), but with some slightly modification, I am pretty sure that it is possible to use it with any analog gyro/accelerometer.

If you have any question, fell free to post a comment below.


I have just finished a Processing code which prints out data from the Arduino on a nice graph. As you can see in the video below the filtering is quit effective. The light blue line is the accelerometer, the purple line is the gyro, the black line is the angle calculated by the Complementary Filter, and the red line is the angle calculated by the Kalman filter. As you might see the Kalman filter is just a bit more precise (i know it is difficult to see in the video) than the Complementary Filter, especially when I shake it.
I have attached my code, both the updated code for the Arduino and the Processing code. It is also possible to see the data from the y-axis. Just uncomment drawAxisY(); in the code.    

Newest firmware
I decided to put all the source code on github, as it is much easier to maintain.
The newest code can now be found at github: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter

My Balancing robot
Below is a video of my balancing robot. It uses the same IMU and algorithm as described in the post above.

I have just released my balancing robot on Kickstarter: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit
Please consider backing the project.

<p>Hello Lauszus,</p><p>This is an excellent article with extraordinarily precise code. That said, I am working on a project that requires data from to gyros to the Arduino simultaneously. Where in the code can change the analog inputs? If there is a better way to do it, i'm all ears. </p><p>Thanks</p>
You can change them in the top of the code here: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/d140efc95d7cc98643f5cdfe9cd543d7328bf28d/IMU/LPR530AL_LY530ALH_ADXL335/LPR530AL_LY530ALH_ADXL335.ino#L23-L29.
<p>Lauszus,</p><p>I appreciate the timely response and I have been looking through the code available for download in this article as well as the one you sent me a few days ago. The code posted on this article, has worked on my arduino and has been compatible with the IMU 9150 I have been using. Here is the hookup I have been using:</p><p>IMU--&gt; Arduino:</p><p>GND--&gt; GND</p><p>VCC--&gt; 3.3V</p><p>SDA--&gt; A4</p><p>SCL--&gt; A5</p><p>So far this setup has been working for the code downloaded from this article and I am able to cleanly receive dependable data from the gyro, accelerometer, and the combined Kalman calculated filter. That said, based off the code you gave me a few days ago, I do not understand how to properly hookup this IMU 9150 for those parameters, or how to alter the code you sent to satisfy the IMU 9150 setup. I was thinking that perhaps there is a way to change the analog input on the code posted in the article, or there is a way to modify the code you sent so it is compatible with the technology I have. If necessary I may be able to purchase the IMU Analog Combo Board Razor, though I am unable to find it available online, but I would like to try to resolve this problem with the equipment I have at this time.</p><p>I appreciate the time and effort you are putting in to help me resolve this problem.</p><p>Thanks, Geoffrey</p>
<p>You should take a look at the following example I have provided for the MPU-6050: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/master/IMU/MPU6050. If you want to use the magnetometer inside the MPU-9150 you should have a look at the following drivers for my flight controller: https://github.com/Lauszus/LaunchPadFlightController/blob/master/src/MPU6500.c and https://github.com/Lauszus/LaunchPadFlightController/blob/master/src/AK8963.c, as the MPU-9150 has a AK8975 connected to it's external I2C pins, which is properly very similar to the MPU-9250 (MPU-6500 with AK8963) which is used on the flight controller.</p>
<p>Lauszus,</p><p>The program for the MPU 6050 is the one I have been using, with the setup I previously mentioned. Is there anyway I can use multiple IMUs (gyro and accelerometer with the kalman filter) using this code? This is why I was wondering how to change the analog input. I want to be able to run multiple IMUs with one program and arduino.</p><p>Thanks for all the help,</p><p>Geoffrey</p>
<p>You can hook up two sensors by connecting AD0 to 3.3V and GND respectively. If you need more sensors than that you will have to use a I2C multiplexer like this one: <a href="https://www.adafruit.com/products/2717" rel="nofollow">https://www.adafruit.com/products/2717</a>.</p><p>Sorry but no analog inputs are used when you use the MPU-6050. I guess you are referring to A4 and A5, right? Remember that almost any analog pin can be used as digital pins as well. The A4 and A5 on the Uno can be used for I2C as well. You should hook up both sensors to these two pins. For more information regarding I2C please see: <a href="https://learn.sparkfun.com/tutorials/i2c" rel="nofollow">https://learn.sparkfun.com/tutorials/i2c</a>.</p><p>Regards</p><p>Kristian Sloth Lauszus</p>
<p>Hi,</p><p>iI'm newbie..my project is about stabilizing...i want to stabilize the motor on moving platform by using 5dofIMU(SN-IMU5DOF-LC) and G15 Cube servo..using gyro..or should i use both gyro and accelerometer..but i'm really new and don't know how to do it..now i just got the reading of gyro and convert it to degree, then i want to combine the IMU and cube servo but don't have any idea how to do it.. can u help me..maybe with source code or how to do it...please help me.. :(</p>
Sorry, but did you read the guide? It explains how to calculate the angle.<br><br>In order to control a servo you can simply use the Arduino servo library: https://www.arduino.cc/en/Reference/Servo.<br><br>Regards<br>Kristian Sloth Lauszus
<p>The best explanation that I have seen online so far.</p><p>Thx for the amazing effort.</p>
<p>Hi I wanted to use the IMU data with camera for vision based navigation,I need help regarding this,and IMU is 9DOF sensor stick from sparkfun and camera is Link sprite JPEG camera</p>
<p>Modeling accelerometer and gyroscope in simulink</p><p>Hi everyone , i'm working on a tracking system project that will localise people inside </p><p>a building during their mouvements using the IMU : inertial measurement unit (gyroscope </p><p>+ accelerometer) , and i have chosen the kalman filter algorithm to read the output of </p><p>the IMU to estimate and update the actual position</p><p>i need if it possible a module in simulink that simulate the gyroscope and accelerometer </p><p>and also how to implement the algorthm using kalman filter</p><p>thanks in advance :)</p>
<p>what part is the gyro i have an wireless remote/keyboard i want to disable the air-mouse gyro in well add a switch really</p>
<p>Hey, I am working on a project called wireless inertial pointer. Its a device just like a wireless mouse which can be used to control the cursor(pointer) on screen. I have used Arduino UNO, MPU 9150 and I am combining my gyro and accelerometer data using complimentary filter.</p><p>Everything is working well but my cursor on the screen is not moving smoothly, can you please help me with the mouse code.</p><p>my code for implementing mouse function is:</p><p>if(compAngleX &gt; 90)</p><p> compAngleX = 90;</p><p> if(compAngleX &lt;-90)</p><p> compAngleX = -90;</p><p> if(compAngleY &gt; 95)</p><p> compAngleY = 95;</p><p> if(compAngleY &lt;-95)</p><p> compAngleY = -95;</p><p> int x = map(compAngleY, -90, 90, -10, 10);</p><p> int y = map(compAngleX, -90, 90, -10, 10);</p><p> Mouse.move(x,y,0);</p>
<p>Hi,</p><p>I am a newbie in this area. How is the accelerometer used for measuring angles when the subject is in dynamic condition. I'm confused about this fact???</p>
Please read the following post I have written:&nbsp;<a href="http://blog.tkjelectronics.dk/2012/09/a-practical-approach-to-kalman-filter-and-how-to-implement-it/" rel="nofollow">http://blog.tkjelectronics.dk/2012/09/a-practical-approach-to-kalman-filter-and-how-to-implement-it/</a>.
<p>Very detailed article and very useful, your post makes me more motivated to do the balancing robot. Thank you very much!</p>
<p>wow.wow .that's really really cool</p>
i used the code given in &quot;IMU6DOFVer2.zip&quot;.. i set the baud rate same with the coding, but the serial monitor display all values zero..<br> <br> if i put the code as you give in link below,<br> <a href="https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/00c0bbfddbc27a302f29f22df8318959aa344435/IMU6DOF/MPU6050/MPU6050.ino#L39" rel="nofollow">https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/00c0bbfddbc27a302f29f22df8318959aa344435/IMU6DOF/MPU6050/MPU6050.ino#L39</a><br> then the error &quot;sketch_nov24a:45: error: 'i2cWrite' was not declared in this scope&quot;<br> <br> now i confused which code i have to use..<br> i am totally zero..
Sorry I got confused and send you the wrong link - that is why you got confused :)<br> <br> The correct is this one:&nbsp;<a href="https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/00c0bbfddbc27a302f29f22df8318959aa344435/IMU6DOF/LPR530AL_LY530ALH_ADXL335" rel="nofollow">https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/00c0bbfddbc27a302f29f22df8318959aa344435/IMU6DOF/LPR530AL_LY530ALH_ADXL335</a>.
i used the code in link above.. the serial monitor now display all value 180 and not change..<br> <br> is the code suitable with my IMU?<br> <a href="http://www.cytron.com.my/viewProduct.php?pcode=SN-IMU5D-LC&name=5%20DOF%20IMU" rel="nofollow">http://www.cytron.com.my/viewProduct.php?pcode=SN-IMU5D-LC&amp;name=5%20DOF%20IMU</a><br> <br> maybe i need to set something there?<br> <br>
Hello Sir! I want to make &quot;9DOF IMU&quot; application: <br>Camera control/stabilisation. <br>By using tri-axis gyroscope,tri-axis accelerometer,tri-axis megnetometer, and interfacing with Real-time control interface x-io board..kindly help please how i can buil this?
You should look up extended Kalman filter and DCM algorithm. Just try to Google &quot;Camera stabilisation Arduino&quot; and there should be plenty of resources.
Are you sure that you have connected everything properly? Simply connect it like it is described in this guide and leave out the pins that is not on your IMU.<br> <br> It must match this pinout:&nbsp;<a href="https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/00c0bbfddbc27a302f29f22df8318959aa344435/IMU6DOF/LPR530AL_LY530ALH_ADXL335/LPR530AL_LY530ALH_ADXL335.ino#L23-L29" rel="nofollow">https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/00c0bbfddbc27a302f29f22df8318959aa344435/IMU6DOF/LPR530AL_LY530ALH_ADXL335/LPR530AL_LY530ALH_ADXL335.ino#L23-L29</a>.<br> <br> Also try to print this value:&nbsp;<a href="https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/00c0bbfddbc27a302f29f22df8318959aa344435/IMU6DOF/LPR530AL_LY530ALH_ADXL335/LPR530AL_LY530ALH_ADXL335.ino#L86" rel="nofollow">https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/00c0bbfddbc27a302f29f22df8318959aa344435/IMU6DOF/LPR530AL_LY530ALH_ADXL335/LPR530AL_LY530ALH_ADXL335.ino#L86</a>.
Ya.. i have connected properly<br> Acc_Gyro Arduino<br> 5V &lt;&mdash;&gt; 5V<br> GND &lt;&mdash;&gt; GND<br> Gx4 X &lt;&mdash;&gt; AN0<br> Gx4 Y &lt;&mdash;&gt; AN1<br> Acc X &lt;&mdash;&gt; AN3<br> Acc Y &lt;&mdash;&gt; AN4<br> Acc Z &lt;&mdash;&gt; AN5<br> <br> when i try to print &quot;accXval&quot; and &quot;accYval&quot;.. they display zero.<br> maybe that is the problem..
Hello Sir! I want to make &quot;9DOF IMU&quot; application: <br>Camera control/stabilisation. <br>By using tri-axis gyroscope,tri-axis accelerometer,tri-axis megnetometer, and interfacing with Real-time control interface x-io board..kindly help please how i can buil this?
ok, i connect <br>Acc_Gyro Arduino <br>5V &lt;&mdash;&gt; 5V <br>GND &lt;&mdash;&gt; GND <br>Gx4 X &lt;&mdash;&gt; AN0 <br>Gx4 Y &lt;&mdash;&gt; AN1 <br>Acc X &lt;&mdash;&gt; AN3 <br>Acc Y &lt;&mdash;&gt; AN4 <br>i dont use z-axis.. <br> <br>i just straight put in your code.. but the serial monitor display zero.. <br>what the problem? do i need to set anything else before upload to arduino?
Have you remembered to set the right baud rate in the serial monitor - take a look at the dropdown menu in the bottom right of the serial monitor.<br> <br> It must match this value:&nbsp;<a href="https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/00c0bbfddbc27a302f29f22df8318959aa344435/IMU6DOF/MPU6050/MPU6050.ino#L39" rel="nofollow">https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/00c0bbfddbc27a302f29f22df8318959aa344435/IMU6DOF/MPU6050/MPU6050.ino#L39</a>.
hello.. help me.<br> how to connect my imu..<br> i have these pins:<br> 1) 5v<br> 2) GND<br> 3) X-acc<br> 4) Y-acc<br> 5) Z-acc<br> 6) X-ratex4.7(inv)<br> 7) Y-ratex4.7(inv)<br> 8) X-rate<br> 9) Y-rate<br> 10) ST-acc<br> <br> <br>
Please send me a link to your IMU. <br>If looks like it is just a regular analog IMU, so simply connect it as described in this guide. <br> <br>Regards <br>Lauszus
www.cytron.com.my/viewProduct.php?pcode=SN-IMU5D-LC&amp;name=5%20DOF%20IMU <br> <br>
Simply just connect it as explained in this guide. Except you should connect 5V to the IMU instead of 3.3V. Also you can not measure yaw using it, as only have a 2-axis gyroscope.
hello <br>first of all thanks for such clear and simple instructions. <br>you wrote that the gyro readings are precise but it drifts away with time, my question is in my project the gyro's orientation will be changing in almost every second to every minute(max), is the drifting problem still an issue for my application i.e. does the value drifts away when the orientation is kept same for long or does it drifts anyway on very long periods of use, and also what is the approximate period for significant drift. <br> <br>thanks in advance!
Hi, The amount of drift you will get depends on your gyroscope, so will have to do some measurement yourself and test it.<br> <br> If the filter is properly tuned then the output from the Kalman filter is very precise.<br> You should have a look at the following blog I wrote: <a href="http://blog.tkjelectronics.dk/2012/09/a-practical-approach-to-kalman-filter-and-how-to-implement-it/" rel="nofollow">http://blog.tkjelectronics.dk/2012/09/a-practical-approach-to-kalman-filter-and-how-to-implement-it/</a>.<br> <br> Regards<br> Lauszus
well i have MPU-6050, and thanks for the blog link!
I have written an example specific for that sensor. You can find it here:&nbsp;<a href="https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/IMU6DOF/MPU6050/MPU6050.ino" rel="nofollow">https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/IMU6DOF/MPU6050/MPU6050.ino</a><br> <br> Regards<br> Lauszus
ok, after calculations, and getting the values, how is it supposed to control the speed of my quad and make it hover...I just can't able to visualize in implementing the gyros and accs... <br>Sorry for my dummines :confused: <br> <br>need fast and better sugestions... <br>
Try to Google PID controllers. That's how I would do it. You can find plenty of existing firmware already online.
i don't y,i'm confused for PID. <br>Are these the steps of PID Tuning? <br>dv=kp*angle+ki*integral+kd*diff; <br> integral+=angle; <br> diff=angle-prev_angle; <br> prev_angle=angle; <br> oc0=(int)dv; <br>
Take look at these lines:&nbsp;<a href="https://github.com/TKJElectronics/Balanduino/blob/8a7955b9a96e24fd50034f70927c5b58e8966d5e/Firmware/Balanduino/PID.ino#L45-L53" rel="nofollow">https://github.com/TKJElectronics/Balanduino/blob/8a7955b9a96e24fd50034f70927c5b58e8966d5e/Firmware/Balanduino/PID.ino#L45-L53</a>&nbsp;I use for my balancing robot.<br> You will need to find the Kp, Ki and Kd values.
Hello sir, <br>once again! i'm here with you. <br>As i told you in my previous conversation, i was trying to make the self balancing robot using AVR platform but some of the problem.My robot was not working properly so again i need your help. <br>I got the PID result on my MATLAB Software but unfortunately,i'm not able to tune the graph of PID so I need your help to tune the parameter of Kp,Kd,Ki of PID to balance the robot. <br>if u know any software to direct tune PID using graph then it will be really helpful for me.
Take a look at the following comment on my blog:&nbsp;<a href="http://blog.tkjelectronics.dk/2012/03/the-balancing-robot/comment-page-1/#comment-20170" rel="nofollow">http://blog.tkjelectronics.dk/2012/03/the-balancing-robot/comment-page-1/#comment-20170</a>.
Hello Sir. First of all, congratulations for the job done (and time spent) to explain step by step your self balancing project. I live in France, so, please, be patient with my Englsh. I don't know if I write this message in the right place, but I'll write it anyway; maybe you'll see it. I intend to built a self balancing scooter. I want to mix the Segway with the Toyota Winglet. I mean 2 big motor-wheels (12&quot; diameter, 1500W each, 48 to 60V batteries with 20 to30Ah) mounted on two leaning half platforms. It will be made for private use only(my wife and me). I allready designed the mechanical part of the thing (no name yet), but the electronics is very new for me, and there I have questions to ask. Two months ago I started to learn programation with Arduino. So, if you think that my project is reliable and you have, from time to time, a few minutes for me, answer this mail, please. Best regards R. DOMIDE
I don't have any experience with balancing scooters, but the principle is the same. Perhaps you should try to build a smaller version like a balancing robot first and then build the large one?<br> Also check out my Kickstarter campaign:&nbsp;<a href="http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit" rel="nofollow">http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit</a>. It's a complete kit with all you need to build a balancing robot.<br>
How would I do this with the arduIMU V3? The pinouts are completely different from the razor IMU.
It uses a digital sensor instead. The accelerometer and gyroscope is the MPU-6000. Check out this code I have written for the MPU-6050:&nbsp;<a href="https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/IMU6DOF/MPU6050/MPU6050.ino" rel="nofollow">https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/blob/master/IMU6DOF/MPU6050/MPU6050.ino</a>.<br> <br> The only difference between the MPU-6000 and the&nbsp;MPU-6050 is that the&nbsp;MPU-6000 supports SPI as well. So you can use my code just fine.
Thanks for your advice but I was able to do it without wheel encoders, putting the IMU at the top portion, the battery at the bottom and with just a PD loop(I could not figure out how to implement the integral part, as it will keep on adding the errors resulting to a maximum dutycycle). It's stable on rough surfaces but not with smooth floors. Although it's about 15 degrees of tilt limit(maybe due to the insufficient speed of the motors and the improper positioning of the components especially on concentrating the weight on the bottom), atleast it can do balancing by itself. <br> <br>Your suggestions really helped!
As I already said. You should put the battery at the top and the IMU at the bottom. <br>You can see how to implement the integral term here: https://github.com/TKJElectronics/BalancingRobotArduino/blob/master/BalancingRobotArduino.ino#L178.
Thanks for your advice but I was able to do it without wheel encoders, putting the IMU at the top portion, the battery at the bottom and with just a PD loop(I could not figure out how to implement the integral part, as it will keep on adding the errors resulting to a maximum dutycycle). It's stable on rough surfaces but not with smooth floors. Although it's about 15 degrees of tilt limit(maybe due to the insufficient speed of the motors and the improper positioning of the components especially on concentrating the weight on the bottom), atleast it can do balancing by itself. <br> <br>Your suggestions really helped!
Thanks for your advice but I was able to do it without wheel encoders, putting the IMU at the top portion, the battery at the bottom and with just a PD loop(I could not figure out how to implement the integral part, as it will keep on adding the errors resulting to a maximum dutycycle). It's stable on rough surfaces but not with smooth floors. Although it's about 15 degrees of tilt limit(maybe due to the insufficient speed of the motors and the improper positioning of the components especially on concentrating the weight on the bottom), atleast it can do balancing by itself. <br> <br>Your suggestions really helped!
I have tried your sketch for the MPU6050 that outputs both kalman and complimentary angles and it worked pretty well. I can see the angles are changing when I move my robot. In my case, the Y-axis is the axis of interest and both of the filters are outputting approximately 180 degrees at standing position (I have to subtract it by 180 to make it 0 degree as the target balanced position angle). But point (0,0,0) of the IMU is not in place or at fixed position, as I have placed it on the upper portion of the robot (I didn't realize it before). Will it add complexities? <br>Now that I can monitor the angle along the Y axis, am I ready to go for the balancing process? Almost all tutorials are using PID controller (I haven't used it before) and in my case I'm not using wheel encoders. Will the PID algorithms still applicable for my L293D motor driver?

About This Instructable


173 favorites


More by Lauszus: Guide to gyro and accelerometer with Arduino including Kalman filtering
Add instructable to: