Loading...

Self balancing robot using Arduino | Arduino Geek


Hello everyone! Welcome to Arduino Geek. Today we will discuss about an Arduino based Self balancing robot. So let's get started.

Self balancing robot using Arduino:

A self-balancing robot is a type of robot that can balance itself on two wheels without falling over. It requires precise control and sensing of the robot's orientation and movement. Arduino is a popular microcontroller board that is widely used for building such robots.

Materials:

Arduino board (such as the Arduino UNO or Arduino Nano)
IMU (Inertial Measurement Unit) sensor
Motor driver board
Two DC motors
Battery pack
Chassis (can be 3D printed or purchased)
Wheels

How to make a self balancing robot:

Here are the general steps for building a self-balancing robot using an Arduino:

1. Hardware

You'll need an Arduino board, motor driver board, two DC motors, a gyro sensor, and a battery. You can also add other components like an LCD screen for displaying data, a Bluetooth module for remote control, and more.

2. Software

You'll need to program the Arduino board using a language like C++ or Python. The code will control the motors and the gyro sensor to maintain the robot's balance.

3. Motor Control

You need to use a motor driver board to control the speed and direction of the motors. You can use the PWM pins of the Arduino to control the motor speed.

4. Gyro Sensor

A gyro sensor measures the rate of rotation of the robot around its axis. It provides data to the Arduino board, which can be used to adjust the motor speed and maintain balance.

5. PID Algorithm

You need to use a PID algorithm to control the motors based on the gyro sensor data. The algorithm calculates an error value based on the difference between the desired angle and the current angle of the robot, and then adjusts the motor speed to reduce the error.

6. Calibration

Before running the robot, you need to calibrate the gyro sensor to measure the zero angle of the robot. You can do this by placing the robot in an upright position and setting the zero angle value in the code.

7. Testing and Tuning

Once you've built and programmed the robot, you need to test it and fine-tune the parameters of the PID algorithm for better performance.

Arduino Code:

Here's an example code that you can use as a starting point:

#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>

#define MOTOR1_PIN1 6
#define MOTOR1_PIN2 5
#define MOTOR2_PIN1 10
#define MOTOR2_PIN2 9

Adafruit_MPU6050 mpu;

float angle, gyro, acc, accAngle, gyroAngle, angleError, previousError, motorSpeed, Kp, Ki, Kd;

void setup() {
  pinMode(MOTOR1_PIN1, OUTPUT);
  pinMode(MOTOR1_PIN2, OUTPUT);
  pinMode(MOTOR2_PIN1, OUTPUT);
  pinMode(MOTOR2_PIN2, OUTPUT);

  Serial.begin(9600);
  
  while (!Serial) {
    delay(10); 
  }

  Serial.println("Initializing MPU6050...");
  mpu.begin();
  Serial.println("MPU6050 initialized.");
  delay(1000);
  
  Kp = 10; // Proportional gain
  Ki = 0.2; // Integral gain
  Kd = 0.1; // Derivative gain
}

void loop() {
  angle = getAngle();
  angleError = angle - 0;
  acc = getAcc();
  accAngle = atan2(acc, sqrt(pow(mpu.getAccelerationY(), 2) + pow(mpu.getAccelerationZ(), 2))) * 180 / M_PI;
  gyro = getGyro();
  gyroAngle += gyro * 0.0000611;
  angle = 0.98 * (angle + gyro * 0.0000611) + 0.02 * accAngle;

  motorSpeed = Kp * angleError + Ki * previousError + Kd * (angleError - previousError);
  previousError = angleError;

  if (motorSpeed > 255) {
    motorSpeed = 255;
  } else if (motorSpeed < -255) {
    motorSpeed = -255;
  }

  if (motorSpeed > 0) {
    analogWrite(MOTOR1_PIN1, motorSpeed);
    analogWrite(MOTOR2_PIN1, motorSpeed);
    digitalWrite(MOTOR1_PIN2, LOW);
    digitalWrite(MOTOR2_PIN2, LOW);
  } else {
    analogWrite(MOTOR1_PIN2, -motorSpeed);
    analogWrite(MOTOR2_PIN2, -motorSpeed);
    digitalWrite(MOTOR1_PIN1, LOW);
    digitalWrite(MOTOR2_PIN1, LOW);
  }

  Serial.print("Angle: ");
  Serial.print(angle);
  Serial.print("   Motor speed: ");
  Serial.println(motorSpeed);
}

float getAngle() {
  float roll = mpu.getRotationX() * 0.0000611;
  float pitch = mpu.getRotationY() * 0.0000611;
  gyro = mpu.getRotationZ() * 0.0000611;
  float temp = mpu.getTemperature() / 340.00 + 36.53;
  return atan2(-roll, -pitch) * 180 / M_PI;
}

float getGyro() {
  return mpu.getRotationZ() * 0.0000611;
}

float getAcc() {
  return mpu.getAccelerationX() * 9.81;
}

Arduino Code Description:

This code reads data from an MPU6050 accelerometer and gyroscope, and uses that data to balance a two-wheeled robot. The robot is controlled by two motors connected to pins 5, 6, 9, and 10 on the Arduino board.
The code calculates the angle of the robot by combining data from the accelerometer and gyroscope. It then calculates the motor speed needed to balance the robot using a PID controller, which adjusts the motor speed based on the difference between the desired angle (0 degrees) and the current angle.

Conclusion:

Building a self-balancing robot using Arduino requires a good understanding of electronics, programming, and control theory. But with the right components and some practice, you can build a cool and functional robot that can balance itself on two wheels.
 
close