OBSTACLE AVOIDING ROBOT

What are we going to build?

We will build a simple obstacle avoiding robot that will avoid obstacles which are in its path.

RE

There can be two possible robots that can be built:-

1. A simple robot with no rotation of the distance sensor.

2. A mediocre robot with the rotation of the distance sensor.

Let’s gather the components needed:-

1. Arduino mega. (or any other Arduino i.e Arduino Nano or Arduino UNO).
2. L298N Dual H Bridge Stepper Motor Driver.
3. DC hobby BO motors.  
4)HC-SR04 Ultrasonic Distance Measuring Sensor Module
5)9v battery
6)Robot chassis
Obstacle avoiding robot Circuit Diagram.
Connections of Ultrasonic sensor –
1.         VCC – VCC terminal of Arduino.
2.         GND – GND terminal of Arduino.
3.         Trigpin – digital pin 9 on Arduino.
4.         Echo pin – digital pin 10 on Arduino.
Connections of L298N –
1.          +12V – Positive terminal of the battery.
2.         GND – a)GND of Arduino b)Negative terminal of the battery.
3.         Input terminal 1 – Pin 4
4.         Input terminal 2 – Pin 5
5.         Input terminal 3 – Pin 6
6.         Input terminal 4 – Pin 7
7.         The output terminal 1 – Positive of the first motor.
8.         The output terminal 2 – Negative of the first motor.
9.         The output terminal 3 – Positive of the second motor.
10.  The output terminal 4 – Negative of the second motor.

Code

int trigPin = 9;
int echoPin = 10;
int revright = 4;  //REVerse motion of Right motor
int fwdleft = 7; 
int revleft= 6;  
int fwdright= 5;   //ForWarD motion of Right motor
int c = 0;
void setup() {
  //Serial.begin(9600);
   pinMode(5, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(7, OUTPUT);
   pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // put your setup code here, to run once:
}
void loop() {
  long duration, distance;
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin, HIGH);
  distance =(duration/2)/29.1;
  //Serial.print(distance);
  //Serial.println("CM");
  delay(50);
if(distance>20)
 {
forward();
 }
  else if(distance<20) 
 {
 left();
delay(200);
}
}
void forward() {                        
  digitalWrite(7, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(5, HIGH);
  digitalWrite(4, LOW);
}
void backward() {
  digitalWrite(7, LOW);
  digitalWrite(6, HIGH);
  digitalWrite(5, LOW);
  digitalWrite(4, HIGH);
}
void right() {
  digitalWrite(7, LOW);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, LOW);
}
void left() {
  digitalWrite(7, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, HIGH);
}
void stopp() {
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
}
Select the appropriate board in the Arduino ide
Select the com port to which the Arduino board is plugged into and upload the code

Obstacle avoiding Robot Explanation

First, we will be finding the distance between the robot and its surroundings. For this, we have the ultrasonic sensor module which can measure distances from 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. The ultrasonic sensor emits a high-frequency sound pulse and calculates the distance depending upon the time taken by the echo signal to travel back after reflecting from the desired target. The speed of sound is 341 meters per second in air, knowing this we get the duration of the pulse from the pulse function and we divide it by 2 since the duration output not only contains time taken for the pulse to hit the obstacle but also contains the time taken for the high-frequency pulse to return. This corresponds to about 29.412µs (microseconds) per centimeter. To measure the distance the sound has traveled we use the formula: Distance = (Time x SpeedOfSound) / 2.The better way to get the distance as centimetres is to use the formula: Centimetres= ((Microseconds / 2) / 29).
After getting the distance we will use the distance to perform obstacle avoidance by sending commands from the microcontroller to the H-bridge motor driver.
The robot will move forward only if the distance between the obstacle and robot is greater than 20 centimeters else it’s going to keep on turning left until the robot finds a path with no obstacles up to 20 centimeters of range.
But why do we need a motor driver?
Motor drivers act as an interface between the motors and the control circuits. The motor requires a high amount of current whereas the controller circuit works on low current signals. So the function of motor drivers is to take a low-current control signal and then turn it into a higher-current signal that can drive a motor.

Spread knowledge

1 thought on “OBSTACLE AVOIDING ROBOT”

Leave a Comment

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