Projects for geeks

Self-balancing robot

01 Jul 2011

The robot is able to balance on two wheels.
Originally this project was created using only junk parts to test software for electric unicycle. But as time passed the initial design was upgraded. At the beginning robot was using 2 hacked car lamp servos as motors, ATmega16 board, quickly assembled motor controller based on L293 and the only brand new part: gyro + 2 axis accelerometer board.



  • Atmel ATmega32 microcontroller
  • L293 motor controller
  • RS232 (MAX232)
  • Bluetooth module
  • ADXL203 accelerometer and ADXRS401 gyro


  • Two second hand motors with planetary gearbox
  • Custom made frame (steel + plexiglass)


  • C robot control program for ATmega32 (Kalman filter, PID controller)
  • C# diagnostic tool for PC

Principle of operation

It is quite simple. When robot moves to the right you need to move wheels to the same direction. When it moves opposite, turn wheels in the same way.
Balance control only require small amount of processor power. AVR microcontroller can easily perform this task. In the simplest form you can read gyro an control rotation of wheels based on angle between neutral position and actual robot angle.

That’s theory. In practice balance control is a bit more complicated but still straightforward.
First — gyro has some drawback. Gyro has a drift. That simply means it reports different readings in time for the same position. For the robot that means crash as after some time gyro will indicate wrong position.
The second thing is — popular gyros does not report absolute position but how fast they rotate (angular rate [°/s]). So you don’t know what current position of the robot is but only how fast it falls down.
To solve this problem readings from gyro must be combined with 2 accelerometer readings. One accelerometer must measure acceleration in X axis, the other one in Y axis. Having Y and X readings you can compute absolute angle. You could think that you could use only 2 accelerometers without gyro. But it’s not possible accelerometers in some situations gives you wrong angle. Only combined readings of XY accelerometers and gyro gives you accurate angular position.

Consequences of the above are that for the balancing robot you need 2 axis accelerometer and one axis gyro (more precisely angular rate-sensing gyroscope).

Kalman filter

To combine all the signals into something useful for robot control Kalman filter can be used. Originally designed for Apollo missions still is a fantastic tool. Here I do not want to go deeply in theory but some advantages of using Kalman filter:

  • Eliminates gyro drift
  • Removes noise from input data
  • Compute absolute angle

Understanding and properly implement (and configure) Kalman filter is challenging task. In addition processor power needed for Kalman filter implementation can be too much for AVR chip. Fortunately, optimized Kalman filter routine can be implemented in C language for AVR microcontrollers.

One of the most popular implementation is located here:

PC software

To see how Kalman filter works a diagnostic tool was created in C# (in free Visual Studio 2010). Later some other features were added. Current version include:

  • Visualization of sensor data and Kalman filter output
  • Robot software upgrade via serial cable
  • Settings of PID controller and Kalman filter
  • Visualization of robot position

Robot firmware

Current version of robot software supports:

  • Remote upgrade via serial port (or Bluetooth)
  • Kalman filter
  • PID controller
  • Bluetooth support

Application for Android phones is planned to control robot via Bluetooth.

How it works

Here is a video of the early version (with car servos). The balance is not perfect yet.

Project progress

The project is not yet finished. Robot can balance but it’s still not perfect. Some mechanical issued must be solved.
At the end I publish all the software including source codes.