Given this Arduino code snippet using the HC-SR04 sensor, what will be printed to the Serial Monitor if the sensor measures a 20 cm distance?
const int trigPin = 9; const int echoPin = 10; long duration; int distance; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } 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.println(distance); delay(1000); }
Remember the speed of sound is approximately 340 m/s, and the sensor measures the time for the sound to travel to the object and back.
The duration is the time for the sound to travel to the object and back. Dividing by 2 gives the one-way time. Multiplying by 0.034 converts microseconds to centimeters. For a 20 cm distance, the code prints 20.
Consider this Arduino code snippet using HC-SR04. What will be printed if there is no object in front of the sensor (no echo received)?
const int trigPin = 9; const int echoPin = 10; long duration; int distance; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH, 30000); // 30 ms timeout distance = duration * 0.034 / 2; Serial.println(distance); delay(1000); }
Check what pulseIn returns if no pulse is detected within the timeout.
pulseIn returns 0 if no pulse is detected within the timeout. So duration is 0, and distance calculates to 0.
This Arduino code using HC-SR04 always prints 0 for distance, even when an object is close. What is the bug?
const int trigPin = 9; const int echoPin = 10; long duration; int distance; void setup() { Serial.begin(9600); pinMode(trigPin, INPUT); pinMode(echoPin, OUTPUT); } 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.println(distance); delay(1000); }
Check the pinMode setup for trigPin and echoPin.
The trigPin must be set as OUTPUT to send the trigger pulse. The echoPin must be INPUT to receive the echo signal. The code has these reversed, so no pulse is sent or received, resulting in zero duration.
Identify the option that fixes the syntax error in this Arduino code snippet:
distance = duration * 0.034 / 2
Assume the rest of the code is correct.
const int trigPin = 9; const int echoPin = 10; long duration; int distance; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } 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.println(distance); delay(1000); }
Check the end of the line for missing punctuation.
In Arduino (C++), each statement must end with a semicolon. The missing semicolon causes a syntax error.
You want to improve the accuracy of distance measurements using the HC-SR04 sensor in your Arduino project. Which approach below is the best to reduce noise and get stable readings?
Think about how to handle fluctuating sensor readings.
Taking multiple readings and using the median reduces the effect of outliers and noise, improving accuracy. Increasing pulse duration or delay won't help accuracy, and changing the speed of sound constant arbitrarily causes wrong results.
