Building a Self-Balancing Robot – The Base

In my first post about a self-balancing two-wheeled robot I introduced the prototype. Now it is time for the beginnings of the construction of the ‘real’ thing. This post focuses on the platform. In order for it to be sturdy enough to eventually let a human ride on it, I opted for a pair of wheelchair motors with wheels that I bought used from ebay. The pictures show the dimensions (inches) of one of the Pride Jet motors with the attached gear box.

2009-12-30-18-18-12-028-cropped-small   2009-12-30-18-19-42-030-cropped-small

The motors come with an electric brake attached at the end opposite to the gearbox. In the photos the black bowl shaped cover and the electric brake (still attached to two white wires) have been removed from the motor. Instead of the brake I will attach wheel encoders (more about this in a later post). The matching wheels came with the motor.


In a wheelchair the motors are mounted horizontally. However, in order to gain more ground clearance I decided on a vertical mount for the robot. The next pictures show the assembled wooden base of the robot.

2010-01-05-16-36-00-002-cropped-small 2010-01-05-16-37-54-004-small

The gearboxes are attached to a board on the underside of the round base board. On the top of the base board, behind the motors that stick out from below, two sealed lead acid AGM (Absorbed Glass Matt) batteries are mounted. The off center configuration balances out most of the motors’ weight. The two batteries, model RBC6 12V 12Ah SLA Sealed Lead Acid AGM, are offered by Battery Geek on ebay for US $31 plus US $12 shipping.

This covers the construction of the base. Apart from the motors and the batteries all materials can be bought in a local hardware store.

Next I will work on the electrical system and the electronics. Stay tuned …

Building a Self-Balancing Robot – The Prototype

Like many others I am fascinated by two-wheeled, self-balancing robots. I am currently in the process of building a self-balancing robot that should be big enough to allow me to ride on it. However, before I blog about this new project I want to briefly introduce a LEGO prototype that I built a couple of months ago. The structure and the motors are LEGO but as the ‘brain’ I used the Make Controller Kit (see image below). The Make controller board can be seen in the center. Only the larger diameter wheels are used for balancing; the small wheel that is visible in the front (there is another one in the back) is only there to prevent the robot from falling over too far if the power is off or the balancing fails.


The following short video shows the balancing in action. At the end of the video I turn off the power supply, making the robot ‘fall’ as a result.

The tilt sensor consists of the 3-axis accelerometer ADX330 and the dual axis gyroscope IDG300. The x and z accelerations from the ADX330 sensor are used to calculate the raw tilt angle. It is accurate provided the sensor is static; i.e., the acceleration is purely based on the gravitational forces. In a moving robot the actual forces are based on the gravitational forces and the acceleration of the robot itself. To compensate for acceleration the gyroscope sensor is used. The tilt angle is calculated by integrating over the angle velocity that is measured by the gyroscope. This calculated tilt angle is heavily impacted by drift and hence cannot be used in isolation. For this reason the output of the gyroscope and the output of the accelerometer are combined via a Kalman filter.

The following graph shows the difference between the angle that is calculated from the accelerometer (blue) and the angle based on the application of the Kalman filter (red):


The calculated tilt angle is passed into a PID loop that is used to control the motors. For details see the complete controller code. In addition to the code that calculates the tilt angle and drives the motor, the code also contains two asynchronous USB tasks (UsbReceiverTask and UsbSenderTask) that were used to communicate directly with the PC. This allowed for fairly efficient debugging of the code and fine tuning of the various control parameters as can be seen in the screen shot below. The included graph shows the tilt angle (red), the angular rate (black) and the normalized speed (green) over time. It is clearly visible how the speed oscillates in order to keep the robot balanced. Below the graph the PID parameters and parameters that are used to assemble the input into the PID loop are shown.


As a prototype the robot worked very well. The balancing was very stable as long as the robot was not pushed. When pushed the robot would counteract but would start moving in the direction that it was pushed towards. This shortcoming is caused by the fact that the control code has no insight into the speed of the robot. The typical remedy is to add wheel encoders to measure the speed and incorporate the speed signal into the PID algorithm. Since wheel encoders are a bit cumbersome I decided not to bother with them for the prototype. I will use wheel encoders in the ‘official’ project though.


I learned a lot from what other balancing robot builders and Segway clone builders have published. Here is a list of some of the links (in no particular order) that I used:

Tic-Tac-Toe Playing Robotic Arm

The description of my Tic-Tac-Toe playing robotic arm is now available from my web site at It includes a detailed explanation of the formulas used for the inverse kinematics of the arm. The vision system leverages the OpenCV computer vision library through the Emgu CV cross platform .Net wrapper. The complete C# source code for the project is provided. Here is a short video of the robot playing against a human opponent: