Interface Ultrasonic sensor Hc-sr04 with Arduino

About Ultrasonic Sensor:

The ultrasonic sensor is used to measure the distance of an object. It consists of a transmitter and a receiver. the transmitter transmits an ultrasonic wave that is not audible to human ears. The transmitted wave then gets reflected back by the object and received by the receiver. After calculating the time between the received and transmitted wave we can easily calculate the distance of the object. As we already know the speed of sound and time taken by the wave we can now easily use the formula speed=distance/time and calculate the distance of the object. 

Pinout or Pin Diagram of Ultrasonic Sensor HC-sr04

Features of Ultrasonic Sensor HC-sr04HC

  • Operating Voltage: DC 5V
  • Current consumption: 15mA
  • Working Frequency: 40KHz
  • Maxmum Range: 400cm
  • Minmum Range: 2cm
  • Measuring Angle: 15 degree
  • Trigger Input Signal: 10µS TTL pulse

Components Needed for This Project

  • Arduino UNO
  • Ultrasonic sensor HC-sr04
  • Jumper wires

Pin Connection of Ultrasonic Sensor 

  • Pin VCC-5v
  • Pin GND-GND
  • Pin Trig- Pin 2
  • Pin Ech0-Pin 3

Circuit diagram of Ultrasonic Sensor with Arduino

Arduino code for Ultrasonic Sensor

const int tPin = 2;//trigger pin
const int ePin = 3;//echo pin

long d;
int dist;
void setup() {
  Serial.begin(9600);
pinMode(tPin, OUTPUT); 
pinMode(ePin, INPUT); 
 
}
void loop() {

digitalWrite(tPin, LOW);
delayMicroseconds(2);
digitalWrite(tPin, HIGH);
delayMicroseconds(10);
digitalWrite(tPin, LOW);
d= pulseIn(ePin, HIGH);
dist=d*0.034/2;
Serial.print("Distance of object: ");
Serial.println(dist);
}

Leave a Reply

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