What it shows
An inverted pendulum falls over unless something keeps correcting it. The robot measures its tilt hundreds of times a second and drives the wheels under the falling body, which is feedback control with a PID algorithm.
The science
The accelerometer gives tilt from gravity but is noisy, and the gyro gives smooth rotation rate that drifts. A complementary filter combines them: angle = 0.98 x (angle + gyro x dt) + 0.02 x accelerometer angle. The PID controller sets the motor speed from three terms: proportional to the angle error, integral of the error and derivative (the gyro rate). Driving the wheels towards the lean puts them back under the centre of mass.
Materials
Cost: under Rs 1,500. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.
- An Arduino Uno or Nano
- An MPU6050 accelerometer and gyro module
- An L298N motor driver
- Two 6 V to 12 V gear motors with 65 mm wheels
- A two-cell 18650 holder (7.4 V) with protected cells and a switch
- A chassis of acrylic or wood with standoffs
- Jumper wires, nuts and bolts
Safety
For this project: Charge the lithium cells only with a proper charger and an adult’s help; never short them. Keep fingers away from the wheels while it balances or falls. Put the robot on a stand while tuning.
Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.
All the safety rulesStep by step
8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.
Build the chassis with the batteries low and the motors at the bottom; keep the body tall and the weight high, which makes balancing easier.
Fix the MPU6050 near the axle line, flat and tight, and connect it to 5 V, GND, A4 (SDA) and A5 (SCL).
Connect the L298N inputs to pins 5, 6, 9 and 10 with the PWM pins used for speed, and its power to the 7.4 V pack.
Read the MPU6050 and print accelerometer and gyro values to check they respond when you tilt the board.
Write the complementary filter and print the angle; it should read about 0 when the robot is held upright.
Add the PID: speed = Kp x angle + Ki x sum + Kd x gyro rate, and send it to both motors with direction by sign.
Start with Kp only, raise it until the robot oscillates, then add Kd to damp it, and a little Ki at the end.
Hold the robot, release it gently and increase the motor dead zone value until it responds to small angles.
What you should see
The robot stands upright by itself for at least a minute and recovers from a gentle push.
Viva questions
A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.
1Why is a self-balancing robot unstable without control?
Its centre of mass is above the axle, like an inverted pendulum.
2Why combine a gyro with an accelerometer?
The accelerometer is noisy but does not drift; the gyro is smooth but drifts, so the filter uses both.
3What does the D term do?
It damps the motion by reacting to how fast the angle is changing.
4How should tuning begin?
With Ki and Kd at zero, raising Kp until it just oscillates.
Take it further
Add a Bluetooth module so a phone can steer the balancing robot forward, back and in turns.
Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.
Free ebooks for this project
Robotics
Motors, Gears and Motor Drivers
Make things move: DC motors, servos, steppers and how to drive them
Robotics
Robotics and Electronics Basics Study Book
The complete study notes, practice questions and glossary for Robotics and Electronics Basics
