BaliBot, An Inverted Pendulum Robot


Balibot is the prototype of a two-wheeled robot that has its center of gravity above the wheels.  Without an active control system, the robot would just fall over.  If the robot could sense its direction as it is falling, the wheels could move toward the fall and upright itself.

Tilt Sensor

Tilt Sensor:  To measure the tilt of the robot, a Motorola MMA2260 low G Accelerometer was used.  The device consists of a micromachined structure formed from polysilcon materials. It can be described as two stationary plates with a movable plate in-between. The center plate moves as the device is subjected to tilt.  The output is an analog voltage that is further amplified and conditioned by circuitry on the controller board.

Controller Board

Controller:  A 16F876 PIC microcontroller from Microchip© was selected for the controller.  The PIC has onboard a multichannel analog to digital converter suitable to measure the tilt sensor sensor and digital I/O lines to control the two servos modified for continuous rotation.  Power is supplied by four "AA" batteries and regulated by a low dropout regulator.  Unregulated 6 volts is delivered to the servos with a 3300 microfarad capacitor across the power to prevent the microcontroller from resetting when power is drawn from the servos.  The electronics was constructed on a Radio Shack RS 276-150 project board and mounted above the servos on an aluminum chassis.  The batteries are near the top and act as an inverted pendulum weight.

Problems:  The program written in Basic using PIC Basic Pro©, continuously samples the tilt angle and drives the servos using PWM.  Control is proportional; the speed to catch the fall is greater the further the robot is off-balanced; less force is applied when the robot is balanced. The robot encountered many problems to maintain balance with such a simple control.  Some of these problems are overshoot causing the robot to oscillate, vibration transmission to the sensor and mismatched wheel speed.  Issues such as response time, damping ratio and settling time need to be clearly identified. The addition of wheel encoders and a better algorithm to control the servos are currently under development.

Using The Sharp GP2D12 to sense tilt instead of the low G Accelerometer improved balancing enough for the robot to stay upright.  The GP2D12 is an IR range finder that can sense a height from 4 to 30 inches. The sensor's analog output voltage is measured by the PIC's on-board A/D.  The robot keeps it's balance by measuring the height at the front and back of itself and adjusts the wheels position to maintain equal height.  Performance was greatly improved by increasing the wheel diameter (CD rom disc wheels), providing increased speed of correction to keep the robot upright.  Presently the robot performs fairly well and was shown at the 2003 National Conference on Educational Robotics.

Side View of BalibotBack View of Balibot
Two Sharp GP2D12 sensors replace the silicon accelerometer. The sensors are mounted on an aluminum strip just below the batteries and placed at the front and back of the robot.  The line of sight is toward the ground at an angle.  One inch spacers are used to hold and protect the sensors. These photos were taken before moving the sensor bar to the top of the robot.

ET sensor
View of the Sharp GP2D12  "ET Sensor".

Design Changes:
To allow for variation of sensitivity from sensor to sensor, the angle to ground of the sensor support bar required to be adjustable. Using springs and a thumbscrew solved this problem.  See photo below.

The sensor bar is now at the top of the robot instead of below the battery pack.  Mounting the bar in this manner makes it easier to adjust the sensor to ground relationship.  The increased height also adds "gain" to the feedback. Line of sight distance from sensor to ground is about 17 inches.

Sensor angle adjuster detail

Balibot, Cambot and Rollie at the 2003 National Conference of Educational Robotics, Norman OK

You can get these sensors (very good prices!) at the Mark III Robot Store

Movie of Balibot (Balibot falls over when the PIC resets due low battery voltage)    Balibot.MOV

Program Listing:

' PicBasic Pro program
' 10-bit A/D conversion
' Balancing robot using two "ET" sensors (Sharp GP2D12)
' Use analog inputs channel 0, 1, 2, 4
' Use a 16f876 pic mcu from microchip (

Define osc 20    'crystal speed 20 Mhz

        'Define ADCIN and DEBUG parameters
Define ADC_BITS 10      ' Set number of bits in result was 10
Define ADC_CLOCK 3      ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50  ' Set sampling time to 50 microsecs
Define DEBUG_BIT 5
Define DEBUG_BAUD 9600

tiltfor    var word        ' ET Sensor Forward
tiltbac   var word        ' ET sensor Back
lwheel   var word       ' Left wheel zero speed
rwheel  var word        ' Right wheel zero speed
speed    var word        ' speed variable
k           con 7              ' k factor (gain)
smax     con 500          ' speed limiter

 TRISA = %11111111         ' Set PORTA to all input
 ADCON1 = %10000010     ' Set PORTA analog and right justify result

'lwheel=747     'zero calibrate left wheel
'rwheel=747     'zero calibrate right wheel

    debug "Balbot4"  'send program name over debug to ID program
    pause 1000
    high portc.2         'flash led to indicate reset
    pause 1000
    low portc.2

    adcin 1, lwheel    'read trimmer for left wheel zero speed setting
    adcin 2, rwheel    'read trimmer for right wheel zero speed setting

    adcin 0, tiltfor      'read A/D for forward sensor value
    adcin 4, tiltbac     'read A/D for back sensor value
    speed = abs (tiltfor - tiltbac)* k
    if speed > smax then speed = smax        'limits maximum servo speed
    if tiltbac > tiltfor then back

forward:    'PWM to drive servos forward
    low portc.0
    pulsout portc.0,(lwheel + speed)  'left wheel
    pause 5
    low portc.1
    pulsout portc.1,(rwheel - speed)   'right wheel
    pause 5

goto loop

back:    'PWM to drive servos backward
    low portc.0
    pulsout portc.0,(lwheel - speed)   'left wheel
    pause 5
    low portc.1
    pulsout portc.1,(rwheel + speed)  'right wheel
    pause 5

goto loop


Pic Basic Program to control balibot. (Use editor to view)  Balibot.bas

Hex file for 16f876    ETbalibot.hex

Schematic (PDF)   balibot.pdf

Links to balancing robots:

Nbot          David Anderson
LegWay     Steve Hassenplug     Robot built with Legos
Joe             Built by Students at LEI (french)
Gyrobot     Larry Barello

Ted Larson's Balancing Robot
Shown at the July 27, 2003 San Francisco Robotics Society Expo