Home › DIY Projects › LCD Distance Display

Distance Meter with 16x2 I2C LCD, HC-SR04 and Arduino

Show the distance on a real screen with a live bar graph and a STOP! warning. No computer needed once it is running.

  • Level: Beginner
  • Time: 35 minutes
  • Board: Arduino Uno
  • No soldering
▶ Copy code + open simulatorGet the code

Watch it in action

Subscribe to ZEENAIX on YouTube for more projects

What you will build

This upgrades the classic ultrasonic distance meter with a 16x2 LCD screen. Line 1 shows the distance, like Dist: 34 cm. Line 2 shows a bar graph that grows as the object moves away, plus a status word: FAR (50 cm or more), NEAR (20 to 50 cm) or STOP! (under 20 cm).

The LCD has an I2C backpack, so it needs only four wires: power, ground, SDA and SCL. You will learn how I2C works, how to install and use an Arduino library, and how to format numbers neatly on a small screen with snprintf().

Components required

Arduino Uno R3 compatible boardArduino Uno R3 × 1The brain. Any Uno or Nano works.
View in store →
HC-SR04 ultrasonic distance sensorHC-SR04 ultrasonic sensor × 1Measures about 2 cm to 4 m.
View in store →
16x2 character LCD with I2C module and blue backlight16x2 LCD with I2C module × 1Only 4 wires thanks to the I2C backpack.
View in store →
Breadboard × 1Half or full size.
View in store →
Jumper wires (male to female) × 8For the LCD and sensor pins.
View in store →

Circuit diagram and connections

Tap any part in the diagram to see what it does and where it connects.

Arduino Uno9 10A4 A55V GND HC-SR04 Dist: 34 cm█ NEARVCC GND SDA SCL Red = 5V · Black = GND · Orange = Trig · Blue = Echo · Green = SDA (A4) · Purple = SCL (A5)

Tap a part to learn more.

Part pinArduino pinWire colour (suggested)
HC-SR04 VCC5VRed
HC-SR04 TrigD9Orange
HC-SR04 EchoD10Blue
HC-SR04 GNDGNDBlack
LCD I2C VCC5VRed
LCD I2C GNDGNDBlack
LCD I2C SDAA4Green
LCD I2C SCLA5Purple

Arduino code

Copy this sketch into the simulator or the Arduino IDE. It uses the LiquidCrystal I2C library: in the Arduino IDE open Sketch › Include Library › Manage Libraries, search "LiquidCrystal I2C" and install the one by Frank de Brabander. We wrote and tested the code ourselves; you are free to use and change it.

/*
  ZEENAIX DIY Project: Distance Meter with 16x2 I2C LCD and Arduino
  https://zeenaix.com/diy-projects/lcd-distance-display-arduino/

  Parts: Arduino Uno, HC-SR04 ultrasonic sensor,
         16x2 LCD with I2C module (PCF8574, address 0x27), jumper wires.
  Library: LiquidCrystal I2C (Library Manager: "LiquidCrystal I2C" by Frank de Brabander)

  Wiring:
    HC-SR04  VCC -> 5V   GND -> GND   TRIG -> pin 9   ECHO -> pin 10
    LCD I2C  VCC -> 5V   GND -> GND   SDA  -> A4      SCL  -> A5

  What it does:
    Shows the distance in cm on line 1 and a bar graph plus a status word
    (FAR / NEAR / STOP!) on line 2. Updates 4 times a second.
*/

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

const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int MAX_CM   = 200;   // bar graph full scale

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

long 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 = about 5 m
  if (us == 0) return -1;                                // nothing in range
  return (long)(us / 58);                                // microseconds to cm
}

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("ZEENAIX");
  lcd.setCursor(0, 1);
  lcd.print("Distance meter");
  Serial.println("ZEENAIX LCD distance meter ready");
  delay(1000);
}

void loop() {
  long cm = readCm();

  // Line 1: "Dist:  123 cm   "
  char line1[17];
  if (cm < 0) snprintf(line1, sizeof(line1), "Dist:  --- cm   ");
  else        snprintf(line1, sizeof(line1), "Dist: %4ld cm   ", cm);

  // Line 2: bar graph (10 blocks) + status word
  char line2[17];
  int blocks = (cm < 0) ? 10 : (int)min(10L, cm * 10 / MAX_CM);
  for (int i = 0; i < 10; i++) line2[i] = (i < blocks) ? (char)0xFF : ' ';
  const char *status = (cm >= 0 && cm < 20) ? "STOP!" : (cm >= 0 && cm < 50) ? " NEAR" : "  FAR";
  snprintf(line2 + 10, 7, " %s", status);

  lcd.setCursor(0, 0);
  lcd.print(line1);
  lcd.setCursor(0, 1);
  lcd.print(line2);

  Serial.print(line1);
  Serial.println(status);
  delay(250);
}

Step-by-step instructions

A. Try it in the free simulator (5 minutes)

  1. Open the ZEENAIX Arduino Simulator and choose Blank editor with the Arduino Uno board.
  2. Delete the starter code, then paste the sketch from the code section.
  3. Click Libraries, search LiquidCrystal I2C and click + Add to project (the one by Frank de Brabander). Without it you get LiquidCrystal_I2C.h: No such file.
  4. Tap + Add and place an HC-SR04 ultrasonic sensor and a 16x2 LCD (I2C).
  5. Draw the wires exactly as in the connection table above. SDA goes to A4 and SCL to A5.
  6. Press the green ▶ run button. The screen shows ZEENAIX for a second, then the live distance.
  7. Tap the sensor and move its distance slider. Watch the number, the bar graph and the FAR / NEAR / STOP! word change.

B. Build it for real (30 minutes)

  1. Install the LiquidCrystal I2C library in the Arduino IDE (Sketch › Include Library › Manage Libraries).
  2. Connect the LCD backpack with male-to-female jumper wires: VCC to 5V, GND to GND, SDA to A4, SCL to A5.
  3. Connect the HC-SR04: VCC to 5V, GND to GND, Trig to pin 9, Echo to pin 10. Use the breadboard rails to share 5V and GND.
  4. Upload the code (Tools › Board › Arduino Uno). The backlight should come on and show ZEENAIX.
  5. If the screen is lit but empty, slowly turn the blue contrast screw on the back of the LCD until the letters appear.
  6. Still nothing? Your LCD may use address 0x3F. Change 0x27 to 0x3F in the code and upload again.

Try it here: what the screen will show

Same rules as the sketch: the bar has 10 blocks for 0 to 200 cm, under 20 cm shows STOP!, 20 to 50 cm shows NEAR.

How it works

The HC-SR04 part works exactly like our ultrasonic distance meter: the echo time divided by 58 gives centimetres. The new part is the screen.

A 16x2 LCD normally needs 6 or more data wires. The I2C backpack (a PCF8574 chip) turns that into just two signal wires: SDA carries the data and SCL carries the clock. Each device on the I2C bus has an address; most of these LCDs use 0x27. The library sends the characters for you, so lcd.print("Hi") is all you need.

The bar graph uses character 0xFF, a solid block built into the LCD. cm × 10 ÷ 200 gives how many of the 10 blocks to fill, so 100 cm fills 5 blocks. snprintf() pads the number to 4 places so the text does not jump around when the distance changes from 9 to 10.

Tested: we compiled this sketch for the Arduino Uno with the LiquidCrystal I2C and Wire libraries (zero warnings) and ran it in an AVR simulation with a model of the I2C LCD that decodes every byte sent to address 0x27. With the object at 150, 35 and 12 cm, the screen showed Dist: 149 cm with 7 blocks and FAR, Dist: 34 cm with 1 block and NEAR, and Dist: 11 cm with STOP!, as designed.

Real-life uses of a distance display

Garage parking helper

Mount it on the garage wall facing the car. The driver sees the exact distance and STOP! on the screen.

Water tank level display

Put the sensor on the tank lid facing down and show how full the tank is on an LCD in the kitchen.

Height measuring station

Fix the sensor above a doorway facing down: height = doorway height minus the measured distance. Great for school events.

Queue distance reminder

Show a friendly message when a person stands too close to a counter.

Science lab instrument

Use it to measure the motion of a toy car or a falling ball and note the readings.

Troubleshooting

  • Backlight on but no text: turn the blue contrast screw on the backpack. This fixes most "blank screen" problems.
  • Still blank or random blocks: change the address in the code from 0x27 to 0x3F. You can also run an I2C scanner sketch to find the address.
  • Error "LiquidCrystal_I2C.h: No such file": install the LiquidCrystal I2C library from the Library Manager.
  • Distance always --- : check Trig on pin 9, Echo on pin 10 and that the sensor has 5V.

Frequently asked questions

What are SDA and SCL on the Arduino Uno?

On the Uno, SDA is A4 and SCL is A5. Newer Uno boards also have separate SDA and SCL pins near the USB port; they are the same signals.

Can I use a 20x4 LCD instead?

Yes. Change LiquidCrystal_I2C lcd(0x27, 16, 2) to (0x27, 20, 4) and you get two more lines to show extra information.

Can I show inches instead of cm?

Yes. Divide the echo time by 148 instead of 58 to get inches, and change the text from cm to in.

Do I need to buy parts to try this?

No. You can run the whole project in the free ZEENAIX simulator first, then buy only the parts you need when you are ready to build it.

Build it for real

Get the Arduino Uno, HC-SR04 sensor, 16x2 I2C LCD, breadboard and male-to-female jumper wires from the ZEENAIX store, with delivery across India.

▶ Copy code + open simulatorMore DIY projects