The code is released under the GNU General Public License.
Developed by Kristian Lauszus

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: https://github.com/TKJElectronics/BalancingRobot.

The code will work for all boards that features an ATmega328p (Duemilanove, Uno etc.) - it's not directly pin compatible with the larger Arduinos (Mega, Mega 2560 etc.) as I use the port registers to save processing power - see http://www.arduino.cc/en/Reference/PortManipulation. But just take a look at the pinMapping pages for comparison http://arduino.cc/en/Hacking/PinMapping168 and http://arduino.cc/en/Hacking/PinMapping2560 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 recommend starting by using an Arduino Mega board, as I have used all the I/O's of the small version (Duemilanove, Uno etc.) even the UART ports. I recommend starting out with an Arduino Mega and then when one have the finished code - port it to one of the smaller boards.

I also use 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 http://www.atmel.com/Images/doc8025.pdf page 128-135.

I use a 6DOF IMU from Sparkfun (http://www.sparkfun.com/products/10010), though I only use 1 of the gyro axis.
For more info about calculating the pitch and the Kalman filter see my post at the Arduino forum: http://arduino.cc/forum/index.php/topic,58048.0.html.

To steer the robot, I use a USB Host Shield (http://www.circuitsathome.com/products-page/arduino-shields/usb-host-shield-2-0-for-arduino/) together with my PS3 Controller Bluetooth Library for Arduino (https://github.com/TKJElectronics/USB_Host_Shield_2.0).

For information about the hardware, see the wiki: https://github.com/TKJElectronics/BalancingRobot/wiki/Hardware.

For more information see my blog post at http://blog.tkjelectronics.dk/2012/03/the-balancing-robot/ or send me an email at kristianl at tkjelectronics dot dk.

Also check out the youtube video of it in action: http://www.youtube.com/watch?v=N28C_JqVhGU - this is actually the mbed version, but they behave the same way.