Ultrasonic Sensor HC-SR04 (Arduino Project)




Introduction: Ultrasonic Sensor HC-SR04 (Arduino Project)

About: All what I do I'm doing it for fun 🎩 🎮 My name is Chris, I'm an electronics engineer and a Youtuber who likes : Electrical projects 🔌 3D printing ⚙️ Electronic R&D stuff 🔎 We all know that robots are our …

In this project I will show you how to use two ultrasonic sensors connected to your Arduino board.

I’m providing a full step-by-step guidance to expalin how to use this kind of sensors, and I'm explaining in details both hardware and software parts in order to turn this guidance the simplest way that leads you to try your own skills in electronic making. This project is so handy to try even without hardware part if you are familiar with Proteus simulator and also there is enough documents and codes in this guide to allow you use your components in the right way. If you dislike to read so much! You can watch this video for more clear guidance.

We've made this project in a weekend so you can try it your own.

Before starting let’s see first

What you will learn from this tutorial :

  1. We will identify the Pins of our sensor.
  2. Then I will explain the operating principle (how it works).
  3. I will show you then how to connect two ultrasonic sensors to one Arduino board.
  4. For sure the code and tests are available too.

Step 1: What Is an Ultrasonic Sensor!

Here is a

description of our sensor it has 4 pins I’m talking about the HC-SR04 sensor

We have the VCC and GND pins for power supply then the trig and echo pins for signal information and I will explain the use of these pins, also the sensor has an ultrasonic transmitter and receiver with one oscillator to generate a 4 Khz Ultrasonic sound that allows you to measure distances up to 2 meters.

Ultrasonic transducers are divided into three broad categories:

  • Transmitters.
  • receivers.
  • transceivers.

Transmitters convert electrical signals into ultrasound, receivers convert ultrasound into electrical signals, and transceivers can both transmit and receive ultrasound. In our case we are using HC-SR04 which is a transceiver ultrasonic sensor.

In a similar way to radar and sonar, ultrasonic transducers are used in systems which evaluate targets by interpreting the reflected signals. For example, by measuring the time between sending a signal and receiving an echo the distance of an object can be calculated. Passive ultrasonic sensors are basically microphones that detect ultrasonic noise that is present under certain conditions.

Step 2: How It Works !

So first of we have to understand how the ultrasonic sensor operates, well it’s so simple based on the transmission of an ultrasonic wave and detect its reflection through an object to identify the distance between the sensor and the objects, so we need to generate an ultrasonic wave by creating a 10us pulse and measure the duration taken by that pulse to reach the object then bounce back, and to identify the distance we should follow this algorithm.

Do not forget that the measured duration is proportional to the double of the desired distance this is why we should divide that duration by two then divide it again by 29.1 which is the speed of our wave.

Step 3: Ingredients

Now we will identify the components that we’ll use in this tutorial.

So we need:

Step 4: The Circuit

Now let’s take a closer look at our sensor, we have four pins here, the first one is the Ground pin then we have the echo pin, we will read the reflection signal from it and the trig pin from where we’ll transmit the ultrasonic wave and the last pin is Vcc where we should provide 5v to turn on the sensor.

Now let’s prepare the circuit showed in the photo above, I used fritzing to make it clear for you guys, do not forget that we will use the digital pins only so just place your connectors wherever you want in your Arduino board.

If you don't have the necessary components and you want to try this project, you can use Proteus simulator as showed in the video down bellow.

Step 5: The Arduino Code

Now let’s move to the source code, so run the Arduino IDE, as usual you can download the appropriate code from thisLink, as you can see in the photos the code is so short and well commented actually no much details in it, we will measure the distance between the sensors and the object then display the measured distance on the serial monitor then make a test on that distance, if it is less than 10 cm we well turn on the LED or turn it off in the opposite case.

About measuring the distance I create this function that takes to parameters which are the trig pin and the echo pin then we generate the pulse through the trig pin and measure the Propagation time of that pulse through the echo pin using the pulseIn API

Now everything is ready, just upload the code and start the test by moving these two objects and as you can see here in the serial monitor the distance changes depending on the object position and when we exceed 10 cm the LED turn on.

Thank you :)

I believe this Instructable is the most comprehensive step-by-step guide to understand how to use ultrasonic sensors. That is it for this instructable, feel free to thumb it and to ask us anything you want to know about this project through a comment.

Be the First to Share


    • Stone Concrete Cement Contest

      Stone Concrete Cement Contest
    • Digital Fabrication Student Design Challenge

      Digital Fabrication Student Design Challenge
    • Make It Modular: Student Design Challenge

      Make It Modular: Student Design Challenge



    2 years ago

    Hi, I have a question. Can I use one HC-SR04 as a transmitter and receive the signal from another HC-SR04?


    3 years ago on Step 5

    Hi, I'm wondering if you can help me with the schematics and codes for my school project. I have 2x ultrasonic sensors and 2 neopixel ledstrips this together are connected to one extern power source. The lights from the led strip change colors depending on the distance. How closer to the sensor, the color will be red. How further from the sensor, less light or other color for instance green will turn on. Do you know how ever must be connected and do you know the codes? I have an arduino uno/pro mini


    4 years ago

    The link (above) to the code doesn't work. Can you repost or make available somewhere else...?

    Thank you,



    Question 4 years ago

    what would the wires look like if you were to use an elegoo original sized arduino uno board?


    4 years ago

    Question, awesome design, I have most of the parts, but can I do this with an 5110 video screen? I don't have 16x2 handy. If so can you let me know the pinouts and the code if I have to change it?. I have a small container I want to use for this and a Nano, but need a screen, and i have 2 already, but would love to use if possible. Thanx.

    DIY GUY Chris
    DIY GUY Chris

    Reply 4 years ago

    Hello LoganB22

    Yes you can use that display screen but you need to configure its pins in your code using the appropriate library which is available in Arduino library manager. just follow the library examples to understand how it works and change the instruction lines in the code that I've provided to send data to you screen instead of sending it to LCD display.

    Let us know if we can further assist you.


    4 years ago

    Hi, not sure but the equation is bit strange, since when we have Distance= Time/Speed(Velocity)? Velocity always was dx/dt or in average V=Distance/Time thus Distance= Velocity/Time e.g. m/s divided to s and in the end we got meters... please check the equation on the slide.

    DIY GUY Chris
    DIY GUY Chris

    Reply 4 years ago

    You've just write it wrong !
    So in this case Distance= Velocity*Time