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.

Components.

So let’s do this project step by step.

Step 1

Let’s identify these components.

Arduino Uno board

Please use the Arduino Uno board for this project. Because this is good for this project.

Motor driver shield

This component is used to control the four motors. Click this link for more details.

Ultrasonic sensor.

This component is used to get the distance. Click this link for more details.

Servo motor

This is used to rotate the ultrasonic sensor. Click this link for more details.

Gear motors and wheels.

These components we want to move the robot car.

Dot board

Next, we need a robot car chassis. For that, I use the dot board. But you can use any car chassis.

Li-ion battery and holder               

You can use any kind of power source. But remember to use a power source from 7 to 12 DC.

Jumper wires

Use these jumper wires to connect the ultrasonic sensor to the motor shield.




Step 2

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

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 li-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.




Step 7

Let’s look at this code.
The complete program of this project – 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);
  }
}
Download this code  —  Click me
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 understanding them.

Step 8

Select the correct board and port.




Step 9

Upload this code and enjoy it.
Now we can remove the USB cable and give it the power we set. It’s easy for you if you use a switch. Now we can see the Robot car moving. Below is a video related to this project. Please watch it. Please comment below on your issues. So we will meet in the next post. Have a good day. Bye-bye.

Obstacle avoidance Robot car.

Similar Posts

Leave a Reply

Your email address will not be published.