Introduction: Arduino Ultrasonic Sensor Module Tutorial for Beginners (HC-SR04)

Hello There Fellow Electronic Enthusiasts and Hobbyists! We integrate different sensors with our Arduino Board for different applications; here is another Sensor Tutorial where we will integrate “Ultrasonic Sensor Module” or “HC-SR01 Module” on our Arduino Board!

So let’s get started!

Step 1: Watch the Video

Step 2: Gather the Requirements

Step 3: Understanding the Working of Ultra Sonic Module

So this HC-SR04 Module consists of 5 major parts namely

  1. Transmitter
  2. Receiver
  3. Microcontroller unit
  4. Transmitting Amplifier
  5. Receiving amplifier

We will use Speed Time and Distance relation Formula which is

Time = Distance /Speed

we will modify this formula according to our conditions and use case.

we have, Ultrasonic Sound wave of 40KHertz.

the speed for us here is Speed of Sound, which is 340 m/s

we need our calculations in cm, so we have Speed = 0.034 cm/us ( Cm per Micro Second)

now the distance becomes


Distance = 0.034 * Time / 2

(here we want time of travel from point A to B, but the wave is going from A to B and returns to A so the time is divided by 2)

so we will get the time from our Module via Echo pin from our Module.

Step 4: Connections

Connections Part is Very Simple

connect the

Vcc to 5V

Ground to Ground of Arduino

Trig to Digital Pin 9

Echo to Digital Pin 10

Step 5: Code

We will first Declare Constants and variables

<p>const int trigPin = 9;<br>const int echoPin = 10;</p><p>long duration;
int distance;</p>

Next, In Void setup, we will define Pin modes of Trigger and Echo pins and then initializes the Serial Com

<p>void setup()<br>   {
    pinMode(trigPin,OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin,INPUT); // Sets the echoPin as an Input
    Serial.begin(9600); // Starts the serial communication
   }</p>

Then, we will clear the Trigger Pin with a 2 microsecond Low pulse, and then we will trigger the Pin 9 using a 10 Microsecond high Pulse.

This when received on Micro-Controller unit of module, it transmits a train of 8 pulse cycles.

<p>void loop() <br>{
 
    digitalWrite(trigPin, LOW); // Clears the trigPin
    delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds
 
    digitalWrite(trigPin, HIGH); // write the Trigger for MCU on Module 
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds
 
    duration = pulseIn(echoPin, HIGH); // Using the Formula
    distance= duration*0.034/2;</p><p>Serial.print("Distance: ");</p><p>   Serial.println(distance);// Prints the distance on the Serial Monitor</p><p>}</p>

So now the data received on receiver will be stored in variable called as Duration, and finally we will Implement our Formula in a Variable called Distance and at the end of this Code, we will Display “ Distance “ in Serial Com port using ‘serial.println’ command.

And once the code is done, simply upload it to your Arduino board and we are all set to receive distance from our sensor module!

Step 6: Conclusion

So you can use this project and make your own electronic scale or something more complicated like some automation application!

Whatever you make! Do share it with me on "I made it " tab of this article