Obstacle avoidance Robot car.

Obstacle avoidance Robot car.

Hello guys, welcome back to my SriTuHobby blog. Today we are going to talk about a topic that everyone likes. That is, today we are going to talk about how to build a robot car. Making robots is a big dream of many Arduino learners. This is a topic I like a lot too. For this, we need the knowledge of the previous few articles. If you are new to Arduino. Please read my previous articles. Today we are going to talk about how to make an obstacle avoidance robot car. For this, we can mainly use an ultrasonic sensor. Because we know we can get the distance with the ultrasonic sensor. We also need a few components but this sensor is special. We discuss these components below.

How does this robot car work?

 Once this robot car is activated, a slight delay begins to activate. The first time the ultrasonic sensor is shaking slightly. That way we can see that the robot is active. It also increases the robot’s attractiveness. Afterward, the ultrasonic sensor is activated. At the same time, the four motors begin to rotate forward. That’s how the robot car starts to move forward. Going forward, when the sensor value is less than 10 cm, the four motors stop and rotate backward for half a second. Later, this robot car stops. The sensor is then rotated left and right to calculate the distance. For that uses a servo motor. Next, this servo motor stops at the starting point. Then, using the distance obtained by looking left and right, the robot car turns to the long distance and goes forward. To turn the car, the motors must rotate backward on one side and the motors rotate forward on the other side.

Okay, now we talked about how the robot car works. Let’s look at the components required for this project.

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

Step 1

Let’s identify these components.

Step 2

First, attach the gear motors to the dot board. For that, I used a glue gun. You can use any method.

Obstacle avoidance Robot car.

Step 3

Then, connect the motor drive shield to the Arduino board and attach it to the center of the dot board.

Step 4

Next, connect these four motors to the motor driver shield. For that, use the circuit diagram below.

Step 5

Attach the servo motor to the dot board and connect it to the motor shield. Then, attach the ultrasonic sensor to the top of the servo motor and connect it to the driver shield using the jumper wire. To do this, use the circuit diagram above.

Step 6

Finally, attach the power source to this robot car. For that, I used two lithium-ion batteries. You can use any type of battery. But you have to keep in mind that its voltage ranges from 7 to 12 VDC.

Obstacle avoidance Robot car.

Step 7

Let’s look at this code.

  • The complete program of this project – Download
  • AFMotor library — Download
/*Obstacle avoidance Robot car.
  created by the SriTu Tech team.
  Read the code below and use it for any of your creation
*/
#include <AFMotor.h>
#include <Servo.h>
#define Speed 180
#define Trig A0
#define Echo A1
#define spoint 90
int distance;
int Left;
int Right;
int L = 0;
int R = 0;
Servo servo;
AF_DCMotor M1(1);
AF_DCMotor M2(2);
AF_DCMotor M3(3);
AF_DCMotor M4(4);
 
void setup() {
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  servo.attach(10);
  start();
  M1.setSpeed(Speed);
  M2.setSpeed(Speed);
  M3.setSpeed(Speed);
  M4.setSpeed(Speed);
 
}
 
void loop() {
  distance = ultrasonic();
  if (distance <= 12) {
    Stop();
    backward();
    delay(100);
    Stop();
    L = leftsee();
    servo.write(spoint);
    delay(800);
    R = rightsee();
    servo.write(spoint);
    if (L < R) {
      turnleft();
      delay(500);
      Stop();
      delay(200);
    } else if (L > R) {
      turnright();
      delay(500);
      Stop();
      delay(200);
    }
  } else {
    forward();
  }
}
 
void forward() {
  M1.run(FORWARD);
  M2.run(FORWARD);
  M3.run(FORWARD);
  M4.run(FORWARD);
}
void backward() {
  M1.run(BACKWARD);
  M2.run(BACKWARD);
  M3.run(BACKWARD);
  M4.run(BACKWARD);
}
void turnleft() {
  M1.run(BACKWARD);
  M2.run(BACKWARD);
  M3.run(FORWARD);
  M4.run(FORWARD);
}
void turnright() {
  M1.run(FORWARD);
  M2.run(FORWARD);
  M3.run(BACKWARD);
  M4.run(BACKWARD);
}
void Stop() {
  M1.run(RELEASE);
  M2.run(RELEASE);
  M3.run(RELEASE);
  M4.run(RELEASE);
}
int leftsee() {
  servo.write(20);
  delay(800);
  Left = ultrasonic();
  return Left;
}
 
int rightsee() {
  servo.write(150);
  delay(800);
  Right = ultrasonic();
  return Right;
}
 
int ultrasonic() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(4);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  long t = pulseIn(Echo, HIGH);
  long cm = t / 29 / 2; //time convert distance
  return cm;
}
void start() {
  delay(3000);
  for (int a = 0; a < 4; a++) {
    servo.write(spoint);
    delay(50);
    servo.write(40);
    delay(50);
    servo.write(90);
    delay(50);
    servo.write(spoint);
  }
}

For the first time, we need a library to control the motor driver shield. Please download it using this link and add it to your Arduino IDE. The other codes are described in my previous posts. Please read those articles and understand them.

Step 8

Now, select the board and port. After, click the upload button.

Now, remove the USB cable and turn on the power. If you are using a switch it is easier for you. Now you can see the robot car moving. The full video guide is below. So see you in the next post. Have a good day.

Obstacle avoidance Robot car.

Similar Posts

Leave a Reply

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