How ultrasonic sensor works?
As mentioned in the name ultrasonic sensor, it works on ultrasounds. First of all, we have to know about ultrasounds. These are the high-pitched sound waves with higher frequencies than the frequencies that can be easily audible by human ears. Human ears can hear the waves of vibration about 20 times a second to about 20,000 times a second. However ultrasounds has frequency of 20,000Hz which is inaudible by human ear.
HC-04 Hardware overview
The HC-SR04 Ultrasonic sensor consists of two ultrasonic wholes. One of the whole is the transmitter which convert electrical signal into 40 KHz ultrasonic sound pulses. The receiver catches the transmitted pulses. If the receiver receives that signals it produces output pulse of width that can be used to find or determined distance travelled by the pulse.
As the sensor is small so we can use it in any project because of its size. Its range is between 2cm to 4oo cm. This sensor works of 5v so we can connect it directly to the Arduino 5v pin.
The specification of ultrasonic senor is:
Operating voltage | 5V |
Operating Frequency | 40Hz |
Max Range | 4cm |
Min Range | 2cm |
Ranging Accuracy | 3mm |
Mearing angle | 15degree |
Trigger input signal | 10 |
HC-04 Pinouts:
Vcc: vcc pin is used to power on the senor. We can connect to 5v pin on Arduino.
GND: This pin is ground and will connect to the gnd pin on Arduino.
Trig: Trig pin is used to trig or transmit the signal.
Echo: Echo pin produces pulse when the transmitted signal receives. The length of the signal is directly proportional to the time taken by the transmitted signal to reach.
How ultrasonic sensor works?
When a pulse of at least 10 microsecond in duration is applied to the Trigger pin then it starts. Sensor transmits a burst of eight pulses at 4oKHz.This burst makes the ultrasonic waves unique from the ambient ultrasonic noise.
The pulses travels via air away from the sensor. The echo ping again goes high to start emitting the beginning of the echo back signal.
If the transmitted signals or pulses not go back then Echo system will timeout after 38 milliseconds.
If those pulses are reflect back then the Echo goes low as soon as the pulses received and the time depends upon the time required by the pulses to reach the sensor. The width of the reflected pulse is used to calculate the distance between sensor and the object.
Distance=speed/time
Time=distance/speed
Speed=distance/time
Let’s take an example. Let we have an object in front of our sensor by some distance. We have to calculate distance. We received a pulse of 500 microseconds. Now we use formula to calculate distance.
Distance=speed/time
Here speed of sound is of course 340 m/s. Here we have to convert speed in cm/microseconds which is 0.034 cm/µs.
Distance=o.o34/500
So here is point to be notice that the time is that pulse took to transmit and again received by the sensor. So we have to divide it by two to the total distance.
Distance= (o.o34/500)/2
Distance=8.5 cm
So object is 8.5 cm away from the sensor.
Wiring – Connecting HC-SR04 to Arduino Uno
Connecting HC-04 with Arduino is very easy. We already take a look on our sensor that it has four pins. The circuit diagram is as follows:
Coding section
void setup()
{
Serial.begin(9600);
}
void loop()
{
// Send ping, want to get distantce in cm
distance = sonar.ping_cm();
// display result on serial monitor
Serial.print("Distance = ");
if (distance >= 400 || distance <= 2)
{
Serial.println("Out of range");
}
else
{
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
First of all we have to define pin numbers that we are using.
Then in void setup initialize trig pin as output and echo pin as input using pinMode() command. Then initialize baud rate as Serial.begin(9600).
Now in void loop, we have to do low trig pin to transmit pulses that we already discussed before. We use variable named pingtraveltime to store the time taken by the pulse to go back to the sensor. Now use formula of distance to find distance.
Distance= Distance=speed/ pingtraveltime
What are the limitation of using HC-04
- In some experiments we found that we found that when there is soft or irregular body in front of the sensor it will sometimes no reflect pulses instead it absorb pulses.
- When the object is too small it is probability to not reflect back of pulses.
- The sensor must be in front of the object.