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.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

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
 * https://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

Similar Posts

7 Comments

  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.

  2. I am having the same issues as the comment above

    C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\j4m1e\OneDrive\Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=2341_0043 -ide-version=10813 -build-path C:\Users\j4m1e\AppData\Local\Temp\arduino_build_799560 -warnings=none -build-cache C:\Users\j4m1e\AppData\Local\Temp\arduino_cache_914042 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Users\j4m1e\OneDrive\Desktop\arduino-1.8.12\obstacle_avoidance_robot_with_three_U._Sensor\obstacle_avoidance_robot_with_three_U._Sensor.ino
    C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\j4m1e\OneDrive\Documents\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=2341_0043 -ide-version=10813 -build-path C:\Users\j4m1e\AppData\Local\Temp\arduino_build_799560 -warnings=none -build-cache C:\Users\j4m1e\AppData\Local\Temp\arduino_cache_914042 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Users\j4m1e\OneDrive\Desktop\arduino-1.8.12\obstacle_avoidance_robot_with_three_U._Sensor\obstacle_avoidance_robot_with_three_U._Sensor.ino
    Using board ‘uno’ from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
    Using core ‘arduino’ from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
    Detecting libraries used…
    “C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++” -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR “-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino” “-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\standard” “C:\\Users\\j4m1e\\AppData\\Local\\Temp\\arduino_build_799560\\sketch\\obstacle_avoidance_robot_with_three_U._Sensor.ino.cpp” -o nul -DARDUINO_LIB_DISCOVERY_PHASE
    Alternatives for AFMotor.h: []
    ResolveLibrary(AFMotor.h)with_three_U._Sensor:5:10: fatal error: AFMotor.h: No such file or directory
    #include
    ^~~~~~~~~~~
    compilation terminated.

    -> candidates: []
    exit status 1
    AFMotor.h: No such file or directory

  3. My son’s car drives into the wall and only has two wheels. He commented out motors 2 and 3 and his car still runs into the walls. My son loves stuff like this but I have no idea how any of this stuff works I like that he is trying to build his own. But my knowledge is limited can you help?

Leave a Reply

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