Bird
0
0
Arduinoprogramming~10 mins

Ultrasonic distance sensor (HC-SR04) in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Ultrasonic distance sensor (HC-SR04)
Start
Send Trigger Pulse
Wait for Echo Signal
Measure Echo Duration
Calculate Distance
Output Distance
Repeat or Stop
The sensor sends a sound pulse, waits for the echo, measures the time, then calculates and outputs the distance.
Execution Sample
Arduino
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

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(500);
}
This code triggers the sensor, measures echo time, calculates distance in cm, and prints it every half second.
Execution Table
StepActionPin StateDuration (microseconds)Distance (cm)Output
1Set trigPin LOWtrigPin=LOW
2Wait 2 microsecondstrigPin=LOW
3Set trigPin HIGHtrigPin=HIGH
4Wait 10 microsecondstrigPin=HIGH
5Set trigPin LOWtrigPin=LOW
6Measure pulse on echoPinechoPin=HIGH during pulse600
7Calculate distance60010
8Print distance1010
9Wait 500 ms
10Repeat loop
💡 Loop repeats indefinitely to update distance continuously.
Variable Tracker
VariableStartAfter Step 6After Step 7After Step 8Final
duration0600600600600
distance00101010
Key Moments - 3 Insights
Why do we set trigPin LOW before sending the HIGH pulse?
Setting trigPin LOW first ensures a clean pulse start; see execution_table rows 1-3 where LOW then HIGH creates a clear 10 microsecond pulse.
How does pulseIn measure the echo duration?
pulseIn waits for echoPin to go HIGH and measures how long it stays HIGH, shown in execution_table row 6 with duration 600 microseconds.
Why do we divide duration by 2 when calculating distance?
Sound travels to the object and back, so dividing by 2 gives the one-way distance; this is shown in the calculation at step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'duration' after step 6?
A10.2 cm
B600 microseconds
C500 milliseconds
D0
💡 Hint
Check the 'Duration (microseconds)' column at step 6 in execution_table.
At which step is the trigPin set HIGH to send the ultrasonic pulse?
AStep 2
BStep 5
CStep 3
DStep 6
💡 Hint
Look at the 'Pin State' column in execution_table for when trigPin changes to HIGH.
If the echo duration doubles, how does the distance value change in variable_tracker?
ADistance doubles
BDistance halves
CDistance stays the same
DDistance becomes zero
💡 Hint
Distance is calculated from duration * 0.034 / 2, so distance changes proportionally with duration (see variable_tracker).
Concept Snapshot
HC-SR04 sensor sends a 10μs HIGH pulse on trigPin.
It listens on echoPin for the returning pulse.
pulseIn measures echo duration in microseconds.
Distance (cm) = duration * 0.034 / 2.
Repeat measurement to update distance continuously.
Full Transcript
This visual trace shows how the HC-SR04 ultrasonic sensor works in Arduino code. First, the trigPin is set LOW to prepare, then HIGH for 10 microseconds to send a sound pulse. The echoPin listens for the returning pulse, and pulseIn measures how long the echoPin stays HIGH, which is the echo duration. The code calculates distance by multiplying duration by 0.034 (speed of sound in cm/us) and dividing by 2 because the sound travels to the object and back. The distance is printed and the loop repeats every 500 milliseconds to update the reading continuously. Variables duration and distance change step-by-step as shown. Key points include why trigPin is set LOW before HIGH, how pulseIn works, and why dividing by 2 is necessary. The quizzes test understanding of these steps and variable changes.