Title
HC-SR04 Ultrasonic Sensor

What It Is
The HC-SR04 is an ultrasonic distance sensor. It measures how long it takes for a sound pulse to leave, bounce off an object, and return.
What Ultrasonic Means
"Ultrasonic" means sound at frequencies higher than human hearing. People usually hear up to about 20 kHz. The HC-SR04 works at about 40 kHz, so the sound pulses are far above what we normally hear.
What It Does In This Project
- measures distance in front of the sensor
- gives live values to the dashboard
- provides a real example of timing-based sensing
Origins And Background
The HC-SR04 became common in schools, hobby kits, and beginner robotics because it is low-cost and easy to demonstrate. It gives students a very visible “real world to number” sensor experience.
The deeper idea behind it is much older. Long before cheap classroom sensors existed, engineers used sound pulses to detect objects and measure distance. That is the basic idea behind sonar.
Sonar Connection
This is a great place to talk about submarines and ships. In sonar, a system sends out a sound ping and listens for the echo. If the echo comes back later, the object is farther away. If it comes back sooner, the object is closer.
The HC-SR04 is doing a tiny classroom version of that same idea:
- send a short ultrasonic pulse
- wait for the reflection
- measure the round-trip time
- convert time into distance
So when students use this sensor, they are really using a small modern version of a classic sonar principle.
How It Communicates
- uses two digital lines
TRIGinput tells it to start a measurementECHOoutput stays high for a time related to distance
Pinout
The standard front-facing HC-SR04 module usually exposes four pins:
VCC: powerTRIG: trigger inputECHO: echo pulse outputGND: ground
Course Wiring
In this course hardware:
TRIG-> BluepillPB4ECHO-> BluepillPB0VCC-> sensor power railGND-> common ground
That gives a nice example of one plain GPIO output and one timing-sensitive input capture path.
Why It Is Not SPI, I2C, or UART
This sensor does not send structured digital packets. It uses pulse timing instead. The MCU has to measure how long the echo pulse lasts.
Physical Layer Notes
This is a good example of the physical layer being important even when there is no fancy protocol. Voltage level, pulse width, and timing are the key parts.
Why It Matters
It teaches that not all devices speak a named serial protocol. Some devices simply use clever timing on digital pins.
Teaching Focus
- pulse timing
- time of flight
- sonar as a real-world analogy
- polling vs capture timing
- converting time into distance
Datasheet Navigation Tips
Look for:
- trigger pulse timing
- echo pulse behaviour
- voltage requirements
- distance range
- recommended measurement intervals
Interesting Detail
The sensor is really solving a mini sonar problem. It sends a burst of sound and waits for the reflection.
Good Questions To Ask Students
- Why does sound travel time tell us distance?
- Why is this different from I2C or SPI?
- Why does timing matter so much for this sensor?
- How is this similar to submarine sonar?