Skip to content
DersTeders Log inSign up My panel

Measuring Distance with HC-SR04

Calculate the distance to an obstacle from the echo time of an ultrasonic sensor.

Circuit diagram

Measuring Distance with HC-SR04 — diagram of the arduino circuit built on a breadboard
Measuring Distance with HC-SR04 circuit breadboard layout and connections.

Interactive circuit guide

Run the simulation, follow the build steps one by one and try it in our real app.

Real screenshot of our circuit simulator Stopped
Open in our app →

Step-by-step build

Open in simulator

You can open the same circuit in the browser-based circuit simulator without any installation and inspect the wires and components there.

Parts list

ComponentQtyDescription
Arduino Uno1Measures the pulse duration and calculates the distance.
HC-SR04 sensor1Sends an ultrasonic pulse and detects the echo.
5V supply connection1Supplies voltage to the sensor's power leg.
Ground connection1Common reference for the sensor and the board.

Breadboard layout

The Arduino is at the lower left, the sensor at the upper right, the 5V supply at the top and the common ground at the lower right. The trigger pin goes to Arduino pin 3, the echo pin to Arduino pin 4; the sensor ground connects to common ground.

Step-by-step build

  1. Place the Arduino Uno on the work area.
    Tip: The GND pin is index 0.
  2. Connect the sensor's VCC leg to 5V.
    Tip: On the sensor, pin 0 is power, pin 1 trigger, pin 2 echo and pin 3 ground.
  3. Connect the trigger and echo pins to Arduino pins 3 and 4.
    Tip: Do not mix up the two signal lines.
  4. Connect the sensor ground and the Arduino GND to common ground.
    Tip: Without a ground connection the measurement is unstable.
  5. Upload the code, put an obstacle in front and read the distance on the serial port.
    Tip: Add a short wait between measurements.

How it works

When the trigger pin is pulled high, the sensor emits an ultrasonic pulse and at the same time raises the echo pin high. The echo pin stays high until the pulse returns from an obstacle and is detected. This measured time is the round-trip time of the sound; to find the one-way distance, the time is halved. Since the speed of sound is known, distance is calculated as half the product of time and speed. Because the speed of sound depends on temperature, ambient temperature can be taken into account for precise measurements. Sufficient waiting between pulse repetitions prevents echoes from mixing with each other.

What you will learn

  • Makes an indirect measurement from the echo time.
  • Applies the relation that converts time into distance.
  • Manages the trigger and echo signals on separate pins.

Expected result: The distance to an obstacle placed in front appears on the serial port in centimetres.

Challenge

Distance meter: Calculate the distance to an obstacle in centimetres.

Goal: The distance value appears on the serial port.

Tips

  • If the obstacle is too close, the sensor may not measure reliably.
  • Soft fabric surfaces weaken the echo.

Frequently asked questions

How is the distance calculated?

The time the echo pin stays high is measured; this is the round-trip time. For one-way distance the time is halved and multiplied by the speed of sound.

Can trigger and echo use the same pin?

The template uses two separate pins: trigger pin 3 and echo pin 4. Separate lines are preferred so the signals do not mix.

What happens if the echo returns weakly?

On soft and angled surfaces the echo weakens; the measurement can become unstable.

If you want to build this project in real life, you can get the parts from hobidevre.com. For more circuit ideas, visit the hobidevre.com blog.

Blog · Circuit simulator · KVKK Disclosure