The HC-SR04 sensor helps measure how far away something is by using sound waves. It sends a sound pulse and listens for the echo to find the distance.
Ultrasonic distance sensor (HC-SR04) in Arduino
Introduction
Syntax
Arduino
const int trigPin = 9; const int echoPin = 10; long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * 0.034 / 2; Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); delay(500); }
The trigPin sends a short sound pulse.
The echoPin listens for the returning sound to measure time.
Examples
Arduino
digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);
Arduino
duration = pulseIn(echoPin, HIGH);
Arduino
distance = duration * 0.034 / 2;
Sample Program
This program measures the distance using the HC-SR04 sensor and prints it every half second to the Serial Monitor.
Arduino
const int trigPin = 9; const int echoPin = 10; long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * 0.034 / 2; Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); delay(500); }
Important Notes
Make sure the sensor's VCC and GND are connected correctly to power it.
Keep the sensor steady and avoid obstacles in front to get accurate readings.
The sensor works best for distances between 2 cm and 400 cm.
Summary
The HC-SR04 uses sound pulses to measure distance.
Send a short pulse on the trigger pin, then measure the echo time.
Convert the echo time to distance using the speed of sound.
Practice
1. What is the main purpose of the
trigger pin on the HC-SR04 ultrasonic sensor?easy
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]
Hint: Trigger pin sends pulse, echo pin listens [OK]
Common Mistakes:
- Confusing trigger with echo pin
- Thinking trigger powers the sensor
- Assuming trigger resets the sensor
2. Which Arduino function is used to measure the duration of the echo pulse from the HC-SR04 sensor?
easy
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]
Hint: Use pulseIn() to measure echo pulse duration [OK]
Common Mistakes:
- Using digitalWrite() to read pulse
- Confusing analogRead() with pulse measurement
- Using delay() instead of measuring pulse
3. What will be the output distance (in cm) if the measured echo time is 1160 microseconds? Use the formula:
distance = (echoTime / 2) / 29.1.medium
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]
Hint: Divide echo time by 2 and then by 29.1 [OK]
Common Mistakes:
- Forgetting to divide echo time by 2
- Using wrong divisor instead of 29.1
- Mixing units of time and distance
4. Identify the error in this Arduino code snippet for HC-SR04 distance measurement:
digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); echoTime = pulseIn(echoPin, LOW);
medium
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]
Hint: pulseIn(echoPin, HIGH) to measure echo pulse [OK]
Common Mistakes:
- Measuring LOW pulse instead of HIGH
- Using delay() instead of delayMicroseconds()
- Setting triggerPin LOW before HIGH incorrectly
5. You want to measure distance continuously and print it every second using HC-SR04. Which code snippet correctly triggers the sensor and calculates distance in cm?
hard
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]
Hint: Trigger LOW-2us, HIGH-10us, then LOW; measure HIGH pulse [OK]
Common Mistakes:
- Skipping initial LOW trigger pulse
- Measuring LOW pulse instead of HIGH
- Incorrect distance formula with wrong multiplication/division
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
