Creative Commons License
.

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 Arduino Ultrasonic Sensor Module or HC-SR01 Module on our Arduino Board.

So let’s get started!

Watch the Video

Gather the Requirements.

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 and also
  5. Receiving amplifier

however We will use Speed Time and Distance relation Formula which is:

Time = Distance /Speed

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

We have, Ultrasonic Sound wave of 40 K Hertz.

The speed for us here is Speed of Sound, and 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.

Connections Part is Very Simple(arduino ultrasonic sensor)

Vcc to 5V.

Ground to Ground of Arduino.

Trig to Digital Pin 9.

Echo to Digital Pin 10.

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!

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

[ult_createlink btn_link=”url:https%3A%2F%2Fminov.in%2F5-most-common-arduino-nano-clone-problems-and-their-solutions%2F|||”]

Write A Comment

Minov

A Market Place for your Innovations

0