Tutorial – Ultrasonic Sensor HC-SR04

Tutorial – Ultrasonic Sensor HC-SR04

Yes I know, this module seems like two eyes looking at you…it could also be scary, but it’s not! It’s just one of the most popular and useful (and also pretty) module that you really need!

In this Arduino Tutorial we will learn how the Ultrasonic Sensor HC-SR04 works and how to use it with your Arduino Board. Follow this tutorial and you will understand how to use it!

How it Works

Ultrasonic module

It’s made of two main parts. A Trasmitter and a Receiver. The Trasmitter emits ultrasounds and when those are reflected by an object. The Receiver gets the reflected ones.

Then is possible to calculate the distance between the module and the object. The equation is:

Distance = Speed x Time


The Trig pin is for the Trasmitter to emit the ultrasounds and the Echo pin is for the Receiver.

Ultrasonic module schematic

Where to Buy

I suggest the first option, you will have 5 modules and also some jumpers.


Let’s see a simple sketch!

const int pinTrig = 11;
const int pinEcho = 10;

const float speedSound = 0.034;               /*we use a const float because 
                                              0.034 is a decimal numbers*/
long lenght;
int distance;

void setup()
  pinMode(pinTrig, OUTPUT);
  pinMode(pinEcho, INPUT);

void loop()
  digitalWrite(pinTrig, LOW);
  delayMicroseconds(5);                       /*sets the pinTrig LOW for 5 Microseconds (then you will 
                                              understand why we use DelayMicroseconds)*/
  digitalWrite(pinTrig, HIGH);
  delayMicroseconds(10);                      //sets the pinTrig HIGH for 10 Microseconds
  digitalWrite(pinTrig, LOW);                 /*sets the pinTrig LOW because now we set 
                                              HIGH the pinEcho to obtain the distance*/
  lenght = pulseIn(pinEcho, HIGH);            /*the function pusleIn reads a pulse on a pin and it returns 
                                              the length of the pulse in microseconds. That's why we 
                                              used delayMicroseconds*/
  distance= lenght*speedSound/2;              /*The distance between the sensor and the object is only 
                                              the of the distance that the waves traveled.
                                              So we need to divide the result by 2.*/
  Serial.print("Distance: ");                 /*Prints the distance on the 
                                              Serial Monitor*/

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

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

en_GBEnglish (UK)
it_ITItaliano en_GBEnglish (UK)
%d bloggers like this: