Welcome to this interesting series of sensors. Today, I will explain Ultrasonic sensor.
What is an ultrasonic sensor?
- ✔An ultrasonic sensor is a device that measures the distance between two objects using ultrasonic sound waves.
- ✔A transducer is used in an ultrasonic sensor to transmit and receive ultrasonic signals.
What's the working funda of Ultrasonic sensor?
Ultrasonic sensors work by emitting sound waves at frequencies that are too high for humans to hear. The distance is then calculated based on the time it takes for the sound to be reflected back to them. This is similar to how radar estimates how long it takes a radio signal to return after interacting with an object.
If you need to compute the precise distance from your sensor, use the following formula:
The speed of sound at 20°C (68°F) is 343 meters/second (1125 feet/second), however this changes based on temperature and humidity. However, because sound travels at 4.3 times the speed of light in water as it does in air, this calculation must be considerably modified.
How Ultrasonic Sensor looks like?
Ultrasonic sensor has 4 pins -
- Vcc -> Connects to 5V of positive voltage for power
- Trig -> A pulse is sent here for the sensor to go into ranging mode for object detection
- Echo -> Sends a signal back if an object is detected
- Gnd -> Completes electrical pathway of the power
How to connect Ultrasonic sensor with Arduino?
- Connect pin
D11
Arduino to pinEcho
of HC-SR04 - Connect pin
D12
Arduino to pinTrig
of HC-SR04 - Connect pin
5V
Arduino to pinVcc
of HC-SR04 - Connect pin
Gnd
Arduino to pinGnd
of HC-SR04
Source Code for measuring distance between objects
#define echoPin 11
#define trigPin 12
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
Applications of Ultrasonic Sensor
- This sensor is used in car parking system where car entry is controlled through barrier system, the barrier must not be lowered when there is beneath a vehicle. This whole process is controlled through ultrasonic sensor.
- In oil, chemical, milk, or water tanks, ultrasonic sensors are utilised for level measurement or liquid level management.
- This sensor is utilised for high-speed counting in through beam detection.
- This sensor is used in the robotics sector to detect robots.
- This sensor is utilised with bottle cutting and drink filling equipment, where the bottle is detected at several locations and the ultrasonic sensor is employed for continuous monitoring.
- This sensor detects the speed of a motor or generator.
- This sensor is also utilised in systems that sense presence.
Stay tuned to this series to gain knowledge of next sensor ... [ Proximity Sensor]