Self Balancing Robot using Arduino

Creating a two-wheeled self-balancing robot using Arduino is an exciting and challenging project that can teach you a lot about electronics, programming, and robotics. The essence of this project lies in the synergy between hardware and software. Mechanical components, including motors, wheels, and a sturdy chassis, form the physical foundation, while the Arduino serves as the brain, orchestrating the delicate dance of motor control to keep the robot upright, this guide provides a comprehensive, step-by- step approach to help you navigate the construction, programming, and fine-tuning phases of your self- balancing robot. Here’s a step-by-step guide to get you started:

Components Required for Self Balancing Robot using Arduino

Self Balancing Robot using Arduino
Self Balancing Robot using Arduino

• Arduino Uno R3: The main microcontroller that will process sensor data and control the motors.

• MPU6050 6-axis accelerometer/gyroscope sensor: An accelerometer and gyroscope combo sensor to measure the robot’s tilt and angular velocity.

• Two DC Gear motors: These drive the wheels and are controlled by the motor controller.

• Motor driver L298D: To control the speed and direction of the motors. Popular choices include the L298D motor driver.

• Battery pack 9v or 5v: To power up the entire controller and motor driver.

• Jumper wires: connection purpose

• Construction materials (plywood, acrylic, aluminum)

Hardware Assembly

Build the robot frame: Construct a sturdy frame using plywood, acrylic, or aluminum. The frame should have two upright supports and a platform for mounting the electronics and motors.

Attach the motors: Mount the motors on the frame, ensuring they are aligned properly and have sufficient clearance for the wheels.

Connect the motors to the motor driver board: Use jumper wires to connect the motors to the motor driver board according to the manufacturer’s instructions.

Connect the sensor to the Arduino: Connect the MPU6050 sensor to the Arduino using jumper wires according to the sensor’s datasheet.

Power the system: Connect the battery pack to the Arduino and motor driver board, ensuring proper polarity.

Working of Self Balancing Robot using Arduino

• Sensors: The robot uses an accelerometer and gyroscope to measure its tilt angle and angular velocity. The accelerometer measures the robot’s tilt in the three dimensions (X, Y, Z), while the gyroscope measures the rate of rotation around each axis.

• Microcontroller: The microcontroller, typically an Arduino board, receives data from the sensors and processes it to determine the robot’s current position and orientation. It also calculates the necessary adjustments to maintain balance.

• Motor Control: The microcontroller sends control signals to the motor driver board, which controls the speed and direction of the two motors. The motor driver receives the control signals and adjusts the motor speeds accordingly to execute the desired movement.

• PID Control: The microcontroller employs a PID (Proportional-Integral-Derivative) control algorithm to adjust the motor speeds in real-time. The PID algorithm considers the current angle, the rate of change of the angle, and the error between the target angle and the current angle.

• Balancing Mechanism: When the robot begins to tilt, the sensors detect the change in angle. The microcontroller calculates the necessary adjustments using the PID algorithm and sends control signals to the motor driver. The motor driver adjusts the motor speeds to counteract the tilt and maintain balance.

• Continuous Correction: The PID control algorithm continuously monitors the sensor data and adjusts the motor speeds, accordingly, ensuring that the robot remains upright despite external disturbances or uneven surfaces.

• Feedback Loop: The balance system operates in a feedback loop, constantly receiving sensor data, processing it, and adjusting maintain the desired position. This feedback loop ensures that the robot remains stable and upright.

Circuit Diagram for Self Balancing Robot using Arduino

Connections required for Self Balancing Robot using Arduino

• Connect MPU-6050 SCL – SDA – Int to A5 – A4 – D2 Arduino uno R3.

• Connect Motor Driver L298N I1 – I2 – I3 – I4 to Digital pin 2 – 3 – 4 – 7 Arduino uno R3.

• Connect Motor Driver ENB – ENA -to digital pin 5 – 6 Arduino uno R3.

• Connect Motor Driver Out 1 out 2 to Gear motor 1 Gear motor 2.

• Connect Arduino uno R3 Vin to Positive (+) bread board.

• Connect Arduino uno R3 GND to negative (-) bread board.

• Connect motor driver 12v – GND to Bread board positive (+) – Negative (-).

• Connect 9v or 5v Battery to Bread board positive (+) and Negative (-) terminal.

Software Part for Self Balancing Robot using Arduino :

• Set Up Arduino IDE

• Download and install the Arduino IDE (Integrated Development Environment) from the official Arduino website: Arduino Software.

• Open the Arduino IDE and make sure your Arduino board is selected under the “Tools” menu. Choose the appropriate board type (e.g., Arduino Uno) and the port your board is connected to.

• Step 2: Install MPU6050 Library

• In the Arduino IDE, go to “Sketch” > “Include Library” > “Manage Libraries.”• In the Library Manager, type “MPU6050” into the search bar.

• Look for the “MPU6050” library by Jeff Rowberg and click “Install.”

• Step 3: Write the Control Code

• Open a new sketch in the Arduino IDE and start writing your control code. Use the following pseudocode as a starting point.

• Write the control algorithm: The control algorithm is the heart of the robot’s balancing system. It will continuously read sensor data and adjust the motor speeds to maintain balance.

Self Balancing Robot using Arduino
Self Balancing Robot using Arduino

Code for Self Balancing Robot using Arduino

#include <MPU6050.h>
#include <L298D.h>
const int motorLeftEnablePin = 5;
const int motorLeftPin1 = 2;
const int motorLeftPin2 = 3;
const int motorRightEnablePin = 6;
const int motorRightPin1 = 4;
const int motorRightPin2 = 7;
const int mpuIntPin = 2;
const int mpuSDA = A4;
const int mpuSCL = A5;
L298D motorLeft(motorLeftEnablePin, motorLeftPin1, motorLeftPin2);
L298D motorRight(motorRightEnablePin, motorRightPin1, motorRightPin2);
MPU6050 mpu(mpuIntPin, mpuSDA, mpuSCL);
const float targetAngle = 0.0;
const float kp = 1.0;
const float ki = 0.0;
const float kd = 0.0;
float angle = 0.0;
float gyro = 0.0;
void setup() {
Serial.begin(9600);
mpu.initialize();
mpu.setInterruptPeriod(200);
motorLeft.setSpeed(0);
motorRight.setSpeed(0);
}
void loop() {
mpu.update();
angle = mpu.getAngleX();
gyro = mpu.getGyroX();
float error = targetAngle - angle;
float controlSignal = kp * error + ki * error + kd * gyro;
int leftMotorSpeed = 500 + controlSignal;
int rightMotorSpeed = 500 - controlSignal;
motorLeft.setSpeed(leftMotorSpeed);
motorRight.setSpeed(rightMotorSpeed);
Serial.print("Angle: ");
Serial.print(angle);
Serial.print(" | Gyro: ");
Serial.print(gyro);
Serial.print(" | Control: ");
Serial.println(controlSignal);
delay(10);
}

Testing and Calibration

• Place the robot on a flat surface: Start by testing the robot on a flat, level surface to ensure it can maintain balance without any external disturbances.

• Introduce gradual disturbances: Once the robot is balancing consistently, introduce gradual disturbances to test its responsiveness and ability to recover from imbalances.

• Verify the proper connection of sensors (accelerometer and gyroscope) to the Arduino.

• Confirm the correct wiring of motors and actuators. Check power sources and voltage levels.

Spread knowledge

Leave a Comment

Your email address will not be published. Required fields are marked *