Ultrasonic Distance Measurement
What is Ultrasonic?
In physics, sound is a vibration that propagates as an acoustic wave through a transmission medium such as a gas, liquid or solid. Human ear can hear sounds between 20 Hz and 20,000 Hz. Frequencies above 20,000 Hz are known as ultrasound. Ultrasound refers to any sound waves with frequencies greater than 20kHz which is inaudible to human. The term sonic is applied to ultrasound waves of very high amplitudes.
HC-SR04 Hardware Overview
The HC-SR04 is low power (suitable for battery operated devices), affordable, easy to interface and extremely popular non-contact distance measurement functionality with a range from 2cm to 400cm (about an inch to 13 feet) with a ranging accuracy that can reach up to 3mm
Working Principle of HC-SR04
The ultrasonic sensor works on the principle of SONAR and RADAR system which is used to determine the distance to an object. An ultrasonic sensor generates high-frequency sound (ultrasound) waves. When this ultrasound hits the object, it reflects as echo which is sensed by the receiver sensor.
HC-SR04 is composed of two ultrasonic transducers. One is transmitter which converts the electrical signal into 40 KHz ultrasonic sound pulses and the other is receiver which listens for reflected waves.
HC-SR04 operates on 5 volts so it can be directly connected to an Arduino or any other 5V logic micro-controller.
Technical Specifications
Here are the specifications:
Operating Voltage 5V DC
Operating Current 15mA
Operating Frequency 40KHz
Max Range 4m
Min Range 2cm
Ranging Accuracy 3mm
Measuring Angle 15 degree
Trigger Input Signal 10µS TTL pulse
Dimension 45 x 20 x 15mm
HC-SR04 Ultrasonic Sensor Pinout
Distance = (Speed x Time) / 2
const unsigned int TRIG_PIN1=13;
const unsigned int ECHO_PIN1=12;
const unsigned int BAUD_RATE=9600;
void setup() {
pinMode(TRIG_PIN1, OUTPUT);// Sets the trigPin as an Output
pinMode(ECHO_PIN1, INPUT);// Sets the echoPin as an Input
Serial.begin(BAUD_RATE);
}
void loop() {
// Clears the trigPin
digitalWrite(TRIG_PIN1, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(TRIG_PIN1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN1, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
const unsigned long duration1= pulseIn(ECHO_PIN1, HIGH);
// Calculating the distance
int distance1= duration1/29/2;
if(duration1==0){
Serial.println("Warning: no pulse from sensor");
}
else{
Serial.print("distance to nearest object:");
Serial.println(distance1);
Serial.println(" cm");
}
delay(100);
}