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 doneMount the sensor
Eyes facing forward, nothing in front within 2 cm.
Wire Trig and Echo
Trig D9, Echo D10, 5V, GND.
Add the LCD
SDA A4, SCL A5.
Upload
Move your hand towards the sensor.
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
- Development BoardsBuyArduino Uno R3 × 1
- SensorsBuy
- DisplaysBuyWith the I2C backpack
- 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 / GND | Uno 5V / GND | Power |
| HC-SR04 Trig | Uno D9 | Start pulse |
| HC-SR04 Echo | Uno D10 | Echo time |
| LCD SDA / SCL | Uno A4 / A5 | I2C |
| LCD VCC / GND | 5V / GND | Power |
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.
| Check | Expected |
|---|---|
| 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 |
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 →
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.
