Ultrasonic sensors utilize sound waves to measure distances and detect objects by emitting short bursts of ultrasonic waves, which travel through the air. They generate high-frequency sound waves that are beyond the range of human hearing (typically above 20,000 Hz). These sound waves travel through the air or other mediums and bounce back when they encounter an object or surface. These sensors can be compared to ears of the machines where they get to know by the intensity of sound waves, that, how close or how far is the object from them for informed decision making.
When these waves encounter an obstacle/object, they bounce back and are read by a receiver in the sensor. By measuring the time it takes for the waves to travel to and from the object, the sensor can calculate the distance. This functionality is analogous to how the human auditory system processes sound waves and determines the distance and location of objects based on the timing and intensity of the sound signals The most common applications include obstacle detection and avoidance, distance based decision making in smart factories, robotics, automation, pick and place machines, parking sensors, medical imaging, distance sensing.
Advantages: Easy to Interface, available in water proof versions, versatile , zero physical contact, works on laws of reflection,
To understand more intricacies, lets take for example, the most commonly used ultrasonic sensor in the industry less military grade applications which require it to be more robust in terms of stress handling viz pressure, temperature, vibration and EMI EMC. The most commonly used sensor is HC-SR04 and it looks as under.

img credit – hackster.io 1. Transmitter: The HC-SR04 has an ultrasonic transmitter that emits high-frequency sound waves of around 40 kHz.
2. Receiver: The sensor also includes a receiver that detects the ultrasonic waves reflected back from objects or surfaces. It picks up the echo signals for distance measurement.
3. Protruding Sensors: On the front side of the module, there are two circular sensors positioned side by side. One of them is the transmitter, and the other is the receiver.
4. VCC: This pin is used to provide power to the sensor module typically 5V source.
5. GND: This pin is the ground connection for the sensor module.
6. Trig: This pin is used to initiate the ultrasonic pulse transmission connected to a digital output pin of a microcontroller. 7. Echo: The echo pin receives the echo signals from the reflected ultrasonic waves and connected to a digital input pin of a microcontroller. Lets understand the functionaluty of the same by writing a simple code to detect an object as Near Far or Too far from the sensor with Near defined as 5cm, Far defined as 10cm, Too Far is beyond 10cm.
const int trigger = 2; // Trig pin connected to digital pin 2
const int echo = 3; // Echo pin connected to digital pin 3
// Distance (in centimeters)
const int neard = 5; // Near threshold distance
const int fard = 10; // Far threshold distance
const float speedOfSound = 0.0343;
long duration; //variable to store duration of waveform travelled
int distance; // variable to store distance
void setup() {
// Init serial communication
Serial.begin(9600); // baud rate 9600
// Set trigPin as an output and echoPin as an input
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
// Trigger the sensor by sending a 10us high pulse to the trigPin
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
// Measure the duration of the echo pulse
duration = pulseIn(echo, HIGH);
// Calculate the distance in centimeters
distance = duration * speedOfSound / 2;
// Print the measured distance
Serial.print("Distance from sensor: ");
Serial.print(distance);
Serial.println(" cm");
// Check if the distance is within the near or far thresholds
if (distance <= neard) {
Serial.println("Near");
// Perform actions for near distance
// ...
} else if (distance <= fard) {
Serial.println("Far");
// Perform actions for far distance
// ...
} else {
Serial.println("Too Far Away");
// Perform actions for distances beyond the specified thresholds
// ...
}
// Wait for a short delay before taking the next distance measurement
delay(1000);
}
Drop me any improvements or suggestions at sulabh@sslabs.in