HC-SR04 Ultrasonic Sensor with Arduino

Back to Blog

HC-SR04 Ultrasonic Sensor with Arduino

The HC-SR04 ultrasonic sensor is a distance measuring sensor which is used in many applications like obstacle avoidance robot, Car parking sensors, Sonar, Autonomous robots and many more. The distance measuring range is from 2cm to 4 meters. It is more precise than IR Sensors. Because its measuring capability does not get affected by light and also it can measures distance of about 4 meters.

Working principle:

The HC-SR04 ultrasonic sensor uses sonar to calculate the distance of an object like bats do (Not for Batman either). Similarly, the sensor sends a short sonic pulse (10 microseconds) and calculates the time taken to reach back the sensor. By multiplying this time with the speed of sound gives us the total distance. This value should be divided by 2 because the total time includes sending and receiving time.

The speed of sound:

The sound travels at a speed of 340 meters per second.

v = 340 m/second

Converting it to centimeters per microsecond, we get

v = 0.034cm/microsecond


v is speed. Which is also called as Velocity.


The formula for Time calculation:

Time (t) = distance(s)/speed (v)


Distance (s) = 20 (in cm)

Speed (v) = 0.034 (in microseconds)

t= s/v

= 20/0.034 = 588 microseconds


Distance calculation for Ultrasonic Sensor (Dividing the distance by 2):

s = t*0.034/2

s = 588*0.034/2


Actual Object distance = 9.99 cm

How to use an Ultrasonic sensor with Arduino:

The Arduino sends the signal to the trigger pin which is connected to pin 6 of Arduino. It receives back the signal from echo pin which is connected to pin 5 of Arduino. The power supply pins +5V and Ground are connected respectively.

Ultrasonic Sensor_bb

Buy HC-SR04 Ultrasonic Sensor

Code for HC-SR04 Ultrasonic Sensor:

/* Ultrasonic Sensor HC-SR04 and Arduino Tutorial */

/* www.factoryforward.com */

// defines pins numbers

const int trigPin = 6;// Arduino Pin 6

const int echoPin = 5;// Arduino Pin 5

long duration;

int distance;

void setup() {

    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

    pinMode(echoPin, INPUT); // Sets the echoPin as an Input

    Serial.begin(9600); // Starts the serial communication


void loop() {

    digitalWrite(trigPin, LOW); // Sets the trigPin as LOW initially


/* Set the trigPin HIGH for 10 micro seconds (Sonic pulse) */

    digitalWrite(trigPin, HIGH);

    delayMicroseconds(10);   //To keep HIGH for 10microseconds

    digitalWrite(trigPin, LOW);

/* Reads the echoPin, returns the sound wave travel time in microseconds */

    duration = pulseIn(echoPin, HIGH);

/* Calculating the distance */

    distance= duration*0.034/2;

/* Prints the distance on the Serial Monitor */

    Serial.print("Distance: ");



Open the serial monitor and make sure the baud rate is set to 9600. If you place an object in front of the ultrasonic sensor, the serial monitor will show the distance of the object in centimeters.

Ultrasonic sensor using NewPing Library:

The NewPing Library is written by Tim Eckel has simple methods to measure the distance. Few methods like sonar.ping_cm(); which returns the distance in cm. Similarly sonar.ping_in(); will return the distance by inches. To use these functions you need to include the NewPing.h library in the Arduino IDE. You can get the NewPing Library Here. After installing the library upload the given code to Arduino

Code for Ultrasonic sensor with NewPing Library:

#include <NewPing.h>

#define TRIGGER_PIN  6  // Arduino pin tied to trigger pin on the ultrasonic sensor.

#define ECHO_PIN     5  // Arduino pin tied to echo pin on the ultrasonic sensor.

#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.


void setup() {

    Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.


void loop() {

    delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

    Serial.print("Ping: ");

    Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)



In the serial monitor, you can see the distance of the object is ping for every 50 milliseconds.

Creating an instance for Ultrasonic sensors:



This method returns the distance in centimeters.


This method will return the distance in Inches.

Share this post

Leave a Reply

Back to Blog