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

Arduino UNO Distance Measurement and Display with I2C LCD and LED Indicators

View and Edit Project In Cirkit DesignerImage of Arduino UNO Distance Measurement and Display with I2C LCD and LED Indicators

Circuit Documentation

Summary

The circuit in question is designed to measure distance using an HC-SR04 ultrasonic sensor and display the measured distance on an I2C LCD 16x2 screen. It also includes a series of LEDs that light up at different distance thresholds. The Arduino UNO serves as the central microcontroller unit, controlling the sensor, the display, and the LEDs. The LEDs are connected through individual resistors to limit the current and protect the LEDs.

Component List

Arduino UNO

I2C LCD 16x2 Screen

HC-SR04 Ultrasonic Sensor

LEDs (Red, Green, Yellow, White, Blue)

Resistors (220 Ohms)

Wiring Details

Arduino UNO

I2C LCD 16x2 Screen

HC-SR04 Ultrasonic Sensor

LEDs (Red, Green, Yellow, White, Blue)

Resistors (220 Ohms)

Documented Code

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

LiquidCrystal_I2C lcd(0x27, 16, 2);
//ULTRASONIC SENSOR
#define trigPin A0 // Sensor Trig pin
#define echoPin A1 // Sensor Echo pin
//LED
#define led1 2 // LED for 5 inch
#define led2 3 // LED for 7 inch
#define led3 4 // LED for 9 inch
#define led4 5 // LED for 11 inch
#define led5 6 // LED for 12 inch

long distanceInch;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);

  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   WAIT LANG");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);  // Set LCD 
  lcd.print("Distance:"); // Print Message on First Row
  lcd.setCursor(0, 1);
  lcd.print("Distance:");
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  distanceInch = duration * 0.0133 / 2;

  lcd.setCursor(9, 0);   
  lcd.print("                ");  
  lcd.setCursor(9, 0);    
  lcd.print(distance); // Print measured distance
  lcd.print(" cm");  // Print your units.
  lcd.setCursor(9, 1);  
  lcd.print("                "); // Print blanks to clear the row
  lcd.setCursor(9, 1);   
  lcd.print(distanceInch);
  lcd.print(" inch");  // Print your units. 

  // Control LEDs based on distance
  digitalWrite(led1, distanceInch <= 8 ? HIGH : LOW);
  digitalWrite(led2, distanceInch <= 16 ? HIGH : LOW);
  digitalWrite(led3, distanceInch <= 24 ? HIGH : LOW);
  digitalWrite(led4, distanceInch <= 32 ? HIGH : LOW);
  digitalWrite(led5, distanceInch <= 40 ? HIGH : LOW);

  delay(500); // Delay for half a second
}

This code initializes the LCD and ultrasonic sensor in the setup() function. In the loop() function, it triggers the ultrasonic sensor to measure distance, calculates the distance in centimeters and inches, and displays the results on the LCD. It also controls the LEDs based on the measured distance, lighting them up at different thresholds.