HC-SR04 as known as Ultrasonic sensor is non-contact range detection module used for distance measurement, radar & object displacement etc. It offers high accuracy, steady readings & easy interface pin-out to interface with various microcontroller boards as ultrasonic transceiver. Interfacing HC-SR04 with your Arduino and enhance object detection feature in your DIY project.
Features & Specifications
- Power Supply :+5V DC
- Quiescent Current : <2mA
- Working Current: 15mA
- Effectual Angle: <15°
- Ranging Distance : 2 cm – 400 cm / 1″ 13ft
- Resolution : 0.3 cm
- Measuring Angle: 30 degree
- Trigger Input Pulse width: 10uS
- Dimension: 45mm x 20mm x 15mm
- Weight: approx. 10 g
- Vcc: +5V DC to power ON the module.
- Trig: Trigger pin is an Input pin. This pin has to be kept high for 10µs to initialize measurement by sending ultrasonic wave.
- Echo: Echo pin is an Output pin. This pin goes high for a period of time which will be equal to the time taken for the ultrasonic wave to return back to the sensor.
- GND: Ground of the system.
As shown above the HC-SR04 Ultrasonic sensor there are 4 pins: Vcc, Trigger, Echo and Ground respectively. This sensor is most popular where measuring distance or sensing objects are required. The module has two components in the front which works as Ultrasonic transmitter and Receiver. The sensor works with the simple formula “Distance = Speed × Time”. The Ultrasonic transmitter transmits an ultrasonic wave, this wave travels in air and when it gets objected by any material it gets reflected back toward the sensor this reflected wave is observed by the Ultrasonic receiver module.
Connection with Arduino
Arduino ⇿ HC-SR04
Vcc ⇿ Vcc
D09 ⇿ Trig
D10 ⇿ Echo
Gnd ⇿ Gnd
const int trigPin = 9;
const int echoPin = 10;
pinMode(trigPin, OUTPUT); // Sets the Trig Pin as an Output
pinMode(echoPin, INPUT); // Sets the Echo Pin as an Input
Serial.begin(9600); // Starts the serial communication
digitalWrite(trigPin, LOW); // Clears the Trig Pin
digitalWrite(trigPin, HIGH); // Sets the Trig Pin on HIGH state for 10µs
duration = pulseIn(echoPin, HIGH); // Echo Pin returns sound wave travel time in µs
distanceCm= duration*0.034/2; // Distance calculation for CM
distanceInch = duration*0.0133/2; // Distance calculation for inch
Serial.print("Distance: "); // Prints the distance on the Serial Monitor
Serial.print(" cm, ");
Open the code in Arduino IDE, upload it & get distance values on serial monitor screen. I hope this tutorial will be helpful for HC-SR04 interface in your Arduino projects. Further in this you can interface LCD to display distance & interface HC-05 bluetooth module to send distance values via bluetooth. Your feedback will be appreciated.