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

Arduino Uno R3 Distance Measurement and Display System with Ultrasonic Sensor and LCD

View and Edit Project In Cirkit DesignerImage of Arduino Uno R3 Distance Measurement and Display System with Ultrasonic Sensor and LCD

Circuit Documentation

Summary

This circuit is designed to interface an Arduino Uno R3 with a variety of components including an LCD screen, an ultrasonic sensor, pushbuttons, LEDs, buzzers, and resistors. The primary function of this circuit is to measure distance using the HC-SR04 Ultrasonic Sensor and display the measured distance on the LCD screen. The user can interact with the system using a pushbutton to toggle the display mode between centimeters and meters. Visual feedback is provided by yellow and green LEDs, and an audible alert is generated by a buzzer.

Component List

LCD screen 16x2 I2C

HC-SR04 Ultrasonic Sensor

Pushbutton

Resistor (10k Ohms)

LED: Two Pin (yellow)

LED: Two Pin (green)

Resistor (200 Ohms)

Buzzer

Arduino Uno R3

Wiring Details

LCD screen 16x2 I2C

HC-SR04 Ultrasonic Sensor

Pushbutton

Resistor (10k Ohms)

LED: Two Pin (yellow)

LED: Two Pin (green)

Buzzer

Documented Code

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

const int trigPin = 10;
const int echoPin = 11;

long duration;
int distance;
float distanceInMeters;

LiquidCrystal_I2C LCD (0x27, 16, 2);

int buttonPin = 2; // Button pin in Arduino
int press = 0, totalPress = 0;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
 
  LCD.begin(16, 2);
  LCD.backlight();
  
  pinMode(buttonPin, INPUT);
}

void loop() {
    distance = ultrasonicSensor();
    press = digitalRead(buttonPin);

    if (press == HIGH){
        totalPress += 1;
    
        delay(300);
    }

    if (totalPress == 1){
        LCD.clear();
        LCD.setCursor(4, 0);
        LCD.print("MODE: CM");
        LCD.setCursor(4, 1);
        LCD.print("Distance: " + String(distance) + " cm");
    }
    if (totalPress == 2){
        distanceInMeters = distance / 100.00;
        LCD.clear();
        LCD.setCursor(4, 0);
        LCD.print("MODE: M");
        LCD.setCursor(4, 1);
        LCD.print("Distance: " + String(distanceInMeters) + " m");
    }
    if (totalPress == 3){
        LCD.clear();
        totalPress = 0;
    }
}

int ultrasonicSensor(){
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);

    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);

    distance = duration * 0.034 / 2;

    return distance;
}

This code is designed to run on the Arduino Uno R3 and handles the interaction with the ultrasonic sensor, LCD display, and pushbutton. It includes functions for measuring distance, displaying the distance on the LCD, and toggling the display mode with the pushbutton.