Two-wheel self-balancing robot
A miniature Segway built from a Mechanix kit: IMU fusion and a hand-tuned PID loop keep it upright and push back against nudges. Where it started.
Problem
A two-wheeled robot is an inverted pendulum: left alone, it falls over. Keeping it upright means sensing the tilt continuously and driving the wheels under the falling mass fast enough to catch it, the same principle as a Segway, on a student budget.
What I did
I built the robot in 2013, in my second year at VNIT, and wrote up the build so others could follow it. The chassis is a reconfigurable Mechanix set with two motors, a microcontroller and an IMU, plus a weight on top to raise the rotational inertia and slow the fall.
Sensing. I tested and calibrated the IMU over UART, low-pass filtered the noisy accelerometer, and fused it with the gyroscope using a complementary filter: the accelerometer’s slow but drift-free angle with the gyro’s fast but drifting one.
Control. The fused tilt feeds a PID loop that adds to the motors’ PWM duty rather than setting it, so the controller commands acceleration, a pseudo-torque, instead of speed. I tuned it by hand: P first, up to the edge of bang-bang behaviour, then D until it balanced, then a touch of I, stepping each gain on a log scale. The loop has to run at 40–50 Hz or faster; anything slower and the robot falls flat on its face.
Result
The robot balances on the spot and recovers when pushed, as the video shows. The write-up also sketched the next steps: a PWM offset to drive forward while balancing, differential offsets to turn, and Bluetooth or Wi-Fi control.
Original build notes
Two-Wheel Balancing Robot · Shubham M Wagh, Electrical Engineering, VNIT Nagpur · 12 September 2013
This robot uses sensors to balance itself on two wheels by moving in the required direction. Balancing on two wheels gives a great deal of freedom and mobility, as it can easily avoid obstacles.
Introduction
- Inspired by the Segway, which carries people while balancing on two wheels, this robot is a miniature that does the same.
- It works on the principle of the linear inverted pendulum: a continuous output dynamically balances an unstable mass.
- It senses acceleration continuously and uses a PID control algorithm to create acceleration in the opposite direction.
- That opposite acceleration comes from its two wheels. Motor speed is controlled with pulse-width modulation.
Stage 1: Design of the chassis
- A basic Mechanix set forms the chassis, with a motor on the left and right. The Mechanix base was chosen because it is easy to remove and reconfigure.
- Two support bases hold the microcontroller board and the IMU.
- An extra weight on top creates a larger inertia to rotation.
Stage 2: Set up and test the sensors and motors
- Mount the microcontroller board and the IMU on the chassis and complete the wiring.
- Connect the IMU output to an input pin and declare the registers accordingly.
- Drive the motors through a motor driver, not directly from the microcontroller, which can supply neither the voltage nor the current they need.
- Recheck the IMU power wiring: it is a delicate, sensitive device.
- Test the IMU output and send it to HyperTerminal over UART.
- Check how the output varies with angle, and calibrate if necessary.
- Start the microcontroller’s internal timer and test motor speed control. With PWM, the on and off times of the driver’s enable pin set the motor speed.
Stage 3: IMU filtering and fusion
- Check the UART output for noise: a sudden drop to 0 or a sudden spike is accelerometer noise.
- Plot the output if possible, to see the noise and its effect.
- Filter as needed. For mild noise, a simple low-pass filter is enough.
- If readings are still noisy, check the connections. If noise persists, use a stronger filter such as a Kalman filter.
- Fuse the accelerometer and gyroscope readings to get the robot’s orientation and the magnitude of acceleration.
- The fusion factor depends on the system. It combines two outputs, usually one low-frequency and one high-frequency.
- The result keeps the best of both and avoids errors such as gyroscopic drift.
- Verify readings over UART, for example by logging to a text file and plotting it in another program.
Stage 4: PID control
- The fused reading is the input to the PID algorithm.
- Add the PID output to the motors’ PWM compare value (OCR) rather than setting it. This matters: the output must be acceleration, not velocity, to act as a pseudo-torque that maintains balance.
- Hold the robot vertical and record the sensor reading. That tilt angle is the PID setpoint.
- Start with only the proportional term. Add its output continuously to the PWM OCR, and keep the result below 255 to avoid overflow.
- For the opposite direction, negate the input so the motors reverse.
- Use the sign of the sensor reading, not of the PID output, to choose motor direction, so the robot can move forward and accelerate backward at the same time.
- Raise P until the motors are close to bang-bang: almost full speed the moment the robot crosses vertical.
- Slowly increase D until the robot balances. The more it tilts, the harder it should accelerate towards the tilt.
- Keep it highly reactive. A slow response usually means the robot falls flat on its face. Update at 40–50 Hz or faster.
- If it still fails to balance, try adjusting I, again adding to the existing OCR.
- PID tuning takes time and many trials, but once tuned the robot shrugs off disturbances. Tip: step each gain on a log scale, for example 0.1, 0.33, 1.
Pitfalls and tips
- Adding weight on top raises the centre of gravity and the rotational inertia, which makes balancing easier.
- Add the PID output to the existing PWM value so it simulates acceleration, not velocity.
Warnings
- Connect the IMU correctly: it is sensitive and delicate.
- Never exceed the IMU’s rated voltage. Even small excursions overheat and destroy it.
- Make sure the motor driver (usually an L293D) can handle the motors’ current. If it overheats, switch to a higher-rated driver such as the L298N, and add heatsinks wherever things run hot.
Improvements
- A constant offset on the PWM output adds a steady velocity, so the robot can drive in any direction while balancing, a big step up from balancing on the spot.
- Different offsets on the two motors let it turn by differential drive.
- With well-tuned gains it could even climb slopes without toppling.
- Bluetooth or Wi-Fi control would make it fully mobile.