Bird
0
0
Arduinoprogramming~10 mins

Ultrasonic distance sensor (HC-SR04) in Arduino - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the trigger pin for the HC-SR04 sensor.

Arduino
const int trigPin = [1];
Drag options to blanks, or click blank then click option'
A2
B5
C13
D9
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing an analog pin instead of a digital pin for the trigger.
Using the same pin number for both trigger and echo.
2fill in blank
medium

Complete the code to set the echo pin as an input in the setup function.

Arduino
pinMode([1], INPUT);
Drag options to blanks, or click blank then click option'
AechoPin
BtrigPin
CledPin
DbuttonPin
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the trigger pin as input instead of output.
Using a pin variable that is not defined.
3fill in blank
hard

Fix the error in the code to send a 10 microsecond pulse to the trigger pin.

Arduino
digitalWrite(trigPin, HIGH);
delayMicroseconds([1]);
digitalWrite(trigPin, LOW);
Drag options to blanks, or click blank then click option'
A1
B100
C10
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using delay() instead of delayMicroseconds().
Using too long or too short pulse duration.
4fill in blank
hard

Fill both blanks to correctly calculate the distance in centimeters from the duration.

Arduino
long duration = pulseIn([1], HIGH);
float distance = duration [2] 58.2;
Drag options to blanks, or click blank then click option'
AechoPin
B*
C/
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Multiplying duration instead of dividing.
Using the trigger pin instead of echo pin for pulseIn.
5fill in blank
hard

Fill all three blanks to complete the loop that reads distance and prints it to the serial monitor.

Arduino
digitalWrite([1], LOW);
delayMicroseconds(2);
digitalWrite([2], HIGH);
delayMicroseconds(10);
digitalWrite([2], LOW);

long duration = pulseIn([3], HIGH);
float distance = duration / 58.2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
Drag options to blanks, or click blank then click option'
AtrigPin
BechoPin
Attempts:
3 left
💡 Hint
Common Mistakes
Using echo pin to send pulse instead of trigger pin.
Mixing up the order of setting pins HIGH and LOW.