Hello,

In this tutorial, we are going to learn how to make an obstacle-avoiding car using Arduino.

Hardware Required

Software Required

L293D motor driver

  • L293D is a motor driver integrated chip. It is a dual H-bridge motor driver integrated chip. It also acts as an amplifier. It takes on a low current signal and gives the output of a high current signal. 

Working Concept of the obstacle avoiding car

  • The main component in the operation of the obstacle-avoiding car is the Ultrasonic sensor HC-SR04. This sensor uses the sonar principle to calculate the distance to the objects in front of it. This sensor is used in a lot of projects which require fast and economical distance measurement. This is possible because the sensor is very small in size and can be mounted anywhere on the chassis.

  • When the power supply is connected to the Arduino, the motors start rotating and the car starts moving. As soon as the car reaches near an obstacle, there is a threshold distance set by the user in the Arduino code. If the distance of the obstacle is below this threshold then the ultrasonic sensor sends a signal to the Arduino.

  • As soon as Arduino gets the signal from the ultrasonic sensor, it sends the signal to the motor driver to stop rotating the motors.

  • After this, it again sends the signal to the motor driver to turn the car to the left side. 

  • And the car is turned to the left side till the point when there is no obstacle in front of the car. The car again starts moving forward and moves forward till it finds another obstacle. The above process is repeated till the power supply is cut off.

Circuit Diagram

Pins on Arduino Uno

Pins on Ultrasonic sensor

5V

VCC

GND

GND

Digital pin 10

Echo

Digital pin 11

Trig

Pins on Arduino Uno

Pins on L293D

5V

Pin 1 

GND

Pin 5

Digital pin 2

Pin 10

Digital pin 3

Pin 15

Digital pin 8

Pin 7

Digital pin 9

Pin 2

Arduino Code

const int trigPin = 11;
const int echoPin = 10;
const int in1 = 9;
const int in2 = 8;
const int in3 = 4;
const int in4 = 3;

void setup() 
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode (in1, OUTPUT);
  pinMode (in2, OUTPUT);
  pinMode (in3, OUTPUT);
  pinMode (in4, OUTPUT);
}
long duration, distance;

void loop()
{     
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);  
  duration = pulseIn(echoPin, HIGH);
  distance = duration/58.2;
  if(distance<30)
    {
      digitalWrite(in1, HIGH); 
      digitalWrite(in2, LOW); 
      digitalWrite(in3, LOW); 
      digitalWrite(in4, HIGH);
      delay(1200);
    }
    
  else
    {
      digitalWrite(in1, HIGH); 
      digitalWrite(in2, LOW); 
      digitalWrite(in3, HIGH); 
      digitalWrite(in4, LOW);
    }  
  delay(0);
}
  • You can make some personal changes in the code like the threshold distance as per your requirement. Install the required libraries and upload the code given above. 

  • Connect the power supply to the Arduino board.

Now your obstacle-avoiding car is ready to go. Enjoy!