Home/ Projects/ Ultrasonic tape measure
Project 6 of 16 Sensing Intermediate

Ultrasonic tape measure

Send a pulse, time the echo, divide by two, convert to centimetres. Unlike an analog sensor this is a timing measurement, so you meet pulse timing and microsecond resolution. One safety point: the HC-SR04 is a 5V part and its echo pin outputs 5V, while both boards expect 3.3V — use a divider on that line rather than wiring it straight.

ESP32 & ESP8266 IoT Automation Kit
One of 16 projects in the ESP32 & ESP8266 IoT Automation Kit — the parts below are from that kit.

What this project uses

Every item is in the kit — nothing extra to buy.

Everything for this project

ESP32 & ESP8266 IoT Automation Kit

All 24 parts in one box, with this project and 15 more to build from them.

AED 180.00 See the kit
Need only these parts? Order them loose · from AED 110.74
Used in this project tick what you still need
Also needed untick if you already have them
Selected parts AED 110.74

One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. Backorder parts ship once they arrive — the kit above has every part boxed today. Delivery is free on orders AED 150.00+.

Sketch for this project

Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.

06 Ultrasonic tape measure Arduino C++
/*
  06 — Ultrasonic tape measure
  Board: ESP32.  Parts: HC-SR04, LCD 1602 (I2C backpack), 3 x 1 kΩ from the resistor pack.
  Library: "LiquidCrystal I2C" (Frank de Brabander).

  Wiring:
    HC-SR04: VCC -> VIN (5 V), GND -> GND, TRIG -> GPIO 5
             ECHO -> 1 kΩ -> GPIO 18, and from GPIO 18 two 1 kΩ in series -> GND
             (ECHO outputs 5 V; the divider gives the ESP32 3.3 V. Do not skip it.)
    LCD:     VCC -> VIN (5 V), GND -> GND, SDA -> GPIO 21, SCL -> GPIO 22

  Distance = (echo time in microseconds) / 58. Sound goes there and back, so
  the time is halved, and at ~343 m/s that works out to 58 us per centimetre.
*/

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

const int TRIG_PIN = 5;
const int ECHO_PIN = 18;
LiquidCrystal_I2C lcd(0x27, 16, 2);

float readCm() {
  digitalWrite(TRIG_PIN, LOW);  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  unsigned long us = pulseIn(ECHO_PIN, HIGH, 30000UL);   // 30 ms timeout ~ 5 m
  if (us == 0) return -1;                                 // nothing in range
  return us / 58.0;
}

void setup() {
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  lcd.init();
  lcd.backlight();
  lcd.print("Tape measure");
}

void loop() {
  // Average 5 readings so one bad echo does not make the number jump.
  float sum = 0; int n = 0;
  for (int i = 0; i < 5; i++) {
    float d = readCm();
    if (d > 0) { sum += d; n++; }
    delay(30);
  }
  lcd.setCursor(0, 1);
  if (n == 0) {
    lcd.print("out of range    ");
    Serial.println("out of range");
  } else {
    float cm = sum / n;
    lcd.print(cm, 1); lcd.print(" cm         ");
    Serial.print(cm, 1); Serial.println(" cm");
  }
  delay(150);
}
ECHO is 5 V: 1 kΩ + (1 kΩ + 1 kΩ) divider from the resistor pack. Do not skip it.

More from this kit

All projects →

Root
Online — usually replies instantly
Catalog assistant: verify final stock/price on the product page before checkout.