SR04 Ultrasonic Distance Sensor is a popular and low cost solution for non-contact distance measurement function. This module includes ultrasonic transmitter, ultrasonic receiver and its control circuit.
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2 cm to 400 cm or 1” to 13 feet. It comes complete with ultrasonic transmitter and receiver module.
||Trigger pin D9
||Echo Pin (output) D12
||Ground with Circuit
1. Quiescent Current: <2mA.
2. Working Current: 15mA.
3. Effectual Angle: <15°.
4. Ranging Distance: 2cm – 400 cm/1″ – 13ft.
5. Resolution: 0.3 cm
6. Measuring Angle: 30 degree.
7. Trigger Input Pulse width: 10uS.
8. Dimensions: 45mm x 20mm x 15mm.
9. Power Supply:+5V DC.
The ultrasonic sensor works on the principle of SONAR and RADAR system which is used to determine the distance to an object. An ultrasonic sensor generates the high-frequency sound (ultrasound) waves. When this ultrasound hits the object, it reflects as echo which is sensed by the receiver
Provide TRIGGER signal, atleast 10μS High Level (5V) pulse.
The module will automatically transmit eight 40KHz ultrasonic burst.
If there is an obstacle in-front of the module, it will reflect the ultrasonic burst.
If the signal is back, ECHO output of the sensor will be in HIGH state (5V) for a duration of time taken for sending and receiving ultrasonic burst. Pulse width ranges from about 150μS to 25mS and if no obstacle is detected, the echo pulse width will be about 38ms.
HC-SR04 WITH ARDUINO:
In this project the ultrasonic sensor reads and writes the distance in the serial monitor.
Upload the following code to your Arduino IDE.
* Code for Ultrasonic Sensor HC-SR04
#define trigPin 3
#define echoPin 2
#define led 5
#define led2 6
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
// delayMicroseconds(1000); – Removed this line
delayMicroseconds(10); // Added this line
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4)
// This is where the LED On/Off happens digitalWrite(led,HIGH);
// When the Red condition is met, the Green LED should turn off digitalWrite(led2,LOW);
if (distance >= 200 || distance <= 0)
Serial.println(“Out of range”);
For Arduino IDE Software: Download Here.