Bird
0
0
Arduinoprogramming~20 mins

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

Choose your learning style9 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.