How to make an obstacle avoiding robot with three ultrasonic sensors

How to make an obstacle avoiding robot with three ultrasonic sensors

Hello and welcome back. In this project, we will learn how to make an obstacle-avoiding robot with three ultrasonic sensors. I think this is a new experience for you. Because we usually make this robot using one ultrasonic sensor. Also, a servo motor is used to turn the ultrasonic sensor left and right. But today we will use three ultrasonic sensors for that process. Also, It included four gear motors and these are controlled using an L293D motor driver shield. Finally, I want to say that all these components are controlled by the Arduino UNO board. I hope you know about the ultrasonic sensor. If not, you can use this link to learn more about ultrasonic sensors. OK, let’s go ahead.

Ok, let’s do this project step by step. The required components are given below.

Step 1

Firstly, identify these components.

Step 2

Secondly, let’s create the car chassis. For that, I used a piece of rigifoam. You can use any other material for this chassis.

Step 3

Thirdly, attach the gear motors as follows.

Step 4

Next, mount the ultrasonic sensors to the front of the chassis.

Step 5

Now, connect the L293D motor shield to the Arduino UNO board. Then attach it to the top of the chassis and drill holes on both sides. Next, put the motor wires through those holes.

Step 6

Ok, now connect the motors and ultrasonic sensors to the motor driver shield. For that, use the circuit diagram below.

How to make an obstacle avoiding robot with three ultrasonic sensors

Step 7

Then, attach the battery holder to the top of the chassis and connect it to the motor driver shield.

Step 8

After that, connect the wheels for the gear motors. Then connect this robot car to the computer.

Step 9

Ok, now let’s upload the program for this robot. It is as follows.

/*Obstacle avoidance robot with three ultrasonic sensors
 * http://srituhobby.com
 */
//Include the motor driver library
#include <AFMotor.h>
//Define the sensor pins
#define S1Trig A0
#define S2Trig A1
#define S3Trig A2
#define S1Echo A3
#define S2Echo A4
#define S3Echo A5
//Set the speed of the motors
#define Speed 160

//Create objects for the motors
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

void setup() {
  Serial.begin(9600);
  //Set the Trig pins as output pins
  pinMode(S1Trig, OUTPUT);
  pinMode(S2Trig, OUTPUT);
  pinMode(S3Trig, OUTPUT);
  //Set the Echo pins as input pins
  pinMode(S1Echo, INPUT);
  pinMode(S2Echo, INPUT);
  pinMode(S3Echo, INPUT);
  //Set the speed of the motors
  motor1.setSpeed(Speed);
  motor2.setSpeed(Speed);
  motor3.setSpeed(Speed);
  motor4.setSpeed(Speed);
}

void loop() {
  int centerSensor = sensorTwo();
  int leftSensor = sensorOne();
  int rightSensor = sensorThree();
// Check the distance using the IF condition
  if (8 >= centerSensor) {
    Stop();
    Serial.println("Stop");
    delay(1000);
    if (leftSensor > rightSensor) {
      left();
      Serial.println("Left");
      delay(500);
    } else {
      right();
      Serial.println("Right");
      delay(500);
    }
  }
  Serial.println("Forward");
  forward();
}

//Get the sensor values
int sensorOne() {
  //pulse output
  digitalWrite(S1Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(S1Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S1Trig, LOW);

  long t = pulseIn(S1Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}

//Get the sensor values
int sensorTwo() {
  //pulse output
  digitalWrite(S2Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(S2Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S2Trig, LOW);

  long t = pulseIn(S2Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}

//Get the sensor values
int sensorThree() {
  //pulse output
  digitalWrite(S3Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(S3Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S3Trig, LOW);

  long t = pulseIn(S3Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}

/*******************Motor functions**********************/
void forward() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}
void left() {
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}
void right() {
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}
void Stop() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

Step 10

Now, select board and port. After, upload this code to the Arduino board.

Step 11

Finally, put the batteries to the battery holder and power ON this robot car. Then enjoy this project. The full video guide is below. So, see you in the next project.

How to make an obstacle avoiding robot with three ultrasonic sensors

1 thought on “How to make an obstacle avoiding robot with three ultrasonic sensors”

  1. i am getting this error while trying to upload the code
    Arduino: 1.8.13 (Windows 10), Board: “Arduino Uno”

    obstacle_avoidance_robot_with_three_U._Sensor:5:10: fatal error: AFMotor.h: No such file or directory

    #include

    ^~~~~~~~~~~

    compilation terminated.

    exit status 1

    AFMotor.h: No such file or directory

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

Leave a Comment

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

Shopping Cart