Advertisement

Responsive Advertisement

Arduino Self-Balancing Robot with Ultrasonic Sensor: A Fun and Challenging DIY Project

Have you ever wanted to make your own robot that can balance itself on two wheels and follow a person around? If so, this project is for you! In this article, we will show you how to make an Arduino self-balancing robot with ultrasonic sensor using simple and affordable components. This robot is a two-wheeled vehicle that uses a gyroscope and an accelerometer to measure its tilt and adjust its speed accordingly. It also uses an ultrasonic sensor to detect the distance to the person in front of it and move towards or away from them. It also has a 3D printed frame that gives it a sturdy and stylish appearance. You can customize the frame with different colors and shapes to make your own unique robot.

What You Will Need

To make this Arduino self-balancing robot with ultrasonic sensor, you will need the following components:

  • An Arduino Uno board: This is the brain of the robot. It controls the DC motors and the sensors. You can use any other Arduino board, but the Uno is the most common and compatible one.
  • A MPU-6050 module: This is the balance sensor of the robot. It combines a gyroscope and an accelerometer to measure the angular velocity and acceleration of the robot. It can also calculate the angle of the robot using a complementary filter algorithm.
  • A HC-SR04 ultrasonic sensor: This is the eyes of the robot. It emits ultrasonic waves and measures the time it takes for them to bounce back from an object. This way, it can calculate the distance to the object and avoid collisions.
  • Two DC motors with wheels: These are the feet of the robot. They provide the motion and traction for the robot. You can use any DC motors with wheels that suit your preference, but make sure they have enough torque and speed for your robot.
  • A L298N motor driver module: This is the muscle of the robot. It allows the Arduino board to control the speed and direction of the DC motors. It can also provide the necessary voltage and current for the motors.
  • A 9V battery or any battery ranging from 7V to 12V: This is the power source of the robot. It provides the necessary voltage and current for the Arduino board and the motor driver module.
  • A breadboard and some jumper wires: These are the tools for connecting the components. They make the wiring easy and neat.
  • A 3D printer and some filament: These are the tools for making the frame of the robot. You can use any 3D printer and any filament that suits your preference. We will provide the STL files for the frame in the next section.
  • Some screws, nuts, and glue: These are the tools for assembling the robot. They help to secure the components and the frame together.

How to Make the Arduino Self-Balancing Robot with Ultrasonic Sensor

To make the Arduino self-balancing robot with ultrasonic sensor, you will need to follow these steps:

  1. Download and print the STL files for the frame of the robot. You can find them here. There are four parts: the base part that holds the battery, the breadboard, and the motor driver module, the top part that holds the Arduino board and the sensors, and the two side parts that hold the DC motors and the wheels. You can print them with any color and infill that you like. Make sure to print them with enough support and resolution for a good quality.
  2. Connect the components according to the circuit diagram below. You can use the breadboard and the jumper wires to make the connections. Make sure to follow the polarity and the pin numbers correctly. The circuit diagram is as follows:

Circuit diagram

The pin connections are as follows:

  • Arduino Uno pin 5V -> Motor driver module VCC pin
  • Arduino Uno pin GND -> Motor driver module GND pin
  • Arduino Uno pin 2 -> Motor driver module IN1 pin
  • Arduino Uno pin 3 -> Motor driver module IN2 pin
  • Arduino Uno pin 4 -> Motor driver module IN3 pin
  • Arduino Uno pin 5 -> Motor driver module IN4 pin
  • Arduino Uno pin A4 -> MPU-6050 SDA pin
  • Arduino Uno pin A5 -> MPU-6050 SCL pin
  • Arduino Uno pin 6 -> Ultrasonic sensor trigger pin
  • Arduino Uno pin 7 -> Ultrasonic sensor echo pin
  • Arduino Uno VIN pin -> Battery positive terminal
  • Arduino Uno GND pin -> Battery negative terminal
  • Motor driver module OUT1 pin -> DC motor 1 positive terminal
  • Motor driver module OUT2 pin -> DC motor 1 negative terminal
  • Motor driver module OUT3 pin -> DC motor 2 positive terminal
  • Motor driver module OUT4 pin -> DC motor 2 negative terminal
  • Motor driver module 12V pin -> Battery positive terminal
  • Motor driver module GND pin -> Battery negative terminal

  • Upload the code to the Arduino board. You can use the Arduino IDE or any other software that supports Arduino programming. The code is as follows:

```c // Include the MPU-6050 library

#include

#include

// Create a MPU-6050 object MPU6050 mpu6050(Wire);

// Define the pins for the motor driver module

#define IN1 2

#define IN2 3

#define IN3 4

#define IN4 5

// Define the pins for the ultrasonic sensor

#define TRIG_PIN 6

#define ECHO_PIN 7

// Define the PID constants

#define Kp 21

#define Ki 0

#define Kd 0.8

// Define the threshold distance for obstacle detection

#define DISTANCE_THRESHOLD 30

// Define the variables for the PID controller double pidOutput = 0; double pidError = 0; double pidLastError = 0; double pidIntegral = 0; double pidDerivative = 0;

// Define the variables for the angle measurement double angle = 0; double angleOffset = 0;

// Define the variables for the distance measurement int distance = 0; long duration = 0;

// Define the variables for the motor speed int motorSpeed = 0; int motorSpeed1 = 0; int motorSpeed2 = 0;

void setup() { // Initialize the serial monitor Serial.begin(9600);

// Initialize the I2C communication Wire.begin();

// Initialize the MPU-6050 sensor mpu6050.begin(); mpu6050.calcGyroOffsets(true);

// Initialize the motor driver pins pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);

// Initialize the ultrasonic sensor pins pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT);

// Stop the motors stopMotors();

// Wait for 2 seconds delay(2000);

// Calculate the angle offset for (int i = 0; i < 3000; i++) { mpu6050.update(); angleOffset += mpu6050.getAngleZ(); delay(1); } angleOffset /= 3000; }

void loop() { // Update the MPU-6050 sensor mpu6050.update();

// Calculate the current angle angle = mpu6050.getAngleZ() - angleOffset;

// Calculate the PID error pidError = angle;

// Calculate the PID integral pidIntegral = pidIntegral + pidError;

// Calculate the PID derivative pidDerivative = pidError - pidLastError;

// Calculate the PID output pidOutput = (Kp pidError) + (Ki pidIntegral) + (Kd * pidDerivative);

// Update the PID last error pidLastError = pidError;

// Calculate the motor speed motorSpeed = abs(pidOutput);

// Constrain the motor speed motorSpeed = constrain(motorSpeed, 0, 255);

// Measure the distance to the obstacle distance = measureDistance();

// Check if the distance is less than the threshold if (distance < DISTANCE_THRESHOLD) { // Stop the motors stopMotors();

// Wait for a second
delay(1000);

} else { // Check the PID output sign if (pidOutput > 0) { // Move forward moveForward(); } else { // Move backward moveBackward(); } } }

// A function to move the robot forward void moveForward() { // Set the motor driver pins digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);

// Set the motor speed analogWrite(ENB, motorSpeed); analogWrite(ENA, motorSpeed); }

// A function to move the robot backward void moveBackward() { // Set the motor driver pins digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);

// Set the motor speed analogWrite(ENB, motorSpeed); analogWrite(ENA, motorSpeed); }

// A function to stop the robot void stopMotors() { // Set the motor driver pins digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);

Source: (1) How to Create an Object Following Robot With Arduino. https://maker.pro/arduino/tutorial/an-ultrasonic-object-following-robot. (2) Wheeled Self-Balancing Robot | With Arduino Uno, L293D & Ultrasonic .... https://www.instructables.com/Wheeled-Self-Balancing-Robot-With-Arduino-Uno-L293/. (3) Arduino Based Self Balancing Bot : 6 Steps (with Pictures .... https://www.instructables.com/Arduino-Based-Self-Balancing-Bot/. (4) Obstacle Avoiding Bot Using Ultrasonic Sensor and Arduino. https://www.instructables.com/Obstacle-Avoiding-Bot-Using-Ultrasonic-Sensor-and-/. (5) Human Following Robot Using Arduino and Ultrasonic Sensor - Circuit Digest. https://circuitdigest.com/microcontroller-projects/human-following-robot-using-arduino-and-ultrasonic-sensor. (6) undefined. https://amzn.to/33U9phD. (7) undefined. https://amzn.to/2OULNoC. (8) undefined. https://amzn.to/2qwAGZW. (9) undefined. https://amzn.to/34SjU6i. (10) undefined. https://amzn.to/2s5c3Uh.

Post a Comment

0 Comments