Ultrasonic HC-SR04


The Ultrasonic Sensor HC-SR04 [Product Link] is one of the most commonly used distance measuring ultrasonic sensors and works extremely well with Arduino.

Technical Specs

Ultrasonic Sensor HC-SR04
Ultrasonic Sensor HC-SR04
  • Operating Voltage: 5V
  • Static current: 2mA max
  • Induction Angle: 15°
  • Detection Range: 2 – 200cm
  • High precision up to 3mm



This module has 4 pins- Vcc (5V), Trig, Echo, GND. Trig (trigger) is used to send out an ultrasonic high level pulse for at least 10μs and the Echo pin then automatically detects the returning pulse.

Measuring Distance

The time it takes the sound wave to be sent, hit the object and return back to the sensor is measured. This time is then multiplied by the speed of sound (343m/sec = 0.0343cm/μs = [1/29.1] cm/μs approx.) to give the total distance traveled by the ultrasonic wave, which is then divided by 2 (to account for the fact that the wave was sent, hit the object, and then returned back to the sensor, hence covering twice the distance to the object)
Distance = (Time for wave to return * Speed of sound) / 2.

Arduino Usage

This sensor module uses any 2 digital pins on the Arduino for Trig and Echo, and does not necessarily have to be pins 9 and 10. The code for measuring distance with Chineduino Uno Rev3 [Product Link] is given as:

// Code by Jasmeet Singh for http://Wiki.JMoon.co
#define trigPin 10 // Connect Trig to Uno digital pin 10
#define echoPin 9 // Connect Echo to Uno digital pin 9
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);}
void loop() {
long time, dist;
digitalWrite(10, LOW); // Trig
digitalWrite(10, HIGH); // Trig
digitalWrite(10, LOW); // Trig
time = pulseIn(9, HIGH); // Echo
dist = (time/2) / 29.1; // divide by 29.1 or multiply by 0.0343

Open up your serial monitor after uploading the code and test out the limits of the sensor. It can detect up to 200cm(2m), but sometimes maybe able to detect even further. Then use these limits to print out the distance values only when the value is within these limits. One way to do this is to replace the lines



if (dist<=0||dist>=200){
Serial.println("-"); }
else {
Serial.println("cm"); }

Possible Application Areas

This sensor can be used in any robot that needs to know the distances to objects in front of it. This sensor can be used with Chineduino Uno Rev3 [Product Link], Magician Robot Controller Board [Product Link], Mini Driver Robot Controller [Product Link] or Spider Robot Controller [Product Link]. This sensor can also be mounted on a Pan Tilt Kit [Product Link] to detect objects in a wider area. A few example uses are:
1. Object Detecting Robot [Link]
2. Line Follower with Object Detecting Robot [Link]

Did you create a project using this product? Leave details/links below!


So empty here ... leave a comment!

Leave a Reply

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