This is the Arduino version of the code for my balancing robot/segway
Arduino C++

Developed by Kristian Lauszus, TKJ Electronics 2012

The code is released under the GNU General Public License.

This project is deprecated

This project is no longer maintained as I will instead focus on a Balancing robot kit, the Balanduino. For more information see the new repository: and the Kickstarter campaign:

This is the code for my balancing robot/segway. It's a ported version of the code for the mbed board which I original used. The original code can be found at the following link:

The code will work for all boards that features an ATmega328p (Duemilanove, Uno, Pro, Pro Mini etc.) - it's not directly pin compatible with the larger Arduinos (Mega, Mega 2560 etc.) as I use the port registers to save processing resources - see But just take a look at the pinMapping pages for comparison: and and figure the pins out yourself - for instance OC1A and OC1B are not located on pin 9 and 10, but at pin 11 and 12 on the Arduino Mega.

I have used the registers to set up 20kHz PWM, Phase and Frequency Correct on pin 9 (OC1A) & pin 10 (OC1B) with ICR1 as TOP using Timer1 - see the datasheet page 128-135.

I use a 6DOF IMU from Sparkfun:, though I only use one of the gyro axis, but any IMU can be used. For instance the very popular MPU-6050, see this example code.

For more info about calculating the pitch see my post at the Arduino forum:,58048.0.html.

For more information about the Kalman filter see my blog post: and the source code:

To steer the robot, I use a USB Host Shield together with my SPP Bluetooth Library for Arduino: More information can be found at the blog psot:

You can either use an Android app I wrote: or the Processing Application to control the robot.

For information about the hardware, see the wiki:

Also check out the youtube video of it in action: - this is actually the mbed version, but they behave the same way.

For more information see my blog post at or send me an email at