What is obstacle-avoiding robot?
An obstacle-avoiding robot is a type of autonomous robot designed to navigate its environment and avoid obstacles in its path. It utilizes various sensors, such as ultrasonic sensors or infrared sensors, to detect obstacles and make decisions on how to avoid them. The robot typically incorporates a microcontroller or a programmable board, such as Arduino, Raspberry Pi, or similar platforms, to control its movements and make real-time decisions based on the sensor input.
Obstacle-avoiding robots are commonly used in various applications, including robotic vacuum cleaners, autonomous drones, warehouse robots, and robotic vehicles. They provide a level of autonomy and intelligence that allows them to operate in dynamic environments without constant human intervention.
How does it works?
The robot relies on sensors to detect obstacles in its surroundings, which can be achieved using ultrasonic sensors to measure object distance or infrared sensors to identify objects based on reflections. The sensor data is analyzed by a microcontroller or programmable board to determine the appropriate action for avoiding the obstacle, such as turning left or right, stopping, or adjusting speed. Based on these decisions, the robot’s motors or actuators are controlled accordingly. It may change direction, reduce speed, or come to a halt to prevent collisions. By continuously sensing its environment and making decisions, the robot maintains movement while actively avoiding obstacles in its path. This process of sensing, decision making, and movement is repeated to navigate the environment and ensure collision-free operation.
Obstacle avoiding robot by Arduino Uno, Servo motor and an Ultrasonic sensor:
The remaining part of the article will provide instructions on constructing an obstacle avoidance robot using Arduino Uno R3 and the HC-SR04 ultrasonic sensor.
Required Components
1x Arduino UNO Rev.03
1x USB-B Cable
1x Solid Core Jumper Wires
1x L298N Motor Driver Module
1x 9V Battery
1x 9V Battery Snap
2x DC Motor
1x HC-SR04 Ultrasonic Sensor
1x Servo Motor
Buy these components on EEmentor Shop
Circuit Diagram
This obstacle avoidance robot will utilize an HC-SR04 ultrasonic sensor that will be attached to a servo motor, allowing the sensor to scan its surroundings. The ultrasonic sensor will measure the distance to nearby objects, and if an object is detected within 15 centimeters, the robot will come to a stop. It will then survey the area, turning towards a direction where no obstacles are detected, and proceed to move in that direction.
To control the speed and direction of the robot, we will need two DC motors, as we are using a 2-wheel car chassis. The interface between the DC motors and the Arduino will be facilitated by the L298N DC motor driver, which allows for easy motor control. The L298N motor driver is a suitable option for rotating motors in either a clockwise or counterclockwise direction. It enables the robot to turn right, left, reverse, and move straight. As battery source here used a 9V DC battery.
Download link of NewPing Library – https://bitbucket.org/teckel12/arduino-new-ping/downloads/
Code
//https://www.eementor.co
#include <Servo.h> //Servo motor library. This is standard library
#include <NewPing.h> //Ultrasonic sensor function library. You must install this library
// L298N control pins
const int Right_MotorForward = 4;
const int Right_MotorBackward = 5;
const int Left_MotorBackward = 6;
const int Left_MotorForward = 7;
//Sensor pins
#define trig_pin A2 //Analog input 1
#define echo_pin A1 //Analog input 2
#define distance_maximum 150
Int distance = 100;
boolean is_Forward = false;
NewPing sonar(trig_pin, echo_pin, distance_maximum); //Sensor function
Servo servo_motor; // servo name
void setup()
{
Serial.begin(9600);
pinMode(Right_MotorForward, OUTPUT);
pinMode(Left_MotorForward, OUTPUT);
pinMode(Left_MotorBackward, OUTPUT);
pinMode(Right_MotorBackward, OUTPUT);
servo_motor.attach(8); // servo pin
servo_motor.write(95);
delay(2000);
distance = read_Ping();
delay(100);
distance = read_Ping();
delay(100);
distance = read_Ping();
delay(100);
distance = read_Ping();
delay(100);
}
void loop()
{
int distance_Right = 0;
int distance_Left = 0;
delay(50);
if (distance <= 20)
{
Stop_Mooving();
delay(300);
Backward();
delay(400);
Stop_Mooving();
delay(300);
distance_Right = Right_check();
delay(300);
distance_Left = Left_check();
delay(300);
if (distance >= distance_Left)
{
Right_turn();
Stop_Mooving();
}
else
{
Left_turn();
Stop_Mooving();
}
}
else
{
Forward();
}
distance = read_Ping();
}
int Right_check()
{
servo_motor.write(20);
delay(500);
int distance = read_Ping();
delay(100);
servo_motor.write(95);
return distance;
}
int Left_check()
{
servo_motor.write(170);
delay(500);
int distance = read_Ping();
delay(100);
servo_motor.write(95);
return distance;
delay(100);
}
int read_Ping()
{
delay(70);
int cm = sonar.ping_cm();
Serial.print("Ping: ");
Serial.print(cm); // Send ping, get distance in cm and print result
Serial.println("cm");
if (cm==0){
cm=250;
}
return cm;
}
void Stop_Mooving()
{
Serial.println("STOP");
digitalWrite(Right_MotorForward, LOW);
digitalWrite(Left_MotorForward, LOW);
digitalWrite(Right_MotorBackward, LOW);
digitalWrite(Left_MotorBackward, LOW);
}
void Forward()
{
Serial.println("Forward");
if(!is_Forward){
is_Forward=true;
digitalWrite(Left_MotorForward, HIGH);
digitalWrite(Right_MotorForward, HIGH);
digitalWrite(Left_MotorBackward, LOW);
digitalWrite(Right_MotorBackward, LOW);
}
}
void Backward()
{
Serial.println("Back");
is_Forward=false;
digitalWrite(Left_MotorBackward, HIGH);
digitalWrite(Right_MotorBackward, HIGH);
digitalWrite(Left_MotorForward, LOW);
digitalWrite(Right_MotorForward, LOW);
}
void Right_turn()
{
Serial.println("TurnRight");
digitalWrite(Left_MotorForward, HIGH);
digitalWrite(Right_MotorBackward, HIGH);
digitalWrite(Left_MotorBackward, LOW);
digitalWrite(Right_MotorForward, LOW);
delay(500);
digitalWrite(Left_MotorForward, HIGH);
digitalWrite(Right_MotorForward, HIGH);
digitalWrite(Left_MotorBackward, LOW);
digitalWrite(Right_MotorBackward, LOW);
}
void Left_turn()
{
Serial.println("TurnLeft");
digitalWrite(Left_MotorBackward, HIGH);
digitalWrite(Right_MotorForward, HIGH);
digitalWrite(Left_MotorForward, LOW);
digitalWrite(Right_MotorBackward, LOW);
delay(500);
digitalWrite(Left_MotorForward, HIGH);
digitalWrite(Right_MotorForward, HIGH);
digitalWrite(Left_MotorBackward, LOW);
digitalWrite(Right_MotorBackward, LOW);
}
Code Explanation
Library for servo motor and ultrasonic sensor.
Define L298N motor driver control pins.
Define analog inputs of ultrasonic Trigger pin and Echo pin.
Setting the initial variable values.
Function calling for sensor and motor.
Enter into void setup. Setup the pin modes and servo motor pins.
In the void loop function, the robot will check if the detected distance is below 20. If it is, the robot will proceed to scan the left and right directions to determine the presence of any obstacles.
If the distance to the left is equal to or less than the distance to the right,the robot will make a right turn. Conversely, if the distance to the left is greater than the distance to the right, the robot will make a left turn.
Otherwise the robot will move in forward direction.
This is servo right and left check function.