Bird
Raised Fist0
Arduinoprogramming~20 mins

Ultrasonic distance sensor (HC-SR04) in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ultrasonic Sensor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output distance in cm?

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?

Arduino
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);
}
A10
B20
C0
D40
Attempts:
2 left
💡 Hint

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.

❓ Predict Output
intermediate
2:00remaining
What does this code print if no object is detected?

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)?

Arduino
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);
}
A0
B510
C-1
D30000
Attempts:
2 left
💡 Hint

Check what pulseIn returns if no pulse is detected within the timeout.

🔧 Debug
advanced
3:00remaining
Why does this HC-SR04 code always print 0?

This Arduino code using HC-SR04 always prints 0 for distance, even when an object is close. What is the bug?

Arduino
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);
}
AThe trigPin and echoPin modes are reversed; trigPin should be OUTPUT and echoPin INPUT.
BThe delayMicroseconds values are too small to trigger the sensor.
CThe pulseIn function is used incorrectly; it needs a timeout parameter.
DThe Serial.begin baud rate is missing and causes communication failure.
Attempts:
2 left
💡 Hint

Check the pinMode setup for trigPin and echoPin.

📝 Syntax
advanced
2:00remaining
Which option fixes the syntax error in this HC-SR04 code?

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.

Arduino
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);
}
ARemove the entire distance assignment line.
BChange the multiplication operator * to a dot .
CReplace the division operator / with a comma ,
DAdd a semicolon at the end of the distance assignment line.
Attempts:
2 left
💡 Hint

Check the end of the line for missing punctuation.

🚀 Application
expert
3:00remaining
How to improve distance measurement accuracy with HC-SR04?

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?

AChange the speed of sound constant from 0.034 to 0.1 in the calculation.
BIncrease the trigger pulse duration from 10 microseconds to 100 microseconds.
CTake multiple distance readings in a short loop, then calculate and use the median value.
DUse delay(5000) between readings to let the sensor cool down.
Attempts:
2 left
💡 Hint

Think about how to handle fluctuating sensor readings.

Practice

(1/5)
1. What is the main purpose of the trigger pin on the HC-SR04 ultrasonic sensor?
easy
A. To send a short ultrasonic pulse to start distance measurement
B. To receive the echo signal from the object
C. To power the sensor
D. To reset the sensor

Solution

  1. Step 1: Understand the role of the trigger pin

    The trigger pin is used to send a short pulse that starts the ultrasonic burst.
  2. Step 2: Differentiate from echo pin

    The echo pin listens for the returning pulse, not the trigger pin.
  3. Final Answer:

    To send a short ultrasonic pulse to start distance measurement -> Option A
  4. Quick 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
A. digitalWrite()
B. pulseIn()
C. analogRead()
D. delay()

Solution

  1. Step 1: Identify function to measure pulse duration

    The pulseIn() function measures the length of a pulse on a pin.
  2. Step 2: Exclude other functions

    digitalWrite() sets pin output, analogRead() reads analog values, delay() pauses execution.
  3. Final Answer:

    pulseIn() -> Option B
  4. Quick 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
A. 80 cm
B. 40 cm
C. 10 cm
D. 20 cm

Solution

  1. Step 1: Calculate half of echo time

    Half of 1160 microseconds is 580 microseconds.
  2. Step 2: Divide by 29.1 to get distance

    580 / 29.1 ≈ 19.93 cm, approximately 20 cm.
  3. Final Answer:

    20 cm -> Option D
  4. Quick 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
A. echoTime should be declared as int, not long
B. delayMicroseconds should be delay()
C. pulseIn should measure HIGH pulse, not LOW
D. triggerPin should be set LOW before HIGH

Solution

  1. Step 1: Check pulseIn usage

    The echo pulse is HIGH when the echo is received, so pulseIn should measure HIGH pulse.
  2. 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.
  3. Final Answer:

    pulseIn should measure HIGH pulse, not LOW -> Option C
  4. Quick 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
A. digitalWrite(triggerPin, LOW); delayMicroseconds(2); digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); echoTime = pulseIn(echoPin, HIGH); distance = (echoTime / 2) / 29.1;
B. digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); echoTime = pulseIn(echoPin, LOW); distance = echoTime * 29.1 / 2;
C. digitalWrite(triggerPin, LOW); delay(10); digitalWrite(triggerPin, HIGH); echoTime = pulseIn(echoPin, HIGH); distance = (echoTime / 2) / 29.1;
D. digitalWrite(triggerPin, HIGH); delayMicroseconds(2); digitalWrite(triggerPin, LOW); echoTime = pulseIn(echoPin, HIGH); distance = (echoTime * 2) / 29.1;

Solution

  1. 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.
  2. Step 2: Verify echo measurement and distance formula

    pulseIn measures HIGH pulse duration; distance = (echoTime / 2) / 29.1 converts time to cm correctly.
  3. Final Answer:

    Code snippet C correctly triggers and calculates distance -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard