Complete Guide for Ultrasonic Sensors HC-SR04 with Arduino

Complete Guide for Ultrasonic Sensors HC-SR04 with Arduino

The Ultrasonic Sensors HC-SR04 with Arduino

In this guide, we are talking about the usage of the Ultrasonic Sensors HC-SR04 with Arduino. You can learn how it functions, and see its features and we provide Ultrasonic Sensors with Arduino project examples for you to get some help in your own projects. We are showing you graphical diagrams on wiring the ultrasonic sensors and an example diagram with Arduino.


The HC-SR04 ultrasonic sensor has radars to check any object’s distance. The sensor records from 2cm to 400cm  (0.8inch to 157inch) with correct 0.3cm (0.1inches).  This is very suitable for the enthusiastic projects you are doing. Additionally, this specific model comes to you with ultrasonic transmitter and receiver modules.

The following picture is the HC-SR04 ultrasonic sensor.

Ultrasonic Sensor

Below is the other side of the sensor.

Ultrasonic Sensor


There is a list of HC-SR04 ultrasonic sensor attributes and specs. For more details, you have to check the datasheet of the sensor.

  • Power Supply:+5V DC
  • Inactive Current : <2mA
  • Active Current: 15mA
  • Productive Angle: <15°
  • Ranging Distance : 2cm – 400 cm/1″ – 13ft
  • Resolution : 0.3 cm
  • Measuring Angle: 30 degree
  • Trigger Input Pulse width: 10uS TTL pulse
  • Echo Output Signal: TTL pulse corresponded to the distance range
  • Dimension: 45mm x 20mm x 15mm

How Does it perform?

The Sonars are used by the ultrasonic sensors to decide the distance to the objects. Here is the process going on.

The ultrasound transmitter the trig pin releases a high-frequency sound (40 kHz).

The sounds walk through the air. If it meets any object, it returns back to the module.

The ultrasound receiver the echo pin gets the returned sound which means the echo.

Ultrasonic Sensor

The time during the transmitting and receiving of signal lets us count the distance to the object. This can be done because we know the sound’s speed in the air. This is the formula.

distance to an object = ((speed of sound in the air)*time)/2

Speed of sound in the air at 20ºC (68ºF) = 343m/s

HC-SR04 Ultrasonic Sensor Pinout

Ultrasonic Sensor

The below table shows the pinout of the HC-SR04 Ultrasonic Sensor.

Ultrasonic Sensor

Where to buy it?

Find the best price for the Ultrasonic Sensor HC-SR04 sensor on Maker Advisor.

Arduino with HC-SR04 Sensor

HC-SR04 Sensor

This sensor is mostly used by Arduino tinkerers. So here we are showing you how to use the HC-SR04 ultrasonic sensor with Arduino. In this project, the ultrasonic sensor recorded the distance to the object in the serial monitor.

The aim of this project is to support the understanding of the sensor’s functioning. Then after you will be able to use the example in the projects you are handling.

Parts Needed

Here are the parts that you need to get for the following tutorial.

  • Arduino UNO – read Best Arduino Starter Kits
  • Ultrasonic Sensor (HC-SR04)
  • Breadboard
  • Jumper wires

Follow the earlier links to get the necessary parts for the projects for a good price.

Arduino with HC-SR04 Sensor Wiring

Follow the below pictures to link the HC- SR04 ultrasonic sensor to the Arduino.

HC-SR04 Sensor

The below table brings the connections that you have to make.

HC-SR04 Sensor


How the Code Works

First, make the variable for the trigger and echo pins names as trigPin and echoPin correctly. The trigger pin is linked to digital pin 11 and the echo pin to Pin 12.

int trigPin = 11;

int echoPin = 12;

You have to make three variables in this type: duration and inches. The duration variable is stored between the release and the reception of the signal. The cm variable keeps the distance in centimeters and the inches variable keeps the distance in inches.

long duration, cm, inches;

In the setup() format the serial port at a bandwidth rate of 9600 and set the trigger as Output and echo pin as Input.

//Serial Port begin

Serial.begin (9600);

//Define inputs and outputs

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

In the loop() activate the sensor by sending a high pulse of 10 microseconds, before that provide a short LOW pulse to make sure that you get a clean HIGH pulse.

digitalWrite(trigPin, LOW);


digitalWrite(trigPin, HIGH);


digitalWrite(trigPin, LOW);

We are using the pulseIn() method to get the sound wave travel time.

duration = pulseIn(echoPin, HIGH);

Next, we count the distance to any object considering the speed of the sound.

distance = (traveltime/2) x speed of sound

The speed of sound is: 343m/s = 0.0343 cm/uS = 1/29.1 cm/uS

Or in inches: 13503.9in/s = 0.0135in/uS = 1/74in/uS

The travel time should be divided by 2 because we need to think that the wave is gone, hit the object, and came back to the sensor.

cm = (duration/2) / 29.1;

inches = (duration/2) / 74;

At last, the results will be printed on the Serial monitor.


Serial.print(“in, “);




Source code with NewPing Library

You can use the NewPing library. Download it from here.

After you have uploaded the NewPing Library, upload the code given below.

How the Code Works

It is very easy to get the distance to an object with the NewPing library. You have to start by entering the NwePing library.

#include <NewPing.h>

Next, define the trigger and echo pin. The trigger pin is linked to the Arduino digital pin 11 and the echo to Pin 12. And also you have to define the MAX_DISTANCE variable to use the library.

#define TRIGGER_PIN 11

#define ECHO_PIN 12

#define MAX_DISTANCE 200

Then, create the NewPing sample named Sonar.


In the setup() format the Serial communication is at a rate of 9600.


At last, in the loop(), use the ping_cm() formula on the sonar object to have the distance in centimeters.

unsigned int distance = sonar.ping_cm();

If you need the distance in inches, use sonar.ping_in()


Upload the code to the Arduino board. Next, open the serial monitor at a rate of 115200.

HC-SR04 Sensor

The distance for the observation is given in the Serial monitor window.

HC-SR04 Sensor

Wrapping Up

In this article, we have brought you how the HC-SR04 ultrasonic sensor functions and how to use it on the Arduino board.

Anyhow, if you are new to Arduino, it is good to follow the Arduino course that supports you to start with the board.

Frequently asked questions:

What is the usage of the ultrasonic sensor?

Very often, proximity sensors are linked with ultrasonic sensors. They are in anti-collision safety systems and self-parking automotive technologies. Robotic obstacle detection systems and manufacturing technology both are using ultrasonic sensors.

How to choose a good ultrasonic sensor?

The effective beam angle — the angle around the acoustic axis where a target would be detected—must be considered. The effective beam angle will rise as the target gets closer to the sensor or if a target with a higher TS is employed.

Does the ultrasonic sensor detect water?

Tank level measurement is only one of the uses for ultrasonic depth sensors. Anyhow, it is not only for water detection. It is used fuel gauge for diesel, regulating irrigation


Arduino with BH1750 Ambient Light Sensor

Things to know about BMP388 Altimeter with Arduino

Leave a Reply

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