ElectronLab Project Lab
Sensor Tech Arduino Core Proximity Alarm

Ultrasonic Distance Meter with 16x2 LCD & Proximity Warning Buzzer

Construct a digital acoustic ruler capable of measuring distances from 2 cm to 400 cm with millimeter resolution, displaying live distance metrics on an LCD, and increasing beeping frequency as targets approach.

Reading Time 8 Minutes
Difficulty Level Beginner
Hardware Platform Arduino Uno & HC-SR04

1. Aim of the Project

Project Objective

The goal is to design an ultrasonic rangefinder that calculates exact target distances using high-frequency sonic time-of-flight measurements, presents live readings in both centimeters (cm) and inches (in) on an I2C LCD, and triggers a parking-sensor style variable rate audio warning system.

2. Interactive 3D Assembly & Circuit Wiring Model

Rotate the 3D model 360 degrees, zoom in/out, disassemble/explode parts to inspect individual hardware layers, toggle realistic circuit wires, and click any component to inspect its engineering specifications.

3D Hardware Simulation & Assembly Lab
Left Click + Drag: Rotate Orbit (360°)
Right Click + Drag: Pan Scene
Scroll: Zoom In / Out
Click Component: Inspect Hardware Details
Hovered Component

3. Laboratory Video Masterclass Tutorial

Step-by-step video walkthrough covering breadboard circuit assembly, wiring verification, and testing. Enrolled students and instructors can access video streaming below.

2. Learning Outcomes

Microsecond Time Measurement

Learn how to generate 10-microsecond trigger pulses and capture microsecond-accurate echo responses with pulseIn().

Dynamic Proximity Feedback

Implement automotive reverse parking sensor algorithms where beep frequency accelerates as objects draw nearer.

3. Required Components

Component Name Quantity Specification Interface
Arduino Uno 1 Microcontroller Board USB / 5V
HC-SR04 Ultrasonic Sensor 1 40 kHz Ultrasonic Transceiver (2-400cm) Pins D9, D10
16x2 I2C LCD Display 1 Alphanumeric Display (Address 0x27) I2C (A4, A5)
5V Piezo Buzzer 1 Proximity Alert Sounder Pin D8

4. Circuit Connections

Sensor Pin Arduino Pin Function
HC-SR04 Trig Pin D9 Trigger acoustic transmission burst
HC-SR04 Echo Pin D10 Acoustic reflection duration input
Piezo Buzzer (+) Pin D8 Audio warning tone signal
LCD SDA / SCL Pins A4, A5 I2C Data & Clock

5. Complete Arduino Source Code

ultrasonic_distance_meter.ino
/*
 * Project: Ultrasonic Distance Meter with LCD & Buzzer Alarm
 * Author: ElectronLab STEM Curriculum
 */

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

const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int BUZZER_PIN = 8;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Distance Meter");
  lcd.setCursor(0, 1);
  lcd.print("Calibrating...");
  delay(1500);
  lcd.clear();
}

void loop() {
  // Clear trigger pin
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  // Send 10us HIGH pulse
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Read the echo pin pulse duration in microseconds
  long duration = pulseIn(ECHO_PIN, HIGH);

  // Calculate distance in cm and inches (Speed of sound = 0.0343 cm/us)
  float distanceCm = duration * 0.0343 / 2.0;
  float distanceIn = distanceCm / 2.54;

  if (distanceCm >= 400 || distanceCm <= 2) {
    lcd.setCursor(0, 0);
    lcd.print("Out of Range   ");
    lcd.setCursor(0, 1);
    lcd.print("Target > 400cm ");
    digitalWrite(BUZZER_PIN, LOW);
  } else {
    // Display readings
    lcd.setCursor(0, 0);
    lcd.print("Dist: ");
    lcd.print(distanceCm, 1);
    lcd.print(" cm    ");

    lcd.setCursor(0, 1);
    lcd.print("Dist: ");
    lcd.print(distanceIn, 1);
    lcd.print(" in    ");

    // Proximity alert: Beep faster when object is close (< 30 cm)
    if (distanceCm < 30) {
      int beepDelay = map((int)distanceCm, 2, 30, 50, 400);
      digitalWrite(BUZZER_PIN, HIGH);
      delay(50);
      digitalWrite(BUZZER_PIN, LOW);
      delay(beepDelay);
    } else {
      digitalWrite(BUZZER_PIN, LOW);
      delay(200);
    }
  }
}

6. Working Principle

The HC-SR04 ultrasonic transducer transmits eight 40 kHz sonic bursts. When these high-frequency acoustic waves strike an obstacle, they bounce back to the receiver microphone. By computing the round-trip flight time against the ambient speed of sound in air (343 m/s), distance is determined with high accuracy.

7. Troubleshooting Guide

Soft Fabric Objects Not Detected

Soft surfaces absorb acoustic waves rather than reflecting them. Use flat, rigid surfaces like wood, plastic, cardboard, or acrylic for testing.