Projects / Ultrasonic distance display

Ultrasonic distance display

Beginner35 minGuided buildReviewed
Arduino UnoD9D10A4A5GNDHC-SR04Distance 45 cmecho 45×58 µs1602 LCD

Tap a part to see what it does.

Tap the parts to see what each one does. A teaching simulation, not a circuit simulator.

Build it step by step

0 of 4 done
  1. UNO

    Mount the sensor

    Eyes facing forward, nothing in front within 2 cm.

  2. UNO

    Wire Trig and Echo

    Trig D9, Echo D10, 5V, GND.

  3. UNOHello1602

    Add the LCD

    SDA A4, SCL A5.

  4. UNO

    Upload

    Move your hand towards the sensor.

In short

How do you build an ultrasonic distance display?

An HC-SR04 measures distance and a 1602 LCD shows it in centimetres with a simple bar, updated ten times a second. You need Arduino Uno R3, HC-SR04 Ultrasonic Sensor, 1602 Character LCD and Breadboard 830 Tie Points. It takes 4 steps and about 35 minutes.

Level
Beginner
Time
About 35 min
Parts
4

Parts list

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

Wiring

FromToWhy
HC-SR04 VCC / GNDUno 5V / GNDPower
HC-SR04 TrigUno D9Start pulse
HC-SR04 EchoUno D10Echo time
LCD SDA / SCLUno A4 / A5I2C
LCD VCC / GND5V / GNDPower

Code

Library: LiquidCrystal I2C. If the LCD shows nothing, try address 0x3F and turn the contrast trimmer on the backpack.

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 <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int TRIG = 9, ECHO = 10;

long readCm() {
  digitalWrite(TRIG, LOW); delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW);
  long t = pulseIn(ECHO, HIGH, 30000);   // 0 if no echo within about 5 m
  return t ? t / 58 : -1;
}

void setup() { pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); lcd.init(); lcd.backlight(); }

void loop() {
  long cm = readCm();
  lcd.setCursor(0, 0);
  if (cm < 0 || cm > 400) lcd.print("Out of range    ");
  else { lcd.print("Distance "); lcd.print(cm); lcd.print(" cm    "); }
  lcd.setCursor(0, 1);
  int bars = cm < 0 ? 0 : constrain(map(cm, 0, 200, 16, 0), 0, 16);
  for (int i = 0; i < 16; i++) lcd.print(i < bars ? (char)255 : ' ');
  delay(100);
}

Test checklist

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

CheckExpected
Hand at a ruler's 20 cm markShows 19 to 21 cm
Point at the ceilingSteady reading or "Out of range" beyond about 4 m
Hand very closeBar fills the bottom line

If it does not work

Always "Out of range"

Trig and Echo swapped, or soft surfaces absorbing the sound.

Row of blocks on LCD

Turn the contrast trimmer.

Jumpy readings

Take the median of 3 readings.

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

Ask Lark AI

Common questions

What parts do I need for the ultrasonic distance display?

Arduino Uno R3, HC-SR04 Ultrasonic Sensor, 1602 Character LCD and Breadboard 830 Tie Points. 1602 Character LCD: With the I2C backpack.

How long does the ultrasonic distance display take to build?

About 35 minutes for a beginner maker, in 4 steps.

How do I know the ultrasonic distance display works?

Hand at a ruler's 20 cm mark: Shows 19 to 21 cm. Point at the ceiling: Steady reading or "Out of range" beyond about 4 m. Hand very close: Bar fills the bottom line.

Always "Out of range: what should I check?

Trig and Echo swapped, or soft surfaces absorbing the sound.

Row of blocks on LCD: what should I check?

Turn the contrast trimmer.

Jumpy readings: what should I check?

Take the median of 3 readings.