The Balancing Robot

Now avaliable as a kit
A balancing robot kit is now avaliable via Kickstarter: Check out the blog post as well:

Hello everybody
I have for a long time wanted to build a remote controllable balancing robot aka Segway – that’s was actually the main reason why I created the PS3 Bluetooth Library both for Arduino and the FEZ Devices. It has been a long time since the sneak peak and the performance has been improved a lot since then. The original one had a FEZ Rhino as the main processor, but I discovered that it was not fast enough to read the encoders, as it is not running embedded code. Also I was already using more than 10ms per loop, which I used as a fixed time loop, so I decided to step up a notch and go for a much more powerful device: the mbed microcontroller, which is an ARM Cortex-M3 running 96MHz.

It might have been possible with just a normal Arduino (NB: I have now ported the code to Arduino, see update for the code), but I didn’t want the speed of the processor to be an issue, so I decided to go for the mbed. The robot also features an Arduino Duemilanove with a USB Host Shield on top running a sketch based on my PS3 Bluetooth Library. The mbed board actually has USB Host functionality, but I decided not to port the PS3 Bluetooth Library as my original thought were to use an Arduino Due, but as you might know it hasn’t been released yet, despite the Arduino team announced, that it would be released by the end of 2011. But as soon as it is released I think I will port the code to it instead.

Video Demonstration
Here is a short video demonstration of the robot and me explaining some of the concepts of the design and how it works:

The Hardware
Here are some pictures of the robot:

Here is a list of all the hardware I used:

I also used:

The robot itself is made of three pieces of 215x75x7.5mm MDF wood and four threaded rods. The distance between the plates is 7cm at the bottom and 7.5 at the top. In total the robot is 27cm high including the battery.
See the 3D model for more information.

3D Model
I have created a 3D model in Autodesk Inventor with true dimensions, this will hopefully inspire other people for there robot design. All files can be found at github.
The 3D model can also be viewed at the following site:

Check out these rendered images of the robot:

The Code
All the code and 3D model can be found at our github. Here is a list of hyperlinks for all the repositories:

Also check out the wiki.

I have now ported the code to Arduino. The code can be found at github:

I have thought about how I could improve the performance of the robot. First of all I could try to use an accelerometer with a smaller resolution, as the one I got is a ±3g and ±1.5g would be sufficient for my needs. Also my gyro got a resolution of ±300 deg/s and I have seen people use gyro with a resolution as low as ±50 deg/s.

Another aspect would to use belts to minimize backlash, instead of connecting the wheels directly to the motors – a bit like this one.

Also I don’t compensate for the battery level in the code – so it behaves differently depending on the battery level.

Overall I am really happy about the end result – it balances pretty well and the remote control works perfect!

It has been a really good learning experience for me and a really fun project to do, but also very time-consuming – I have spend many nights tweaking the PID values and adjusting tiny bits of the code before I accomplished the end result.

The next step would to build a full size one, but I don’t know if I will do it the near future – but hopefully some day :)

That’s all for now. Hope you like my robot. Feel free to post a comment below and I will answer as quickly as possible.

  1. nela
    April 11th, 2015 at 19:25 | #1

    Hi Lauszus,
    I got it working! :D My robot balances now pretty well. Thank your for all your help!

  2. April 14th, 2015 at 15:16 | #2

    You are welcome :) Glad to hear you got it working!

  3. ali
    April 20th, 2015 at 17:19 | #3

    dear lauszus

    i am making a self balancing robot using arduino mega ,mpu6050 and polulu’s 30:1 12v 350 rpm motors with encoder, i am using jeff rowberg library for mpu sensor to get pitch values, the data from mpu comes on interrupt pin which is triggered when fifo buffer has a value , i want to know how can i read encooders using interrupts, because everytime i use ISR for encoders i get false values which i do understand because arduino is already busy on mpu data interrupt ,

    pls help me


  4. Greg
    April 21st, 2015 at 18:28 | #5

    I just had a question about how you implemented the PID loop onto your robot. I have built a robot very similar to yours and I do have it balancing. To do this, I have two PID loops. One varies the angle on which to set the robot to which is determined by how far away from zero the wheels have moved using encoders and the other PID loop gets the robot to that determined angle. Mine is not quite as stable as yours however. Is this similar to how you have set your PID loops up or have you taken a different approach?

  5. April 21st, 2015 at 23:16 | #6

    Yes that is the same approach I have used. Can you share your code somewhere, then I will have a look at your code :)

  6. Mansor
    April 23rd, 2015 at 12:35 | #7

    Hi Lauszus,

    Any chance porting the code to Due?

  7. April 24th, 2015 at 21:33 | #8

    No unfortunately that is not something I will be working on in the near future.

  8. arpit
    May 6th, 2015 at 14:13 | #9

    hi lauszus, ur robot balances very well …. great work.
    1.I would like to know can the same robot balance for polulu’s 19:1 12v 500 rpm motor with encoder…??
    2. what was ur pwm value and how to adjust pwm value for 500rpm motor…

  9. May 6th, 2015 at 14:15 | #10

    It possible will, but why not just use the same ones as I used?

    You will have to tune the PID controller, so it works with your motors.

  10. arpit
    May 7th, 2015 at 05:19 | #11

    @Kristian Lauszus
    i will tune the pid controller…because my robot is heavier some what larger in size than urs …. so for my application those motor were suitable…
    by the way thanks for ur suggestion.

  11. May 7th, 2015 at 18:11 | #12

    I would have recommended motors with more torque if it is heaver i.e. a higher gear ratio.

  12. arpit
    May 13th, 2015 at 03:43 | #13

    hi kirstian,
    how did u tune ur PID values so precise ??did u used some software or just by trial and error?

  13. arpit
    May 13th, 2015 at 04:01 | #14

    i would also like to know if the robot balances pretty well using arduino or it balances more precisely using mbed controller and arduino as u have mentioned on ur blog….thanks

  14. Bill Parker
    May 15th, 2015 at 22:47 | #15

    Hi Would it be practical to use this to control a full size self balance ridable 2 wheeled Segway type of transport? Or do you know of anything that might help me I have trouble walking and thinking this may help.

  15. June 7th, 2015 at 15:06 | #16

    I tuned them manually. Start by setting all value to 0, then increase Kp until it starts oscillating, then turned it down a little. Then start increasing Ki until it starts oscillating, but this time the period should be longer. Finally you can increase Kd to dampen the system.

    The performance is the same between the two systems.

    @Bill Parker
    Please see the following blog post:

  16. Rodrigo
    June 17th, 2015 at 22:43 | #17


    I am little puzzled by the way you implemented: (1) the control to reduce the target angle at high velocities and (2) the control to return to the stop position. A more intuitive approach would have been to use standard PID loops for each. For example in the “non-stop” condition a PID finds the target angle (restAngle) based on the velocity error (difference between the desire velocity and the actual velocity). In the “stop” condition a PID finds the target angle (restAngle) based on the distance away (encoder counts) from the stop position. Does this make sense? Did you try an approach like this instead? If yes, what was your experience? If not, why not? Any suggestions?

    Thank you,


  17. June 20th, 2015 at 15:21 | #18

    You should check out this branch of the Balanduino repository: where I have experimented with just that, but I never got it to work properly.

  18. Ryuoh
    June 28th, 2015 at 05:23 | #19

    Hi Lauszus
    I’m making balanced robot now but no matter how I tune PID parameter it can’t be balanced
    it swing like this

    I use pololu 50: gear motor with encoder
    robot weight is around 1.5 kg
    can you give me some advice

  19. June 30th, 2015 at 00:57 | #20

    I have replied to your Facebook message :)

  20. Siddhant
    July 16th, 2015 at 06:19 | #21

    Hi Kristian,

    Thanks for the great guides you have posted all over the internet and for regularly replying to posts here! There’s a lot to read and I’m hoping I’ll be able to solve most of my troubles during my quest for balance by reading posts from this blog.

    I do have one problem that I cannot seem to solve though. When laying flat on a table, the sensor shows 180 degrees but when I roll it to a vertical position moving it by 90 degrees, the serial monitor does not show a value of 90. It shows 90 degrees at a position that is clearly not 90 degrees. How can I go about fixing this?

    Thank you!


  21. July 16th, 2015 at 07:34 | #22

    You need to calibrate the accelerometer. This can be done by finding the accelerometer values when the robot is horizontal and then subtracting the values from any further measurements. See:

  22. Larry McGovern
    August 23rd, 2015 at 16:47 | #23

    Hi Kristian, your website has a wealth of data, and was a huge source of inspiration for my own balancing robot “Zippy”. Here’s my homage to your project: You may notice one or two design similarities… Thanks for making all this publicly available! I’ll be watching your site.

  23. August 23rd, 2015 at 23:15 | #24

    @Larry McGovern
    Looking good :) How is your experience with those wheels? I might try something similar at some point.

  24. Larry McGovern
    August 24th, 2015 at 01:48 | #25

    @Kristian Sloth Lauszus
    I think the scooter wheels are great, but I can’t say I have any experience with other wheels. They work just as well on hard surfaces and carpet, and have a nice rubbery grip. I used a scooter wheel adapter from Pololu, which directly interfaces with a 6 mm diameter motor shaft. My daughter has wheels that light up on her Razor, which might also be a fun addition to a balancing robot…

  25. August 24th, 2015 at 19:58 | #26

    @Larry McGovern
    I guess you are talking about these ones: I might try them out. They must be relatively new, as I have not seen them in their store earlier.

    Haha yes it might be fun to try someone with flashing lights!

  26. YOBE
    August 29th, 2015 at 03:57 | #27

    Hi Kristian,
    Im also working on my balancing robot but facing problems with interrupt and I2C communication.
    I am using Chipkit, but once I create the high freq interrupts from the encoders, my I2C communication to the IMU is crashing and hangs the uc.
    Have you ever encountered something like this?

  27. mohamed siddik
    September 9th, 2015 at 11:50 | #28

    hi frd;
    i am siddik from india (TN). I am interested to do the self-balance robot. I done mobile controlled robot. I want to do this help me frd . I have Arduino uno, bluetooth transceiver module(hc-05) then two 5v dc motor .

  28. September 11th, 2015 at 08:27 | #29

    No I have not. Maybe it is a problem with your connections? Please try to solder the connections if you haven’t already. Also try to add some capacitors close to the regulators in oder to filter out the noise.

    @mohamed siddik
    Okay sounds good. What do you need help with?

Comment pages
1 10 11 12 2196
  1. March 4th, 2012 at 21:03 | #1
  2. March 7th, 2012 at 21:22 | #2
  3. March 7th, 2012 at 21:29 | #3
  4. March 7th, 2012 at 23:33 | #4
  5. March 8th, 2012 at 02:49 | #5
  6. March 8th, 2012 at 05:03 | #6
  7. March 8th, 2012 at 08:31 | #7
  8. May 14th, 2012 at 15:14 | #8
  9. May 23rd, 2012 at 20:02 | #9
  10. February 17th, 2013 at 15:13 | #10
  11. February 17th, 2013 at 15:24 | #11
  12. March 18th, 2013 at 23:18 | #12
  13. March 24th, 2013 at 17:58 | #13