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.
#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.