Projects / Ultrasonic tape measure

Ultrasonic tape measure

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. UNOHelloPCF8574

    Install the library

    Search for LiquidCrystal I2C in the Library Manager.

  2. UNO

    Wire the sensor

    VCC, GND, Trig on D9, Echo on D10.

  3. UNOHelloPCF8574

    Wire the LCD and button

    SDA and SCL to A4 and A5, button on D2 to GND.

  4. UNO

    Upload the sketch

    Screen shows Press to measure.

  5. UNO

    Aim at a wall 50 cm away

    Use a ruler to set the distance.

  6. UNO

    Press the button

    About 50.0 cm and 19.7 in appear.

  7. UNO

    Set AIR_TEMP_C to the room temperature

    Upload again and compare.

  8. UNO

    Try 20, 100 and 200 cm

    Note the error each time.

In short

How do you build an ultrasonic tape measure?

An ultrasonic sensor and a button measure the distance to a wall and show it in centimetres and inches on an LCD. You need Arduino Uno R3, HC-SR04 Ultrasonic Sensor, 16x2 LCD with I2C backpack (PCF8574), Tactile push button 6 mm, 9 V battery snap with DC jack, 9 V battery, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
8

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
HC-SR04 VCC and GNDUno 5V and GNDSensor power
HC-SR04 TrigUno D9Trigger pulse
HC-SR04 EchoUno D10Echo pulse length
LCD I2C SDA and SCLUno A4 and A5Display
LCD I2C VCC and GNDUno 5V and GNDDisplay power
Measure buttonUno D2 to GNDInternal pull-up, pressed reads LOW
9 V battery with DC jackUno barrel jackOptional, for a handheld unit

Code

The HC-SR04 sends a burst of ultrasound and the Echo pin stays HIGH for as long as the sound takes to go out and come back. pulseIn measures that time, and distance is time multiplied by the speed of sound, divided by two. The speed of sound rises about 0.6 metres per second for each degree Celsius, so the sketch has a room temperature setting. Taking five readings and using the middle one (the median) removes odd readings from soft or angled surfaces. The sensor works best between about 2 cm and 400 cm and struggles with soft cloth or surfaces angled more than about 15 degrees.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);   // try 0x3F if the screen stays blank

const uint8_t TRIG = 9, ECHO = 10, BTN = 2;
const float AIR_TEMP_C = 25.0;        // change to the room temperature for better accuracy

float speedCmPerUs() {                // speed of sound in cm per microsecond
  return (331.3 + 0.606 * AIR_TEMP_C) / 10000.0;
}

long pingOnce() {                     // echo time in microseconds, 0 if no echo
  digitalWrite(TRIG, LOW);  delayMicroseconds(3);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  return pulseIn(ECHO, HIGH, 30000UL);
}

float medianDistanceCm() {
  long t[5];
  for (uint8_t i = 0; i < 5; i++) { t[i] = pingOnce(); delay(40); }
  for (uint8_t i = 0; i < 4; i++)          // small sort
    for (uint8_t j = i + 1; j < 5; j++)
      if (t[j] < t[i]) { long x = t[i]; t[i] = t[j]; t[j] = x; }
  if (t[2] == 0) return -1;                // no echo
  return t[2] * speedCmPerUs() / 2.0;      // out and back, so halve it
}

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BTN, INPUT_PULLUP);
  lcd.init(); lcd.backlight();
  lcd.print("Press to measure");
}

void loop() {
  if (digitalRead(BTN) == LOW) {
    float cm = medianDistanceCm();
    lcd.clear();
    if (cm < 2 || cm > 400) {
      lcd.print("Out of range");
    } else {
      lcd.print(cm, 1);          lcd.print(" cm");
      lcd.setCursor(0, 1);
      lcd.print(cm / 2.54, 1);   lcd.print(" in");
    }
    while (digitalRead(BTN) == LOW) delay(10);   // hold the reading
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Wall at 50 cm49 to 51 cm
Wall at 200 cm197 to 203 cm
Aim at open spaceOut of range
Hold a pillow in frontReading is poor or out of range
Hold the buttonReading stays on screen

If it does not work

Always out of range

Check Trig and Echo are not swapped and the sensor has 5 V.

Reading jumps around

Hold the sensor steady and aim at a flat, hard surface.

Screen blank

Try address 0x3F, or turn the contrast screw on the back.

All readings a few percent off

Set AIR_TEMP_C to the real air temperature.

Minimum distance wrong

The sensor cannot measure under about 2 cm.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ultrasonic tape measure?

Arduino Uno R3, HC-SR04 Ultrasonic Sensor, 16x2 LCD with I2C backpack (PCF8574), Tactile push button 6 mm, 9 V battery snap with DC jack, 9 V battery, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the ultrasonic tape measure take to build?

About 90 minutes for a beginner maker, in 8 steps.

How do I know the ultrasonic tape measure works?

Wall at 50 cm: 49 to 51 cm. Wall at 200 cm: 197 to 203 cm. Aim at open space: Out of range.

Always out of range: what should I check?

Check Trig and Echo are not swapped and the sensor has 5 V.

Reading jumps around: what should I check?

Hold the sensor steady and aim at a flat, hard surface.

Screen blank: what should I check?

Try address 0x3F, or turn the contrast screw on the back.

All readings a few percent off: what should I check?

Set AIR_TEMP_C to the real air temperature.