Self-balancing robot explained: MOBO

Marta Bernardino
6 min readFeb 12, 2023
MOBO standing up;

This is MOBO, a self-balancing automated vehicle that balances itself without any support. This project had the purpose of exploring inverse kinematics using a PID controller. This is a tiny robot based on the Arduino UNO development board and the MPU6050 accelerometer-gyroscope module.

You might be wondering how Mobo holds itself standing. Well, it’s pretty simple:

The physics:

A self-balancing robot is similar to an upside-down pendulum. Unlike a normal pendulum which keeps on swinging once given moved, this inverted pendulum cannot stay balanced on its own. It will simply fall over, but you can imagine balancing a stick on your finger which is a classic example of balancing an inverted pendulum. We move our finger in the direction in which the stick is falling, right?

This robot works the same way, only that the robot will only fall either forward or backward. So, just like how we move our fingers to balance a stick, the robot does the same by driving its wheels in the direction in which it is falling.

What we are trying to do here, by balancing the stick, is to keep the center of gravity exactly above the pivot point, this process is called inverse kinematics because the robot does the opposite of its tendency.

Hardware

Parts:

  • Arduino Uno
  • MPU6050
  • Two DC plastic gearbox motors
  • L298N Motor driver
  • Li-ion battery
  • A pair of wheels

To make balance easily achievable it’s important that the chassis is built in the flattest way possible but at the same time with the perfect weight distribution. The flat shape won’t tend to fall as much and the weight distribution will prevent any fall tendency and will create enough inertia for it to move.

The robot was built on two layers, the first one is where the motors and its driver are placed, and the top one is where the Arduino UNO board (ATmega328 processor) is placed along with the MPU6050 sensor. The battery is placed on the side, and MOBO’s chassis was built with recycled materials.

Plastic gearbox motors, L298N DC Motor Driver;

The actuators of the robot are the motors and its wheels. MOBO’s are plastic gearbox ones, which are simple dc motors with a reduction so they can move more precisely. This duel shaft motor gives good enough rpm and torque at low operating voltages, which is one of the biggest advantages of these motors. This is super important so they can give a better response to the inclination. It has an operating voltage of 3–6V which is perfect for building small-sized robots.

To drive these motors MOBO has an L298N DC Motor Driver to control their spinning direction and speed. The L298N is a dual H-Bridge motor driver that allows control of two DC motors at the same time. The module can drive DC motors that have voltages between 5 and 35V, with a peak current up to 2A. This speed control is done by the PWM inputs, PWM are values ranging from 0v to 6v (this is MOBO’s range), which translates the quadratic waves input on the driver.

Quadratic waves that go as input to the driver;

These motors cannot be driven directly from the microcontroller, because otherwise, we could not control their spinning direction and speed. Therefore is required a high-current motor driver. It is also important to remind that the motor driver is powered directly from the battery with 7 volts to not underpower the microcontroller.

But how does the robot sense the tilt? For us, it’s easy to sense our body’s balance. But for a self-dancing robot do so is essential to have an MPU6050 sensor. It is placed in the middle of the highest platform to be the most sensitive to the inclination.

MPU6050 digital accelerometer and gyroscope;

This is a Digital Accelerometer and Gyroscope, each with three-axis x, y, and z (three-axis accelerometer and a three-axis gyroscope). This module is basically sensitive to acceleration, velocity, orientation, displacement, and many other motion-related parameters.

This is what will tell the robot how much the robot has tilted, and the speed with which it is falling. The software combines all these inputs and generates a signal which drives the motors and keeps the robot balanced. Basically, whenever the robot falls, the MPU sensor tells the robot how fast it is falling and in which direction, and then it goes back to place.

Measuring the angle of inclination;

The acceleration parameters received will be the PWM input on the driver, which means that the faster that the robot falls the faster it will move. The gyroscope parameters received will only indicate whether the robot will move forward, which means that depending on the angle of the tilt it will decide in what direction to move.

Software

Now the magic trick behind all this comes up. A self-balancing robot uses a “closed-loop feedback control” software, in other words, real-time data from the MPU sensor to control the motors and fastly compensate for any tilting motion to keep the robot standing. Here is how:

So, as I mentioned, the MPU 6050 gives certain information to the robot, but the way this robot processes that information is by using a controller called PID. PID stands for proportional integral derivate. Each of these terms provides a specific response to the robot.

The P (proportional), generates a response that is proportional to the error. For MOBO’s software, the error is the angle of inclination of the robot.

The I (integral), generates a response based on the accumulated error. This is the sum of all the errors multiplied by the sampling period. This is a response based on the past behavior of the robot.

The D (derivative) term is proportional to the derivative of the error. This is the difference between the current error and the previous error divided by the sampling period. This predicts how the robot might behave in the next milliseconds.

These letters actually represent values named Kp Ki Kd, which summed up, generate the output which is then sent as a command to drive the motor.

Output formula;

This is because, in a perfect world, it would be enough to tell the robot that once the tilt is inferior or bigger than 0 it has to go back to place. But that is hardly the case. While the robot works, there is an error that is being added, so the conditions to stay balanced cannot be constant.

Values tested on MOBO; Used libraries;

These calculations are done by libraries to shorten the code, that were previously installed.

PID controllers and inverse kinematics are everywhere, they are super important because, for example, that is what prevents drones from falling and the average humanoids from doing the same. A good example is the Segway’s self-balancing scooter.

To sum up the better the PID values the better the stability.

Watch the video on YouTube!

https://youtu.be/hzlG45UNEt4

Sources:

Building tutorial: https://youtu.be/lrbtAVTKnJo

PID constants: https://www.arrow.com/en/research-and-events/articles/pid-controller-basics-and-tutorial-pid-implementation-in-arduino

MPU6050 interfacing with Arduino: https://circuitdigest.com/microcontroller-projects/interfacing-mpu6050-module-with-arduino

More about Self-balancing robots:

--

--

Marta Bernardino

Robotics innovator| Activate at The Knowledge Society | High school finalist | Poet amateur Street artist amateur https://linktr.ee/martabernardino