Obstacle-avoiding robot
Mount one (or three) on a rover to detect walls and steer around them.
A low-cost ultrasonic distance sensor that measures 2 cm to 4 m by timing a 40 kHz echo.

The HC-SR04 is a low-cost ultrasonic distance sensor found in more Arduino and Raspberry Pi projects than almost any other part. It measures distance the way a bat or sonar does: it emits a burst of 40 kHz ultrasound from its transmitter and times how long the echo takes to bounce back to its receiver. Because the speed of sound is roughly constant, that round-trip time maps directly to distance.
It has four pins: VCC, Trig, Echo, and GND. To take a reading you send a 10 µs HIGH pulse to the Trigger pin; the module fires eight 40 kHz cycles and then holds the Echo pin HIGH for a time proportional to the round-trip distance. You convert that echo pulse width to a distance with distance (cm) = echo time (µs) ÷ 58 (from the speed of sound, ~343 m/s, divided by two for the there-and-back trip).
In practice it reliably measures from about 2 cm out to 400 cm, with roughly 3 mm resolution over a ~15° beam. It works best against flat, hard, perpendicular surfaces; soft or steeply angled targets scatter the sound and read poorly. The one thing to watch: the Echo pin outputs a 5 V signal, which needs attention on 3.3 V boards (see Circuit requirements below).
| Pin | Function | Type |
|---|---|---|
VCC VCC (5V) | Power | Power in |
TRIG Trigger | Digital | Digital input |
ECHO Echo | Digital | Digital output |
GND GND | Power | Power in |
Every specification is cited to its source.
The HC-SR04 connects to any of the microcontrollers supported in Tinkered. Mind the 5 V logic level on 3.3 V boards.
The HC-SR04 is fully simulated in Tinkered. Wire it into a circuit, write your firmware, and read real values back with no physical hardware. Test your logic before you touch a breadboard.
Open it in the simulatorThe Echo pin drives a 5 V signal. On ESP32, ESP8266, Pi Pico or Raspberry Pi, connecting it directly can exceed the input's maximum voltage and damage the pin. Add a voltage divider or level shifter on Echo.
The standard HC-SR04 expects a 5 V supply. Powered from 3.3 V it gives short, noisy, or missing readings. Use 5 V, or switch to the 3.3 V-rated HC-SR04P variant.
If the sensor's GND isn't tied to the microcontroller's GND, the echo pulse timing floats and distances jump around. Always connect grounds together.
Re-triggering before the last echo returns, or waiting forever when nothing is in range, produces stuck or garbage values. Space readings ~60 ms apart and give pulseIn() a timeout.
Foam, fabric, and steeply angled targets absorb or deflect the 40 kHz beam, so the echo never comes back cleanly. Expect best results from flat, hard surfaces facing the sensor within its ~15° cone.
Mount one (or three) on a rover to detect walls and steer around them.
Beep faster as a car approaches a wall, like a factory parking assistant.
Point it down a tank to read fill level without any part touching the water.
Trigger a pump or servo when a hand enters range for a no-touch soap or sanitizer station.
Open the lid automatically when someone steps close.
Sweep the sensor on a servo and plot distance vs. angle for a mini sonar display.
Drop the HC-SR04 into a circuit, write firmware, and simulate it in your browser, then deploy to real hardware. All in one editable Tinkered project.