Build it step by step
0 of 8 doneInstall the library
Search for LiquidCrystal I2C in the Library Manager.
Wire the sensor
VCC, GND, Trig on D9, Echo on D10.
Wire the LCD and button
SDA and SCL to A4 and A5, button on D2 to GND.
Upload the sketch
Screen shows Press to measure.
Aim at a wall 50 cm away
Use a ruler to set the distance.
Press the button
About 50.0 cm and 19.7 in appear.
Set AIR_TEMP_C to the room temperature
Upload again and compare.
Try 20, 100 and 200 cm
Note the error each time.
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
- Development BoardsBuyArduino Uno R3 × 1
- SensorsBuy
- DisplaysBuy
- SwitchesBuy
- PowerBuy
- BatteriesBuy9 V battery × 1
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| HC-SR04 VCC and GND | Uno 5V and GND | Sensor power |
| HC-SR04 Trig | Uno D9 | Trigger pulse |
| HC-SR04 Echo | Uno D10 | Echo pulse length |
| LCD I2C SDA and SCL | Uno A4 and A5 | Display |
| LCD I2C VCC and GND | Uno 5V and GND | Display power |
| Measure button | Uno D2 to GND | Internal pull-up, pressed reads LOW |
| 9 V battery with DC jack | Uno barrel jack | Optional, 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.
| Check | Expected |
|---|---|
| Wall at 50 cm | 49 to 51 cm |
| Wall at 200 cm | 197 to 203 cm |
| Aim at open space | Out of range |
| Hold a pillow in front | Reading is poor or out of range |
| Hold the button | Reading 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 →
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.
