Measure distance with an ultrasonic sensor

Use the HC-SR04 ultrasonic sensor to measure distance in centimetres and show it in the Serial Monitor.
  • Level:Beginner
  • Duration: 40 min
  • Ages 10+
  • Platform: Arduino

What you'll need

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Measure distance with an ultrasonic sensor – EEMENTOR STEM™ lesson

Bats find their way with sound, and so can your Arduino. The ultrasonic sensor sends out a short sound pulse that people can't hear and times how long the echo takes to come back.

What you'll learn

  • How an ultrasonic sensor measures distance
  • How to time a pulse with pulseIn()
  • How to print values to the Serial Monitor

What you need

  • UNO R3 board and USB cable
  • Ultrasonic sensor (HC-SR04)
  • Breadboard
  • 4 jumper wires

Wiring

Breadboard wiring diagram: Measure distance with an ultrasonic sensor
Breadboard view. Check each connection against the wiring table below.

Build it step by step

Plug in all the parts first, then add the wires in this order. In each picture the new wires are bright and the ones from earlier steps are faded. Click a picture to see it larger.

Step 1 of 2: Power rails: 5V and GND from the UNO
Step 1. Power rails: 5V and GND from the UNO
Step 2 of 2: Ultrasonic sensor HC-SR04: 2 wires to the UNO and 2 more
Step 2. Ultrasonic sensor HC-SR04: 2 wires to the UNO and 2 more
Sensor pin UNO pin
VCC 5V
Trig 9
Echo 10
GND GND
FREE

Build this circuit in EEMENTOR Lab

Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.

The UNO and the parts for this lesson laid out in EEMENTOR Lab, ready to wireBuild it yourself
  1. The UNO and this lesson’s parts are already placed. Wire them using the wiring table above. Pins with no wire show red.
  2. Missing a part? Add it from the parts list on the left.
  3. Press Run, then try the inputs: click, hold or slide them under Selected.
  4. Then try this lesson’s sketch: paste it into the Code tab and press Compile & Run.

EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab

The sketch

const int TRIG_PIN = 9;
const int ECHO_PIN = 10;

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  // Send a 10 microsecond pulse to start a measurement
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Time the echo (in microseconds), give up after 30 ms
  long duration = pulseIn(ECHO_PIN, HIGH, 30000);

  if (duration == 0) {
    Serial.println("Out of range");
  } else {
    // Sound travels about 0.0343 cm per microsecond, there and back
    float distanceCm = duration * 0.0343 / 2;
    Serial.print("Distance: ");
    Serial.print(distanceCm);
    Serial.println(" cm");
  }
  delay(200);
}

Upload the sketch, then open Tools → Serial Monitor and set the speed to 9600 baud. Move your hand in front of the sensor and watch the distance change.

What you should see

Serial Monitor at 9600 baud showing distances from 51 cm down to 5 cm as a hand moves closer to the ultrasonic sensor, and Out of range with nothing in front.

How it works

Trig and Echo signal diagram: a 10 µs trigger pulse, then Echo stays HIGH for 1166 µs; the sound goes 20 cm there and back, so 1166 × 0.0343 ÷ 2 = 20 cm.

The sound travels to the object and back, so the sketch divides the total time by two. At room temperature sound moves about 343 metres per second, which is 0.0343 cm per microsecond. The sensor works best between about 2 cm and 4 m.

Try this next

  • Add the LED from lesson 1 and switch it on when something is closer than 20 cm.
  • Open Tools → Serial Plotter to see the distance as a live graph.

Troubleshooting

  • Always "Out of range": check that Trig and Echo aren't swapped.
  • Jumpy readings: soft or angled surfaces reflect sound poorly. Point the sensor at a flat, hard object.