Overview

Before building the robot, it is important to understand how the ultrasonic sensor works because this sensor will play an important role in obstacle detection. The basic principle behind the operation of an ultrasonic sensor is to record the time it takes for the sensor to transmit ultrasound and receive ultrasound after touching a surface. Then the distance is calculated using the formula. In this project, the widely available HC-SR04 ultrasonic sensor is used. To use this sensor, a similar approach described above will be followed.

So the Trig pin of the HC-SR04 is pulled high for at least 10 minutes. An audio packet is transmitted with 8 pulses of 40 kHz each.

The signal then hits the surface and comes back and is picked up by the Echo pin of the HC-SR04 receiver. The Echo pin had already gone up at the time of sending.

Components Required

  1. Arduino NANO or Uno (any version)
  2. HC-SR04 Ultrasonic Sensor
  3. LM298N Motor Driver Module
  4. 5V DC Motors
  5. Battery
  6. Wheels
  7. Chassis
  8. Jumper Wires

Bill of Materials

S.N.COMPONENTS NAMEDESCRIPTIONQUANTITY
1Arduino  UnoArduino  Uno1https://www.evelta.com/launchxl-f28379d-c2000-delfino-mcus-f28379d-launchpad-development-kit/
 Robot Smart Car Chassis  Robot Smart Car Chassis  1https://www.amazon.in/Robotbanao-Smart-Chassis-Transparent-Racing/dp/B07WZFRJ8M/ref=sr_1_3?keywords=Robot+Chassis&qid=1645873505&sr=8-3
2HC-SR04 Ultrasonic SensorHC-SR04 Ultrasonic Sensor1https://www.amazon.in/ERH-India-Pc-Ultrasonic-Sensor/dp/B08TQMZ1J6/ref=sr_1_3?crid=3LUW3GUTLBV2U&keywords=HC-SR04+Ultrasonic+Sensor&qid=1645874049&sprefix=hc-sr04+ultrasonic+sensor%2Caps%2C281&sr=8-3
 3LM298N Motor Driver Module LM298N Motor Driver Module 1 https://www.amazon.in/Robocraze-L298-Motor-Driver-Module/dp/B072NCPM5R/ref=sr_1_2?crid=3UKNNUCLJ24YC&keywords=LM298N+Motor+Driver+Module&qid=1645874125&sprefix=lm298n+motor+driver+module%2Caps%2C316&sr=8-2
 4Battery Battery 1 https://www.amazon.in/INVENTO-Polymer-Rechargeable-Battery-cordless/dp/B08W9BLTZR/ref=sr_1_5?crid=1D7PDQK0ZBFHO&keywords=battery+5v&qid=1645874226&s=electronics&sprefix=battery+5v%2Celectronics%2C460&sr=1-5
5Connecting WiresJumper Wires20https://www.amazon.in/dp/B074JB6SX8/ref=twister_B074J9NMB9?_encoding=UTF8&psc=1
6BreadboardBreadboard1http://amazon.in/Electronicspices-Point-Solderless-Breadboard-Prototype/dp/B08YX57FJG/ref=asc_df_B08YX57FJG/?tag=googleshopdes-21&linkCode=df0&hvadid=396988870302&hvpos=&hvnetw=g&hvrand=5555375138870789824&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=9302486&hvtargid=pla-1364897184161&psc=1&ext_vrnc=hi

Circuit Diagram

Final Code


///// www.mevihub.com  ////////////////////////



int TrigPin = 9;      // trig pin of HC-SR04
int EchoPin = 10;     // Echo pin of HC-SR04

int Rev_Lef_4 = 4;       //REVerse motion of Left motor
int Fwd_Lef_5 = 5;       //ForWarD motion of Left motor
int Rev_Rig_6 = 6;      //REVerse motion of Right motor
int Fwd_Rig_7 = 7;      //ForWarD motion of Right motor

long duration, distance;

void setup() {
  
  
  Serial.begin(9600);
  pinMode(Rev_Lef_4, OUTPUT);      // set Motor pins as output
  pinMode(Fwd_Lef_5, OUTPUT);
  pinMode(Rev_Rig_6, OUTPUT);
  pinMode(Fwd_Rig_7, OUTPUT);
  
  pinMode(TrigPin, OUTPUT);         // set trig pin as output
  pinMode(EchoPin, INPUT);          //set echo pin as input to capture reflected waves
   digitalWrite(Fwd_Rig_7, HIGH);          
    digitalWrite(Rev_Rig_6, LOW);
    digitalWrite(Fwd_Lef_5, HIGH);                                
    digitalWrite(Rev_Lef_4, LOW); 
}

void loop() {

  digitalWrite(TrigPin, LOW);
  delayMicroseconds(2);   
  digitalWrite(TrigPin, HIGH);     // send waves for 10 us
  delayMicroseconds(10);
  duration = pulseIn(EchoPin, HIGH); // receive reflected waves
  distance = duration / 58.2;   // convert to distance
 Serial.println( distance);

  if (distance <30)
  {
    digitalWrite(Fwd_Rig_7, LOW);  //Stop                
    digitalWrite(Rev_Rig_6, LOW);
    digitalWrite(Fwd_Lef_5, LOW);                                
    digitalWrite(Rev_Lef_4, LOW);
    delay(600);
    digitalWrite(Fwd_Rig_7, LOW);      //movebackword         
    digitalWrite(Rev_Rig_6, HIGH);
    digitalWrite(Fwd_Lef_5, LOW);                                
    digitalWrite(Rev_Lef_4, HIGH);
    delay(500);
    digitalWrite(Fwd_Rig_7, LOW);  //Stop                
    digitalWrite(Rev_Rig_6, LOW);
    digitalWrite(Fwd_Lef_5, LOW);                                
    digitalWrite(Rev_Lef_4, LOW);  
    delay(600);  
    digitalWrite(Fwd_Rig_7, HIGH);       
    digitalWrite(Rev_Rig_6, LOW);   
    digitalWrite(Rev_Lef_4, LOW);                                 
    digitalWrite(Fwd_Lef_5, LOW);  
    delay(500);
    digitalWrite(Fwd_Rig_7, LOW);       
    digitalWrite(Rev_Rig_6, LOW);   
    digitalWrite(Rev_Lef_4, LOW);                                 
    digitalWrite(Fwd_Lef_5, HIGH);  
    delay(500);
    digitalWrite(Fwd_Rig_7, HIGH);          
    digitalWrite(Rev_Rig_6, LOW);
    digitalWrite(Fwd_Lef_5, HIGH);                                
    digitalWrite(Rev_Lef_4, LOW); 
    delay(100);
  }

}

Output Images

By Devilal

Leave a Reply

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