What if your robot could 'see' how far things are without eyes or touching them?
Why Ultrasonic distance sensor (HC-SR04) in Arduino? - Purpose & Use Cases
Imagine trying to measure the distance to an object by walking up to it with a ruler every time you want to know how far it is.
This is slow, tiring, and not practical for robots or automated systems.
Manually measuring distance takes a lot of time and effort.
It is also prone to mistakes and cannot be done continuously or quickly.
For machines, manual measurement is impossible without sensors.
The HC-SR04 ultrasonic sensor sends sound waves and measures how long they take to bounce back.
This lets your Arduino quickly and accurately calculate distance without touching the object.
It automates distance measurement easily and reliably.
// No code, just manual ruler measurement
long duration; float distance; // Trigger the sensor digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the echo duration = pulseIn(echoPin, HIGH); // Calculate the distance distance = duration * 0.034 / 2;
It enables robots and devices to sense their surroundings and react based on real-time distance data.
A robot vacuum uses the HC-SR04 sensor to detect walls and furniture so it can clean without bumping into things.
Manual distance measurement is slow and impractical for machines.
The HC-SR04 sensor automates distance sensing using sound waves.
This allows quick, accurate, and contactless measurement for many projects.
Practice
trigger pin on the HC-SR04 ultrasonic sensor?Solution
Step 1: Understand the role of the trigger pin
The trigger pin is used to send a short pulse that starts the ultrasonic burst.Step 2: Differentiate from echo pin
The echo pin listens for the returning pulse, not the trigger pin.Final Answer:
To send a short ultrasonic pulse to start distance measurement -> Option AQuick Check:
Trigger pin sends pulse = A [OK]
- Confusing trigger with echo pin
- Thinking trigger powers the sensor
- Assuming trigger resets the sensor
Solution
Step 1: Identify function to measure pulse duration
ThepulseIn()function measures the length of a pulse on a pin.Step 2: Exclude other functions
digitalWrite()sets pin output,analogRead()reads analog values,delay()pauses execution.Final Answer:
pulseIn() -> Option BQuick Check:
pulseIn measures pulse length = B [OK]
- Using digitalWrite() to read pulse
- Confusing analogRead() with pulse measurement
- Using delay() instead of measuring pulse
distance = (echoTime / 2) / 29.1.Solution
Step 1: Calculate half of echo time
Half of 1160 microseconds is 580 microseconds.Step 2: Divide by 29.1 to get distance
580 / 29.1 ≈ 19.93 cm, approximately 20 cm.Final Answer:
20 cm -> Option DQuick Check:
(1160/2)/29.1 ≈ 20 cm [OK]
- Forgetting to divide echo time by 2
- Using wrong divisor instead of 29.1
- Mixing units of time and distance
digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); echoTime = pulseIn(echoPin, LOW);
Solution
Step 1: Check pulseIn usage
The echo pulse is HIGH when the echo is received, so pulseIn should measure HIGH pulse.Step 2: Verify other code parts
delayMicroseconds(10) is correct for 10 microseconds pulse; triggerPin sequence is correct; echoTime type depends on usage but not an error here.Final Answer:
pulseIn should measure HIGH pulse, not LOW -> Option CQuick Check:
pulseIn measures HIGH pulse = D [OK]
- Measuring LOW pulse instead of HIGH
- Using delay() instead of delayMicroseconds()
- Setting triggerPin LOW before HIGH incorrectly
Solution
Step 1: Check trigger pulse sequence
The correct sequence is to set trigger LOW for 2 microseconds, then HIGH for 10 microseconds, then LOW again.Step 2: Verify echo measurement and distance formula
pulseIn measures HIGH pulse duration; distance = (echoTime / 2) / 29.1 converts time to cm correctly.Final Answer:
Code snippet C correctly triggers and calculates distance -> Option AQuick Check:
Trigger LOW-2us-HIGH-10us-LOW, pulseIn HIGH, distance formula = C [OK]
- Skipping initial LOW trigger pulse
- Measuring LOW pulse instead of HIGH
- Incorrect distance formula with wrong multiplication/division
Ready to prove it?
Pass the test and this pattern moves unknown
