Its function is to maintain balance using the motor’s axis movement of the wheels and body. There are several types of self-balancing robots, and in this particular case I will present you a way to make a Two-Wheeled Balancing Robot. Actually, I made this robot together with one of my students and it is a high school graduation project. This is the simplest type of balancing robot and only requires a few components to build. 

   First, let's explain the components used and their functions.
  - MPU6050 Accelerometer and Gyroscope sensor with Micro Electro Mechanical System(MEMS) technology, which is used to detect angle of tilt or inclination along the X, Y and Z axes.
  - The information from this sensor is transmitted to a microcontroller, and in our case it is an Arduino Uno board. This board is the brain of the robot, processes the sensor data and determines the actions needed to keep the robot upright.
  - Processed  data from the Arduino is sent to the motor driver board, which in our case is the  L298N 5AD type.  A motor driver circuit controls the direction and speed of the motors.
  - This robot uses two DC motors, each connected to a wheel. These motors adjust the speed of the wheels to correct the robot’s tilt.
  - and power source, specifically two Lithium batteries connected in series wich provides energy to the motors and electronics. All components are mounted on a suitable supporting structure made of 5mm thick PVC board.

 This project is sponsored by PCBWay. They has all the services you need to create your project at the best price, whether is a scool project, or complex professional project. On PCBWayyou can share your experiences, or get inspiration for your next project. They also provide completed Surface mount SMT PCB assemblY service at a best price, and ISO9001 quality control. Visit pcbway.com for more services.

  Now, let's briefly explain the working principle: The accelerometer gives a measure of the robot’s orientation relative to gravity, and the gyroscope detects the rate of tilt. The Arduino uses a PID (Proportional, Integral, Derivative) controller to process the data from the sensors.

   The PID controller calculates the necessary speed and direction adjustments needed to maintain the robot’s balance. If the robot tilts forward, the motors spin faster to move the robot forward and prevent a fall. If it tilts backward, the motors move the robot backward to regain balance. As the robot detects an imbalance, it compensates by adjusting the wheel speed, shifting its center of mass, or moving forward or backward to maintain its position.
  A few words about the Arduino code. The method of installing the code has been described many times before, so now I will only focus on the modifications that need to be made individually for each case due to the different characteristics of the components used, as well as the method of their placement.
   Now comes the fun part, which is customizing the code specifically for our built device. Depending on the layout of the components, the center of gravity of the robot changes, and also each MPU6050 board has minimal differences that need to be compensated for in part of the code. If we look at the code in more detail, we will see that there is a place where the Gyro offset is set. 

 In this part of the code, errors are corrected during the manufacturing of the MPU6050 chip. For each individual chip, these values are different. Their value is determined using Arduino code made specifically for this purpose and you can download it at the end of this text.
  Next, we need to move on to setting the Accelerometer. In the "double setpoint' line, we need to put a value that corresponds to our device.

 To obtain this value, we need to hold the robot in an ideal vertical position, perpendicular to the surface, and read the required value on the serial monitor....

Read more »