Go Down

Topic: Balancing robot for dummies (Read 76502 times) previous topic - next topic


Found your code - that is what I was hoping for.

Thanks, Peter


Good. Note that if you are looking for the PS3 version, have a look at the PS3 branch: https://github.com/TKJElectronics/BalancingRobotArduino/tree/PS3


Mar 19, 2013, 07:09 pm Last Edit: Mar 19, 2013, 07:30 pm by john_john Reason: 1
Hey guys! I am building self-balancing robot too and I would be grateful if you could help me a little bit to understand the following concepts:

1. Integral term (i_term). This term always grows in the existance of the error. Many of you use the ceiling values for this term. The first question is - if my max val for the torque is 255 what are appropriate ceiling values for the i_term and how to determine them?

2. If my robot overshoots, the error sign will change. How to go about the i_term in this case, which might be quite big causing lag? Should I just zero integral term every time my error change in sign?

3. I am using self-made optical rotary encoder. You guys are adding terms to the PID and I cannot get my head around it. So it's sort of a method of providing a feedback to the controller in terms of velocity? But in this case our set-point = zero error is in terms of the angle. How you relate both?

Patricks code:
pTerm_Wheel = Kp_Wheel * count;           //  -(Kxp/100) * count;
dTerm_Wheel = Kd_Wheel * (count - last_count);

4. Somebody wrote about using a potentiometer in order to determine values for PID coefficients. Can you provide some information how it's done?

5. Also can you give some ideas how could I save a stream of data (my error) into external memory? The purpose is to analyze the responce.

Thank you for your help!                  


I just released a balancing robot kit on Kickstarter: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit and I thought you guys might be interested.

Here is a video of it as well:


hello everyone.

       i have been trying to build my own bot for a few months now and i have come quite far in building it, just havent got it to balance yet. but this forum is been of great help. so i finally decided to consult the experts.

    I am using a 9DOF pololu IMU but as shown on previous threads i only use 2axis  accel and 1axis gyro readings which via the kalman filter give an actual angle. i use a dfrobot motor driver and dc motorrs (without encoders).

    the things that i think are causing problems are

1.  The delay caused by the Kalman algorithm. this leads to a backlash since by the time the kalman output reaches zero the real bot angle already crosses vertical zero.

2.  Motor Stiction, there is no movement of the wheels at low speeds, the PWM input has to be atleast 50 (in a 0 - 255 range) for the motors to move.

3. I also started to wonder if the size of the wheels and the structural dimentions of the bot have any effect on the final balancing. most importantly does the weight distribution of the bot have to be perfect.

        Please do let me know what you think. if anyone else has experienced such problems, how did you overcome them.


You should checkout my source code for my Balancing robot: https://github.com/TKJElectronics/BalancingRobotArduino

1. Do you mean there is a problem when the angle change from 0 to 360? This can easily be fixed by making the angle 180 when the robot is upright.

2. I don't do anything to prevent that, but I have seen other people do that, but I really don't think it's necessary.

3. Try to make the robot as symmetric as possible, so one of the sides doesn't weigh more than the other - my robot can almost balance by itself. Also remember to put some weight at the top in order to raise the center of gravity. Remember that the better the mechanical structure is the better the end result will be!


Did you ever get your robot to balance with stepper motors?

I haven't had the time to look at your code yet but it sounds like your having the same problems that I have..

I have no problem getting my robot to balance but it's shaking a lot and I think it's manly to the backlash.
You can see it in the video blow if you look closely you will see the shaking..

I have recently seen that I may have a problem with my IMU which the zero point is moving after a will and that I also think is do to the shaking and mess up the kalman filter..

My next step is to change the motors to the stepper motors below:
#1200 Stepper Motor: Unipolar/Bipolar, 200 Steps/Rev, 42x48mm, 4V, 1200mA
#1182 A4988 Stepper Motor Driver Carrier


We used stepper motors and they work great. Here you can see the result:



Nov 07, 2014, 07:58 am Last Edit: Nov 07, 2014, 08:00 am by kas
Very nice and stable bot  :o  :o  :o
No more backlash

I suspect that moving the smartphone near the the axis of rotation will make it 100% stable
Yes, I know... the eyes won't be at the right position   ;)


Nice robot kit @Lauszus I will most likely buy one from you, liked your code as well. I'm back building some robots with my children, we bought a SainSmart InstaBots that we are tinkering with.

I will start building a new frame for my own bot in the coming holidays.
The balancing robot for dummies guide


Hi Patrick,

Nice to hear from you again  ;)
My balancing bot is now full digital (MPU 6050), using Lauszus modified code
Still looking for motors with no backlash...

I am now busy on another exciting  project    8)



I got the Instabot working ok I like the MPU 6050, I thing I will change the code for the Instabot to my own. I think It's a lot that can get better. i will also try Lauszus code for the IMU. When you thinker with that bot you can really see way you need motors with encoders.

In the video below you can see it balancing after tuning for 3 hours, it can get better! :

In the video below you can see it controlled from the controller, same values as the video before:

The balancing robot for dummies guide


Dec 08, 2014, 08:32 am Last Edit: Dec 08, 2014, 04:00 pm by kas
When you thinker with that bot you can really see way you need motors with encoders
Yeah, without encoders, getting the bot to stay still is hopeless  :smiley-sad:
Encoders are also usefull to compensate for the speed difference between left/right motors, and have straight moves.

I am now controlling the Bot with Joystick BT Commander, from my Android phone


Hello to everybody !!

I have just discovered this forum and I'd like to show you my application of a self balancing robot.
The hardware is composed by two DC brushed motors with 65 mm diameter wheels, a 2 Amps H bridge motor driver, a 6 DOF digital IMU (3 axis accelerometer plus 3 axis gyroscope) and an Arduino UNO microcontroller.

Angular data measured by IMU components are fused via a simplified Kalman filter and used to feed a PID controller that drives the motors.

In the first prototype (www.youtube.com/watch?v=fDqIm2CmBtA) I used 122 RPM motors but the robot is a bit unstable. So I substituted the motors with 399 RPM model and now the robot is a bit more stable (www.youtube.com/watch?v=sOcq6TKG3O4).

The next step is to control the platform remotely via Xbee or  Bluetooth protocol.

Bye, Giancarlo


Yeah, without encoders, getting the bot to stay still is hopeless  :smiley-sad:
Encoders are also usefull to compensate for the speed difference between left/right motors, and have straight moves.

I am now controlling the Bot with Joystick BT Commander, from my Android phone

Encoders are not absolutely necessary, they are useful if you want better speed control or if you want the robot to balance and stay quiet at an angle != 0 (when you add some asymmetric load at the top).

Here is my balancing robot with motors without encoders
in the video I'm closing the speed loop.

Go Up