Distance
Interfacing HC-SR04 Distance with STM32 Blue Pill
What is the HC-SR04?
The HC-SR04 measures distance by timing how long it takes a 40kHz ultrasonic pulse to bounce off an object and return. You send it a short trigger pulse, it fires eight ultrasonic bursts, and the ECHO pin stays high for exactly as long as the round trip took — convert that time to distance and you're done. No library is strictly required since the whole protocol is just two GPIO pins and a stopwatch.
It's a great first sensor for obstacle avoidance, parking sensors, liquid-level gauges, and non-contact proximity switches. The catch that trips people up most is voltage: the sensor runs on 5V and its ECHO pin outputs a 5V pulse, which will drive a 3.3V-only board's GPIO out of spec. Every board below except the 5V-native Arduino Uno needs a simple voltage divider on ECHO — see the per-board wiring notes.
Specifications
- Operating voltage
- 5 V DC
- Operating current
- ~15 mA
- Measuring range
- 2cm to 400cm
- Accuracy
- ±3mm
- Measuring angle
- ~15° effective beam angle
- Ultrasonic frequency
- 40 kHz
- Trigger pulse width
- 10 µs TTL pulse to start a measurement
- Interface
- Digital trigger/echo pulse pair — no library or bus protocol required
Pinout
| Pin | Name | Description |
|---|---|---|
| 1 | VCC | Power, 5V DC |
| 2 | TRIG | Trigger input — send a 10µs HIGH pulse to start a measurement |
| 3 | ECHO | Echo output — goes HIGH for the duration of the ultrasonic round trip. Outputs at VCC level (5V) regardless of board voltage. |
| 4 | GND | Ground |
Swipe sideways to see every column →
TRIG is a standard input and is safe to drive from a 3.3V GPIO — most boards read a 3.3V HIGH as a valid trigger. ECHO is the pin that causes problems: it always swings up to the sensor's 5V supply rail, so wiring it straight into a 3.3V-only GPIO (ESP32, Raspberry Pi, STM32) risks damaging that pin over time. Use a simple resistor voltage divider on ECHO — 1kΩ in series with the signal and 2kΩ from the tap point to GND works well — to bring the HIGH level down to a safe ~3.3V before it reaches a 3.3V board.
Variants
- HC-SR04
- ~$1–3
- HY-SRF05
- ~$2–4
- JSN-SR04T
- ~$3–6
- US-100
- ~$3–5
The plain HC-SR04 is the default choice for indoor, dry, breadboard-range projects. If the sensor needs to live outdoors or in a tank, use the waterproof JSN-SR04T instead — same trigger/echo timing concept, but check its longer minimum range before swapping it in. If you want native 3.3V operation without a voltage divider, the US-100 is the more forgiving pick.
Setting Up HC-SR04 with STM32 Blue Pill
Library
None — uses built-in pulseIn() by Arduino core (via STM32duino)
Boards Manager → add the STM32duino package URL → install "STM32 MCU based boards". No additional library needed — this uses only core digitalWrite()/pulseIn() calls.
Wiring
| Component pin | STM32 Blue Pill pin | Note |
|---|---|---|
| VCC | 5V | Blue Pill boards typically expose a 5V pin fed from the USB/VIN rail — confirm yours does before relying on it to power the sensor. |
| TRIG | PA0 | Safe to drive directly — the sensor reads the Blue Pill's 3.3V HIGH fine as a trigger. |
| ECHO | PA1 | REQUIRED: add a voltage divider (e.g. 1kΩ in series, 2kΩ from the tap to GND) before this pin — every GPIO on this board is 3.3V-only and NOT 5V-tolerant. |
| GND | GND | Common ground with the 5V supply is required for the divider math to work. |
Swipe sideways to see every column →
- Most Blue Pills don't have working USB-CDC serial out of the box, so
Serialmay not show up as a COM port. Flash over SWD with an ST-Link and view output through its virtual COM port, or wire a cheap USB-TTL adapter to PA9/PA10 (Serial1) instead. - Do not skip the ECHO voltage divider — this board's GPIOs are 3.3V-only with no built-in tolerance, unlike the Uno.
- Double-check which 5V source you're using for VCC: some Blue Pill boards only expose a real 5V rail when powered via USB, and run 5V-starved or not at all on battery/VIN-only power.
Related Components
Related Guides & Projects
HC‑SR04 Ultrasound Distance Measurement with Arduino – Parking Alert (LCD & Buzzer)
Learn HC‑SR04 ultrasound distance measurement with Arduino Uno. Build a parking alert system with 16x2 LCD and buzzer – shows distance and beeps faster as you approach.

ESP32 Ultrasonic Radar with Servo Sweep and Live Web Dashboard
Build a sweeping ultrasonic radar with an ESP32, SG90 servo, and HC-SR04 sensor, and watch live blips on an HTML5 canvas dashboard served straight from the chip — no app, no cloud.

Interfacing HC-SR04 Ultrasonic Sensor with Arduino
Build a clean, accurate distance meter with an Arduino Uno, an HC-SR04 ultrasonic sensor, and an I2C 1602 LCD — just four wires to the display, complete code, and the theory behind every line.
STM32 Distance Measurement System with OLED Display
Measure distance in real time with an HC-SR04 ultrasonic sensor and an STM32 Blue Pill, with live readings shown on an SSD1306 OLED display.