December 31, 2019

Ultrasonic Sensor Interface With Arduino

In this project, we will introduce you to the HC-SR04 Ultrasonic sensor. It works by sending sound waves from the transmitter, which then bounce off of an object and then return to the receiver. You can determine how far away something is by the time it takes for the sound waves to get back to the sensor. Let's get right to it!


This project is about the interfacing of an Ultrasonic Sensor module with Arduino UNO. This Project could measure the distance from a 2-400cm distance by using sound wave at 40000hz.


Components Required:
1.     Arduino Uno
2.     HC-SR04 Ultrasonic module
3.     Data Cable
4.     Jumper wires

How Ultrasonic sensor works?
First, the transmitter sends an ultrasonic wave that reflects from an abject nearby. When the sound waves hit the receiver, it turns the Echo pin high for however long the waves were traveling for. To get that, we can use a handy Arduino function called pulseIn(). It takes 2 arguments, the pin you are listening to(In our case, the Echo pin), and a state(HIGH or LOW). What the function does is waits for the pin to go whichever state you put in, starts timing, and then stops timing when it switches to the other state. In our case, we would put HIGH since we want to start timing when the Echo pin goes high. We will store the time in the duration variable. (It returns the time in microseconds)

Now that we have the time, we can use the equation speed = distance/time, but we will make it time x speed = distance because we have the speed. What speed do we have? The speed of sound, of course! The speed of sound is approximately 340 meters per second, but since the pulseIn() function returns the time in microseconds, we will need to have a speed in microseconds also, which is easy to get. A quick Google search for "speed of sound in centimeters per microsecond" will say that it is .0343 c/μS. You could do the math, but searching for it is easier. Anyway, with that information, we can calculate the distance! Just multiply the duration by .0343 and then divide it by 2 (Because the sound waves travel to the object AND back). We will store that in the distance variable.

How To Use?
In order to generate the ultrasound, you need to set the Trig on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward. So in order to get the distance in cm, we need to multiply the received travel time value from the echo pin with the velocity of sound and divide it by 2.

     distance = (velocity of sound x time)/2

     velocity of sound = 0343 cm/μS;
     
Pinout:

  • VCC supplies power for the module. You can directly connect it to the 5V pin on the Arduino.
  • GND is the Ground Pin and needs to be connected to the GND pin on the Arduino.
  • Trig 
  • Echo 
HC-SR04-sensor-library
coming soon

Functions under this library
coming soon

Video Example:
coming soon

SOFTWARE:
coming soon