Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Ultrasonic Distance Sensor with I2C LCD Display and Buzzer Alert

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Ultrasonic Distance Sensor with I2C LCD Display and Buzzer Alert

Circuit Documentation

Summary

This circuit is designed to measure the distance of an object using an HC-SR04 Ultrasonic Distance Sensor and display the distance on a 16x2 I2C LCD. Additionally, a Piezo Speaker is used to provide an audible warning if the object is too close. The entire system is controlled by an Arduino UNO, powered by a 9V battery through a Rocker Switch.

Component List

Arduino UNO

16x2 I2C LCD

HC-SR04 Ultrasonic Distance Sensor

Piezo Speaker

9V Battery

Rocker Switch (SPST)

Wiring Details

Arduino UNO

16x2 I2C LCD

HC-SR04 Ultrasonic Distance Sensor

Piezo Speaker

9V Battery

Rocker Switch (SPST)

Code Documentation

Arduino UNO Code

#define trigPin 9
#define echoPin 7
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27 // Define I2C Address, change the current address to your I2C address

#define BACKLIGHT_PIN   3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
int buzzerPin = 3;

LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);

  lcd.begin(20, 4);
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home();
}

void loop() {
  int distance = getDistance();

  lcd.clear(); 
  lcd.setCursor(0, 0);
  if (distance >= 10){
    lcd.print("Object Distance:");
    lcd.setCursor(0, 1);
    lcd.print(distance);
    lcd.print(" cm");
  }
  
  else if (distance < 10){
    // Buzzer and warning message for too close distance
    if (millis() % 1000 < 500) { // Toggle buzzer on/off every second
      digitalWrite(buzzerPin, HIGH);
    } else {
      digitalWrite(buzzerPin, LOW);
    }

    lcd.setCursor(0, 0);
    lcd.print(" !! WARNING !!"); // Show warning message
    lcd.setCursor(0, 1);
    lcd.print("Close to ");
    lcd.print(distance);
    lcd.print(" cm");
  }

  delay(500); 
}

int getDistance() {
  long sum = 0;
  for (int i = 0; i < 5; i++) { // Take multiple readings
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(100);
    digitalWrite(trigPin, LOW);
    sum += pulseIn(echoPin, HIGH);
  }
  return ((sum / 5) / 2) / 29.1; // Average and convert to cm
}

void displayMessage(const char* line1, const char* line2, bool clear) {
  if (clear) lcd.clear(); // Clear only when necessary
  lcd.setCursor(0, 2);
  lcd.print(line1);
  lcd.setCursor(0, 3);
  lcd.print(line2);
}

This code initializes the pins and libraries required for the ultrasonic sensor and the LCD. It continuously measures the distance of an object and displays it on the LCD. If the object is too close, it triggers the Piezo Speaker to emit a warning sound and displays a warning message on the LCD.