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

by MissionCritical in Circuits > Arduino

3882 Views, 9 Favorites, 0 Comments

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

inst_tmbnl.jpg
scale.gif

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!

Watch the Video

Gather the Requirements

vlcsnap-00023.jpg
IMG_20180601_172452_HDR.jpg
vlcsnap-00024.jpg
1-set-40pcs-Dupont-Wire-Jumper-Cable-2-54mm-1P-1P-Male-to-Female-for-Arduino.jpg

Understanding the Working of Ultra Sonic Module

vlcsnap-00010.jpg
vlcsnap-00001.jpg
vlcsnap-00002.jpg
vlcsnap-00003.jpg

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.

Connections

vlcsnap-00023.jpg
vlcsnap-00024.jpg
Capture.JPG
Untitled Sketch_bb.png

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

Code

Capture-1.JPG
Capture2.JPG
-2.JPG

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!

Conclusion

vlcsnap-00022.jpg
vlcsnap-00018.jpg
vlcsnap-00019.jpg
vlcsnap-00020.jpg
vlcsnap-00021.jpg
scale.gif

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