Obstacle Avoiding Robot (using L293D)


Description:

This Tutorial is for anyone getting started with arduino. We will be building an obstacle avoiding robot that uses an ultrasonic HC SR-04 sensor to detect objects and L293D Motor Driver.

About L293D :

Dual H-Bridge Motor Driver for DC or Steppers - 600mA - L293D

This is a very useful chip. It can actually control one motor independently. We will use in its entirety the chip in this tutorial. Pins on the right hand side of the chip are for controlling a one motor. Pins on the left hand side of the chip are for controlling second motor.

You can run four solenoids, two DC motors or one bi-polar or uni-polar stepper with up to 600mA per channel using the L293D.

Hardware Required:

  • Arduino Uno R3
  • Dual H-Bridge L293D Motor Driver
  • HC-SR04 Ultrasonic Module Distance Sensor
  • Bracket / Holder for HC-SR04
  • DC Motors and Wheels
  • Bovine Wheel
  • Plate or Chassis
  • Jumper or Wires
  • Batteries and Battery Holdes
  • Glue Gun or Cable Ties
  • Breadboard
Get to Code

const int trig = 12;
const int echo = 11;
const int leftForward = 2;
const int leftBackward = 3;
const int rightForward = 4;
const int rightBackward = 5;

int duration = 0;
int distance = 0;

void setup() 
{
  pinMode(trig , OUTPUT);
  pinMode(echo , INPUT);
  pinMode(leftForward , OUTPUT);
  pinMode(leftBackward , OUTPUT);
  pinMode(rightForward , OUTPUT);
  pinMode(rightBackward , OUTPUT);
  
  Serial.begin(9600);

}

void loop()
{
  digitalWrite(trig , HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig , LOW);


  duration = pulseIn(echo , HIGH);
  distance = (duration/2) / 28.5 ;
  Serial.println(distance);
  

  if ( distance < 20 )
  {
    digitalWrite(leftForward , LOW);
    digitalWrite(leftBackward , HIGH);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
    delay(100);
  }
  else
  {
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
  }
 
}