Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

Science project idea

Self-balancing two-wheel robot with an MPU6050, complementary filter and PID

A two-wheel robot is an inverted pendulum and falls unless it moves under itself fast enough. The project builds one with a gyroscope and accelerometer, fuses their data, and tunes a PID controller until the robot stands and recovers from pushes, recording the effect of each gain.

What it shows

A two-wheel robot is an inverted pendulum and falls unless it moves under itself fast enough. The project builds one with a gyroscope and accelerometer, fuses their data, and tunes a PID controller until the robot stands and recovers from pushes, recording the effect of each gain.

The science

The accelerometer gives tilt from gravity but is noisy during movement, and the gyroscope gives smooth rate of rotation but drifts. A complementary filter, angle = 0.98 (angle + gyro × dt) + 0.02 × accelerometer angle, combines their strengths. The PID controller computes motor speed from the tilt error: proportional gives a restoring push, integral removes steady lean, and derivative adds damping. Wheels must drive in the direction of the fall.

Materials

Cost: under Rs 5,000. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.

  • An Arduino Nano or Uno
  • An MPU6050 module
  • A TB6612FNG or L298N motor driver
  • Two 12 V geared DC motors (about 200 rpm) with wheels of 6.5 cm
  • A 3 cell Li-ion pack with a protection board and charger, or 8 AA NiMH cells
  • A chassis of acrylic with space for the battery high up
  • A laptop with serial plotter

Safety

For this project: Li-ion packs need a protection board and the right charger, and an adult must supervise charging. Keep the robot away from fingers as the wheels can pinch, and always test on the floor in a clear area.

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 rules

Step by step

12 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.

  1. Assemble the chassis with the battery as high as possible so the centre of mass is high and the pendulum slower.

  2. Connect the MPU6050 over I2C and read the raw accelerometer and gyroscope values on the serial monitor.

  3. Calibrate the gyroscope offset by averaging 500 readings while the robot is still.

  4. Compute tilt from the accelerometer and from the integrated gyroscope and plot both with the serial plotter.

  5. Implement the complementary filter with a loop time of 5 to 10 ms and check that the angle is smooth and drift free.

  6. Connect the motor driver and test that positive control drives the wheels forward when the robot tilts forward.

  7. Write a PID controller with Kp, Ki and Kd variables adjustable through serial commands.

  8. Start with Kp only and increase until the robot oscillates around the vertical.

  9. Add Kd to damp the oscillations, then a small Ki to remove drift.

  10. Record the angle response to a gentle push for three sets of gains and compare overshoot and settling time.

  11. Add a deadband compensation for the motor and a safe cut-off if the angle exceeds 40 degrees.

  12. Write the report with the equations, tuning procedure, graphs of each tuning stage and limits.

What you should see

After tuning, the robot stands for several minutes, recovers from light pushes and moves slightly back and forth. The recordings show that low Kp falls, high Kp oscillates, and added Kd settles the response.

Viva questions

A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.

1Why not use only the accelerometer?

It is disturbed by the robot’s own acceleration, so the tilt reading is noisy.

2What does the derivative term do?

It reacts to the rate of change of error and damps oscillations.

3What is an inverted pendulum?

A pendulum whose mass is above the pivot, which is unstable without control.

4Why does a taller robot balance more easily?

Its fall is slower, so the controller has more time to react.

Free ebook: Viva Questions for Science Projects

Take it further

Add a Bluetooth module and a phone joystick so that the robot can drive and turn while balancing.

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

WhatsApp us