General purpose Arduino shield for self-balancing machines.

Why did I make it?
I previously made an Instructable in 2010 on how to build a self-balancing skateboard.

There are >500 comments on this and many express confusion setting up the balance sensors, software and electronics. On top of that, the analog output inertial measurement units that were commonly available stopped being made.

All my self balancing projects are now on my Youtube channel here: Click

Here, I have taken a low price obscure analog IMU that IS currently still made in China, that IS available on ebay, and used an Arduino prototyping "shield" to mount ALL the parts, including a cable to a basic hand-controller (for steering and fine-tuning the balance point) and a cable with just 2 wires that you connect to a 2 x 25Amp "Sabertooth" motor power controller.
I have tried to make it as easy and in particular, non-confusing as possible to build.
NOTE December 2013: Even these are getting rare now but I have just found the "Grove" series of analog sensors from Seedstudio and added contact details to page 6.

In essence a complete re-vamp of the control system, making it simpler to build at the same time.

NOTE (March 2014): I have finally made something similar to this that actually works with a modern DIGITAL IMU from Sparkfun, the 6dof sensor, code No: SEN-10121. It has its own new Instructable here:

NOTE(December 2014): I have also done an Instructable using the same digital Sparkfun IMU in a self-balancing scooter. This instructable is most up to date and has the circuit diagram and latest code here:


I have included the basics of how to connect this to the Sabertooth motor power controller, which is an off the shelf commercial robot power controller, how to power the Sabertooth and how to connect the motors to it. For a really detailed explanation of the mechanical side of the build, take a look at my original Instructable of 2010, linked at the top of this introduction page.

One gyro is used for balancing (complementary filter with an accelerometer). Another gyro measures rate of rotation laterally (e.g. when steering).
This provides another useful feature for free; when running in a straight line, if it detects rotation faster than 10 degrees per second laterally, it will change power to the motors to resist this effect. For example the motors often have different friction so when you slow to a stop, one stops before the other and you spin off. This feature stops that happening, and means the wheels can be mounted quite close together.
See this video http://www.youtube.com/watch?v=FEaTxahyQxc and you will see this happening at 0.51 mins, the spare gyro is used to reduce this effect.

NOTE: Added 15/03/14. For those who may manage to have one of the old, no longer available, Sparkfun 5dof analog IMU's, I have just attached the same code as written in this Instructable for the Chinese IMU, to step 30 but with the gains changed for the gyro to suit the old 5dof analog Sparkfun IMU.

Main parts list

www.maplin.com part number GBP US$

N39KR RockerSwitch 2.39 3.62
N39KR RockerSwitch 2.39 3.62
GW72P Microswitch with lever 2.49 3.77
FH04E Sub-Min Toggle switch 2.79 4.23
Project Box 3.79 5.74

XR27E 9 way multicore cable 5.14 7.79
2 core screened cable 0.99 1.50
N30KU Arduino Uno 24.99 37.86
N35KU Arduino protoshield 14.99 22.71
5DOF analog IMU 17.81 26.99
NOTE: List of sellers of this updated August 25th 2013 (See Step 6 for the list)
4 x LED’s 2.56 3.88
80.33 121.71

Video clip

Video of latest self-balancing skateboard in action using this IMU and code

Step 1: Whole Assembly With Hand Controller

This Instructable assumes you know basics of how to load a program or "sketch" into an Arduino microcontroller and also know how to solder.

Even if you think you can see just fine, a pair of magnifying glasses or magnifying visor as sold in many hobby shops makes a huge difference when soldering very small parts and will last you your whole life probably.

The Shield has long pins that allow you to push it on top of the Arduino when completed. Take care as quite easy to bend some of them as you do it. It has a square grid of solder-holes on which you can mount your own components. I will mount my IMU, my LED indicator lights and the cables to the sabertooth motor controller and the hand controller securely to it also.

The shield is top left in this photograph. Arduino UNO is lower left. Hand controller, which we shall also make is lower right.
<p>Where did you get the motors, i don't know where to get them and which ones to get</p>
<p>so why do you use an analog imu and not a digital. i have a digital 6dof</p>
<p>would a 100watt e100 motor be suffucient</p>
<p>Another great instructable!</p><p>Thanks for all of your efforts!</p><p>You have saved me countless hours of trial and error on my project because you took the time to post.</p><p>Can't wait to see you and Mr. Thatcher's latest creation in action!</p>
<p>Hello John,</p><p>I'm attempting to build a copy of your <em>Pneu-Skate attempt at pneumatic tyred smaller skateboard</em> <a href="https://sites.google.com/site/onewheeledselfbalancing/Home/pneu-skate" rel="nofollow">https://sites.google.com/site/onewheeledselfbalancing/Home/pneu-skate</a> for my 8th grade report,</p><p>and was wondering what your dimensions are, length, width and thickness of board. </p><p>Thanks!</p><p><br></p>
<p>Hello John,</p><p>I'm attempting to build a copy of your <em>Pneu-Skate attempt at pneumatic tyred smaller skateboard</em> <a href="https://sites.google.com/site/onewheeledselfbalancing/Home/pneu-skate" rel="nofollow">https://sites.google.com/site/onewheeledselfbalancing/Home/pneu-skate</a> for my 8th grade report,</p><p>and was wondering what your dimensions are, length, width and thickness of board. </p><p>Thanks!</p><p><br></p>
<p>Ciao,</p><p>Ho letto cose molto buone su questo driver del motore, penso che sia meglio di Sabertooth.</p><p>http://www.pololu.com/product/2393</p><p>Xenon si chiedono che cosa ne pensate ... </p><p>Cordiali saluti, </p><p>Gerardo</p>
<p>Can I do it with this http://www.adafruit.com/products/81 motor controller? If not, what else can you recommend besides the sabertooth?</p>
<p>Whats the max speed?</p>
<p>Here is an analog output 3 axis gyro still available in Italy if it helps anyone:</p><p><a href="http://www.robot-italy.com/it/gyro-breakout-board-lpy503al-dual-30-s.html" rel="nofollow">http://www.robot-italy.com/it/gyro-breakout-board-...</a></p>
<p>Ciao John,</p><p>obiettivo raggiunto, ma ancora migliorabile. </p><p>Ecco il Vido: </p><p><br></p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/ht3QDVwe3j8 " width="500"></iframe></p><p>Grazie di tutto, </p><p>a presto, </p><p>Gerry</p>
<p>hi can i also use MPU 6050 GY-521 ??</p>
<p>Hello John,</p><p>I'm using an IMU Analog Combo Board - 5 degrees of freedom IDG500/ADXL335 SEN-11072 -----&gt; https://www.sparkfun.com/products/retired/11072.Ti wonder if I should change the values of the code. </p><p>Thank you, </p><p>friendliness, </p><p>Gerry</p>
<p>Yes, the gyros have a different voltage outputs. I have just added code to this Instructable that lets you use one of the old Sparkfun analog 5dof IMU's (step 39).</p><p>If using the Chinese IMU listed at start of the Instructable use the original code (step 26 I think).</p><p>I am removing all code from the original self-balancing skateboard Instructable from 20010 as too much code is confusing everybody.</p><p>John</p>
hello john, could you add a potentiometer steering code please?
<p>Can do, so long as you already have it working with a rocker switch!</p><p>Keep things really simple until you get it to self-balance. Then you will be 95% done and can afford to add extra complexity at that time.</p>
<p>Hello to all,</p><p>can anyone tell me if the IMU is oriented correctly?</p><p>Thank you, Regards</p>
<p>Hi everyone,</p><p>Big development: I have finally managed to get all this to work with the Sparkfun DIGITAL 6dof IMU Code No: SEN-10121.</p><p>It has taken 2 years but there we are.</p><p>Uses code from all over the web chopped and changed and merged with my existing code. Use the very clever Kalman balance filter algorithm too.</p><p>See the new Instructable here:</p><p><a href="https://www.instructables.com/id/Arduino-Self-Balance-Controller-using-DIGITAL-IMU-/" rel="nofollow">https://www.instructables.com/id/Arduino-Self-Balan...</a></p><p>John</p>
<p>Hello XenonJohn,</p><p>can i please get a copy of the code for this project</p><p>eyre.camp@hyne.com.au</p>
<p>Attached to Step 27</p>
<p>Hello everyone,</p><p>Hello XenonJohn, </p><p>Nice work, and thank you for the inspiration!<strong> I build a segway like self balancing machine and i used your code with the 5 dof IMU. The problem im facing now is when the board is level and the tipstart becomes active, the balance angle gradually drifts, and the board leans more and more forward.</strong></p><p><strong>i tried change </strong></p><p>angle = (float) ((1-aa) * (angle + gyroangledt)) + (aa * x_accdeg);<br>to this:<br>angle = (float) ((1-aa) * (angle + gyroangledt)) - (aa * x_accdeg);</p><p>but with no success.</p><p>Do you have any idea how i can fix this?</p><p></p>
<p>Could is use this IMU? http://dx.com/p/gy-85-6dof-9dof-imu-sensor-module-for-arduino-148436#.Uv_HKfldVww I am planning to make a one-wheeled slim 12v vehicle like a ''onewheel'' http://www.youtube.com/watch?v=ns7RFZPTfpc</p>
<p>The website (dx.com) is reliable, i purchased several products there; never had a problem.</p>
Hi John, I saw this video http://youtu.be/0wm2V9U5TG4 a skate board similar to yours but has only one wheel. Is it possible to modify your code to work with one wheel? <br><br>Peter
instead of using 2 single axis gyro,is it possible to expected the same result from a dual axis gyro, <br> <br>http://www.rhydolabz.com/index.php?main_page=product_info&amp;products_id=1328 <br> <br>the pins are same, <br>just instead of 4.5 rate output its 4, <br>will the code change?
<p>The code would change but not by much. In fact it might work with code unchanged.</p><p>One problem with these unmounted components is soldering the wires to the sides of the gyro. It comes with no wires sticking out at all, just metal tabs you solder to and they are very small.</p><p>Sparkfun used to make a breakout board for these surface mount sensors, not sure if they do anymore. I used one on an old project which had an accelerometer in a similar package.</p><p>Well spotted. Always helps everyone when a new source of analog gyros is discovered!</p>
<p>I bought a <a href="http://forum.arduino.cc/index.php?PHPSESSID=7m9pk2dofu39kl2l1a5iud3as7&topic=179750.msg1334480#msg1334480" rel="nofollow">GY-521 MPU-6050</a>. I'm wondering if I can use this, I thought I read about it on this instructable, but couldn't find it again. Since it is an I2C type chip. Please help? Thank you!</p>
Hello John, thank you very much for all your information. It's fantastic! <br> I have a question. I don't find analogic IMU, and the question is: What's the difference if I use one gyro IDG 500 separate with an accelero ADXL 335 that an IMU with the same component inside? <br>thank you
Hi, <br> <br>Notice that I have added some more analog IMU suppliers I have found. <br>Seedstudio still do an analog accelerometer and gyro, Have added details to page 6.
My early machines used a separate (analog output) gyro and a separate (analog output) accelerometer. <br> <br>You can do it that way. <br> <br>John
Hello John, <br>I finished assembling my self balancing platform and I have to face the most difficult part for me is that the Arduino programming. I would like to modify the code so you can steer left / right with a potentiometer (5 or 10 Kohm) for proportional control. <br>I understand how to modify the variables P, D, but I do not know if you can adjust the torque and the maximum speed of the motors (2x280W 24V). <br>All your advice is appreciated, <br>thanks, <br>regards, <br>Gerry
Seriously now, <br>Keep it as simple as possible until you get the thing to self balance. Once that is done then worry about the finer points of control. <br>I can easily give you code to add in potentiometers but I would stick with simple switches until it is balancing OK. <br>Therevis plenty enough to go wrong as it is. Start simple then addin each improvement incrementally, testing each one works as you go. <br> <br>John
Ok, I do as you say,<br>thanks so much,<br>regards,<br>Gerry.
Congrats John on a nice instructable. Just a quick question, based off of the picture when you were wiring up the IMU, I saw that A0 was used for something. Could you explain what that was for, couldn't find it in the tutorial. On a side note, almost finish welding my frame together. I'm trying this out with 12 inch tires, how well does this unit tackle inclines?<br/><br/>Ken
I soldered a wire to every single output of the IMU before I mounted it on the prototyping shield. From experience it is best to do this at the start than try to add wires later on when lots of obstructions in the way. <br>I then worked out which ones were relevant for what I was trying to do and trimmed off the ones that I did not need later on. <br>In the Instructable I only list the ones that are required therefore.
Hello, <br>congratulations on your tutorial, and thanks for the valuable advice you give. <br>Sincerely, Gerry.
Greywarden has just sent me a message that he has managed to get his board to work fully. Always nice to know it can be built from the instructable. Problem was loose wiring. <br>John
HI, <br> <br>congratulations for this tutorial. This is by far the most complete one I found. I want to use this to make an arduino Segway. what advise would you give me (electronics and SW)? I would also like to power the arduino and shied from the 12V battery instead of doing it from the AA Batteries. Possible for you?
I did in the past use a DC-DC voltage converter, look at Tracopower products for example. This works up to 48V and converts it down to 9V DC. <br> <br>Just once I turned it on and the accelerometer failed in the IMU. It was an older Duemilanove Arduino and not sure if some momentary power surge caused the problem. After that I always power the Arduino with a nice smooth ripple Dc battery as the IMU needs a nice smooth regulated supply. <br> <br>Not sure if I am being paranoid but there are enough odd things that can g wrong that take ages to diagnose and this makes one less to worry about. <br> <br>So, yes you can do it if you want to. <br> <br>There are some micro segways out on the web built from two rear wheel drive sets from electric childrens scooters eg Razor range so you can do it this way or go for a bigger drivetrain. Look at the Sci-tech instructables for inspiration. <br> <br>John
OK, I found the problem. <br>In the Instructable I said connect pins 11 and 12 to the Sabertooth. This was wrong and not what I actually did in the end. <br> <br>GND on the Arduino/Shield goes to 0V on the Sabertooth and Pin 11 goes to S1 on the Sabertooth. Pin 12 not connected to anything at all. <br> <br>This will explain why you had to GND the sabertooth to get anything to happen but then it still behaved erratically. <br> <br>I have updated the Instructable just now. It was a mistake I originally made and corrected when I built mine but forgot to correct when I wrote it all up. Sorry for inconvenience caused. <br>
Alright, I fixed that problem. The most likely problem was that the arduino and sabertooth weren't on common grounds. This was fixed pretty easily by placing a wire between my arduino's - battery terminal and the sabertooth's ground. I tested it with your serial testing sketch posted on the other ible, and it works great. But now I am having trouble getting it to balance. The IMU Tester code tells me to replace the 338 with 350, which I have done, but I can't balance it correctly. No matter what variables I change or how I change them, the balancing code starts too soon (the board is still tilted) often I don't even move the board when it decides to try and balance. I'm just getting confused at this point X) any help to offer for balancing adjustments?
Are turning it on tilted with one end on ground, not moving it at all, then waiting 10 seconds before you bring it level? <br>Just making sure.......
Send me an email address via the messaging system and we can correspond on this. First step would be to video it and put it on Youtube as a private video (only those you send the link to can view it). I can tell a lot by just watching what happens. <br> <br>Best wishes <br> <br>John
*edit* My IMU was oriented the wrong way, but that is now fixed. The issue I'm presented with at this point is that once the balancing code begins (when it becomes level) my motors do not kick in. The board just sits there, even though it should be trying to balance now. No response even when I manually tilt it
Hey Xenon, Huge fan. I have almost finished construction of my own board, but I have run into a problem. To the best of my knowledge everything is connected up correctly, and it should be working. I have tuned the program to start balancing once it reaches my &quot;level zone&quot;, but that is when I hit the problem. All of a sudden, the Sabertooth X25 starts flashing its error led, and doesn't work. The motors just sit there and twitch slightly, and no amount of manipulating the gyroscope changes this. I think I'm having some kind of error in communicating to the Sabertooth via serial. I looked it up, and the Arduino serial ports are pins 0,1 not 11, 12. I can't figure out how to pass the messages to the Saber or what I'm doing wrong, and I'd really appreciate some feedback on the issue
If the above does not work I will modify my motor tester (serial comms tester) sketch as supplied with my original instructable and attach it to this one for pins 11/12. <br> <br>John
I used a bit of code called &quot;SoftwareSerial&quot; to make the Arduino send the serial communication out through pins 11/12. <br>This is a library called SoftSerial.h and commonly used in Arduino projects. <br> <br>If the voltage applied to the Sabertooth (from fully charged 24V batteries for example) is &gt; 27V the Sabertooth will refuse to work. I had this once with a project and fully charged batteries and actually had to leave the thing on for a while until the voltage fell below 27V and it then worked. <br> <br>Also, if you go back through the 500+ comments on this Instructable, there are one or two Sabertooths that seem to have a fault and refuse to work with Serial communication. Are the DIP switches set up right on the Sabertooth? <br> <br>If you have the flashing red light, just get a meter and check the battery voltage though, it may simply be that. <br> <br>John
Can you give a link where you got the pneumatic tires from? I have been looking for some like that, and can't find any.
For rear wheel you need to google &quot;Razer E300 rear wheel&quot; and several suppliers will come up such as razerama.com and monsterscooterparts.com <br>It will appear in the spares section of the website. <br>Tyres and tubes are there too. <br>NOTE: The E300 wheel seems to come with 2 different sprocket sizes from the images displayed. You need the larger diameter 55 tooth sprocket. Ideally get it with the wheel although these sprockets can also too can be bought separately. <br> <br>
hello <br>where you have find your motor ? <br>thanks a lot

About This Instructable




More by XenonJohn:Ardu McDuino: Bagpipe playing robot (chanter) Star Wars Maz Kanata Inspired Glasses Music & lights in childproof case 
Add instructable to: