Interfacing Ultrasonic Ranging Module HC-SR04 With Arduino

Introduction: Interfacing Ultrasonic Ranging Module HC-SR04 With Arduino

Hey, what's up, Guys! Akarsh here from CETech.

This project of mine is a bit on the simpler side but as fun as the other projects. In this project, we are going to interface an HC-SR04 Ultrasonic distance sensor module. This module works by generating ultrasonic sound waves that are out of the audible range of human beings and from the delay between the transmission and reception of the generated wave the distance is calculated.

Here we are going to interface this sensor with Arduino and will try to mimic a parking assistant system that according to the distance from the obstacle behind generates different sounds and also lights up different LEDs according to the distance.

So let's get to the fun part now.

Step 1: Get PCBs for Your Projects Manufactured

You must check out PCBWAY for ordering PCBs online for cheap!

You get 10 good quality PCBs manufactured and shipped to your doorstep for cheap. You will also get a discount on shipping on your first order. Upload your Gerber files onto PCBWAY to get them manufactured with good quality and quick turnaround time. Check out their online Gerber viewer function. With reward points, you can get free stuff from their gift shop.

Step 2: About HC-SR04 Ultrasonic Ranging Module

The ultrasonic sensor (or transducer) works on the same principles as a radar system. An ultrasonic sensor can convert electrical energy into acoustic waves and vice versa. The acoustic wave signal is an ultrasonic wave traveling at a frequency above 18kHz. The famous HC SR04 ultrasonic sensor generates ultrasonic waves at 40kHz frequency. This module has 4 pins which are Echo, Trigger, Vcc, and GND

Typically, a microcontroller is used for communication with an ultrasonic sensor. To begin measuring the distance, the microcontroller sends a trigger signal to the ultrasonic sensor. The duty cycle of this trigger signal is 10µS for the HC-SR04 ultrasonic sensor. When triggered, the ultrasonic sensor generates eight acoustic (ultrasonic) wave bursts and initiates a time counter. As soon as the reflected (echo) signal is received, the timer stops. The output of the ultrasonic sensor is a high pulse with the same duration as the time difference between transmitted ultrasonic bursts and the received echo signal.

The microcontroller interprets the time signal into distance using the following function:

Distance(cm) = Echo Pulse Width(microseconds)/58

Theoretically, the distance can be calculated using the TRD (time/rate/distance) measurement formula. Since the calculated distance is the distance traveled from the ultrasonic transducer to the object—and back to the transducer—it is a two-way trip. By dividing this distance by 2, you can determine the actual distance from the transducer to the object. Ultrasonic waves travel at the speed of sound (343 m/s at 20°C). The distance between the object and the sensor is half of the distance traveled by the sound wave and it can be calculated using the below function:

Distance(cm) = (time taken x speed of sound)/2

Step 3: Doing the Connections

For this step, Materials Required are - Arduino UNO, HC-SR04 Ultrasonic Distance sensor module, LEDs, Piezo Buzzer, Jumper cables

The Connections are to be done in the following steps:

1) Connect the Echo Pin of the Sensor to GPIO Pin 11 of the Arduino, Trigger Pin of the Sensor to the Sensor to GPIO Pin 12 of the Arduino UNO and Vcc and GND Pins of the Sensor to the 5V and GND of the Arduino.

2) Take 3 LEDs and connect the cathodes(generally the longer leg) of the LEDs to Arduino's GPIO pins 9,8 and 7 respectively. Connect the anode(generally the shorter leg) of these LEDs to the GND.

3) Take the piezo buzzer. Connect its positive pin to the GPIO pin 10 of the Arduino and the negative pin to GND.

And in this way, the connections of the project are done. Now connect the Arduino to your PC and move on to the next steps.

Step 4: Coding the Arduino UNO Module

In this step, we are going to upload the code in our Arduino UNO to measure the distance of any nearby obstacle and according to that distance sound the buzzer and light up the LEDs. We can also see the distance readings on the Serial Monitor. Steps to be followed are:

1) Move to the GitHub repository of the project from here.

2) On the Github repository, You will see a file named "sketch_sep03a.ino". This is the code for the project. Open that file and copy the code written in it.

3) Open the Arduino IDE and select the correct board and COM port.

4) Paste the code in your Arduino IDE and upload it to the Arduino UNO board.

And in this way, the coding part for this project is also done.

Step 5: Time to Play !!

As soon as the code gets uploaded you can open the Serial monitor to see the distance readings from the Ultrasonic Sensor module the readings keeps on updating after a fixed interval. You can put some obstacle in front of the Ultrasonic module and observe the change in the reading shown there. Apart from the readings shown on the Serial monitor, the LEDs and buzzer connected to the buzzer will also indicate an obstacle in different ranges as follows:

1) If the distance of the nearest obstacle is more than 50 cm. All the LEDs would be in OFF state and the buzzer will also not ring.

2) If the distance of the nearest obstacle is less than or equal to 50 cm but greater than 25 cm. Then the first LED will light up and the buzzer will create a beep sound with a delay of 250 ms.

3) If the distance of the nearest obstacle is less than or equal to 25 cm but greater than 10 cm. Then the first and Second LED will light up and the buzzer will create a beep sound with a delay of 50 ms.

4) And if the distance of the nearest obstacle is less than 10 cm. Then all the three LEDs will light up and the buzzer will make a continuous sound.

In this way, this project will sense the distance and give different indications according to the distance range.

Hope you liked the tutorial.

Robots Contest

Participated in the
Robots Contest

Be the First to Share

    Recommendations

    • Stone, Concrete, Cement Challenge

      Stone, Concrete, Cement Challenge
    • Backyard Contest

      Backyard Contest
    • Summer Fun: Student Design Challenge

      Summer Fun: Student Design Challenge

    Comments