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.