Ultrasonic Sensor HC-sr04 :

Ultrasonic sensor is use to calculate the distance. It use ultrasonic waves to measure the distance. The ultrasonic wave can hit the object and and come back, ultrasonic sensor can use time to calculate the distance. HC-04 consists of two ultrasonic transducers. One can transmit ultrasonic waves and the other acts as receiver.

Components :

  • Arduino Uno
  • Bread board
  • Jumper Wires
  • Ultrasonic sensor HC-sr04

Ultrasonic Sensor Pinout :

  • Vcc –> 5V
  • Gnd –> Gnd
  • Trigger –> 2
  • Echo –> 3

Circuit Diagram :

Code :

#define trigPin 2
#define echoPin 3
long dur;
int dist = 0;

void setup() {
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,OUTPUT);
  Serial.begin(9600);

}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  dur = pulseIn(echoPin, HIGH);
  dist = dur * 0.034 / 2;
  Serial.print("Distance : ");
  Serial.print(dist);
  Serial.println("cm");

}

1 Comment

Leave a Reply

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

Facebook
YouTube
× Contact us