Posted on December 5, 2018 by electromaniaweb
HC-SR04 is an ultrasonic sensor, most commonly used for range detection/ distance measuring. It uses SONAR mechanism for its working and can detect obstacles from (1-13) feet.
Ultrasonics are Independent of :- Light
- Smoke
- Dust
- Color
- Material (except for soft surfaces, i.e. wool, because the surface absorbs the ultrasonic sound wave and doesn’t reflect sound.)
Long range detection of targets with varied surface properties.
Ultrasonic sensors are superior to infrared sensors because they aren’t affected by smoke or black materials, however, soft materials which don’t reflect the sonar (ultrasonic) waves very well may cause issues. It’s not a perfect system, but it’s good and reliable.
Circuit Set-up:
The technical specifications of the sensor are :
- Power Supply − +5V DC
- Quiescent Current − <2mA
- Working Current − 15mA
- Effectual Angle − <15°
- Ranging Distance − 2cm – 400 cm/1″ – 13ft
- Resolution − 0.3 cm
- Measuring Angle − 30 degree
Let’s understand the working using an Arduino Genuino/Uno:
const int pingPin = 9; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 10; // Echo Pin of Ultrasonic Sensor
void setup() {
Serial.begin(9600); // Starting Serial Terminal (baud rate)
}
void loop() {
long duration, inches, cm;
pinMode(pingPin, OUTPUT); //tells the sensor to send a signal from trigger pin
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH); //sending the signal
delayMicroseconds(10); // waiting
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT); //receiving back at the echo
duration = pulseIn(echoPin, HIGH); // we measured the duration by calculating the time difference for which the echo pin received the signal back from the trigger pin after reflection from the obstacle. This mechanism is also referred to as echolocation.
inches = microsecondsToInches(duration); //called the function we created below
cm = microsecondsToCentimeters(duration);
Serial.print(inches); //printing to serial monitor
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println(); //command to invoke the serial monitor.
delay(100);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}
Working Demo :-
After uploading the code press ctrl+shift+M.Serial monitor will begin displaying the values of cm and inches.visit Working of HC-SR04
Comments