Complete the code to define the trigger pin for the HC-SR04 sensor.
const int trigPin = [1];The trigger pin is set to digital pin 9 to send the ultrasonic pulse.
Complete the code to set the echo pin as an input in the setup function.
pinMode([1], INPUT);The echo pin must be set as an input to read the returning ultrasonic pulse.
Fix the error in the code to send a 10 microsecond pulse to the trigger pin.
digitalWrite(trigPin, HIGH);
delayMicroseconds([1]);
digitalWrite(trigPin, LOW);The HC-SR04 sensor requires a 10 microsecond pulse on the trigger pin to start measurement.
Fill both blanks to correctly calculate the distance in centimeters from the duration.
long duration = pulseIn([1], HIGH); float distance = duration [2] 58.2;
The pulseIn function measures the time on the echo pin. Distance is calculated by dividing duration by 58.2.
Fill all three blanks to complete the loop that reads distance and prints it to the serial monitor.
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");
The trigger pin is first set LOW, then HIGH for 10 microseconds, then LOW again to send the pulse. The echo pin is used to measure the pulse duration.
