sw/
← projects
VNIT·2013

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

  1. 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.
  2. Two support bases hold the microcontroller board and the IMU.
  3. An extra weight on top creates a larger inertia to rotation.

Stage 2: Set up and test the sensors and motors

  1. Mount the microcontroller board and the IMU on the chassis and complete the wiring.
  2. Connect the IMU output to an input pin and declare the registers accordingly.
  3. Drive the motors through a motor driver, not directly from the microcontroller, which can supply neither the voltage nor the current they need.
  4. Recheck the IMU power wiring: it is a delicate, sensitive device.
  5. Test the IMU output and send it to HyperTerminal over UART.
  6. Check how the output varies with angle, and calibrate if necessary.
  7. 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

  1. Check the UART output for noise: a sudden drop to 0 or a sudden spike is accelerometer noise.
  2. Plot the output if possible, to see the noise and its effect.
  3. Filter as needed. For mild noise, a simple low-pass filter is enough.
  4. If readings are still noisy, check the connections. If noise persists, use a stronger filter such as a Kalman filter.
  5. Fuse the accelerometer and gyroscope readings to get the robot’s orientation and the magnitude of acceleration.
  6. The fusion factor depends on the system. It combines two outputs, usually one low-frequency and one high-frequency.
  7. The result keeps the best of both and avoids errors such as gyroscopic drift.
  8. Verify readings over UART, for example by logging to a text file and plotting it in another program.

Stage 4: PID control

  1. The fused reading is the input to the PID algorithm.
  2. 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.
  3. Hold the robot vertical and record the sensor reading. That tilt angle is the PID setpoint.
  4. Start with only the proportional term. Add its output continuously to the PWM OCR, and keep the result below 255 to avoid overflow.
  5. For the opposite direction, negate the input so the motors reverse.
  6. 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.
  7. Raise P until the motors are close to bang-bang: almost full speed the moment the robot crosses vertical.
  8. Slowly increase D until the robot balances. The more it tilts, the harder it should accelerate towards the tilt.
  9. Keep it highly reactive. A slow response usually means the robot falls flat on its face. Update at 40–50 Hz or faster.
  10. If it still fails to balance, try adjusting I, again adding to the existing OCR.
  11. 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

  1. Adding weight on top raises the centre of gravity and the rotational inertia, which makes balancing easier.
  2. Add the PID output to the existing PWM value so it simulates acceleration, not velocity.

Warnings

  1. Connect the IMU correctly: it is sensitive and delicate.
  2. Never exceed the IMU’s rated voltage. Even small excursions overheat and destroy it.
  3. 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

  1. 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.
  2. Different offsets on the two motors let it turn by differential drive.
  3. With well-tuned gains it could even climb slopes without toppling.
  4. Bluetooth or Wi-Fi control would make it fully mobile.
esc