Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

How To Make Distance measurement Device using Ultrasonic sensor and LCD I2C display - Simple circuit

  Distance measurement Device using Ultrasonic sensor and LCD I2C display


Here’s an Arduino code that displays the distance readings from an ultrasonic sensor (HC-SR04) on an I2C LCD display in both inches and centimeters.

Components Needed:

  • Arduino Uno
  • Ultrasonic Sensor (HC-SR04)
  • I2C LCD Display (16x2)
  • Jumper wires
  • Breadboard

Wiring:

  • Ultrasonic Sensor:
    • VCC to 5V
    • GND to GND
    • Trig to Pin 9
    • Echo to Pin 8
  • I2C LCD Display:
    • VCC to 5V
    • GND to GND
    • SDA to A4
    • SCL to A5

Video Link: - Click Here For Video

Distance measurement Device using Ultrasonic sensor and LCD I2C display simple Circuit: -



Distance measurement Device using Ultrasonic sensor and LCD I2C display Arduino Programming: -

Arduino IDE Program

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

// Initialize the LCD with I2C address 0x27 (can be 0x3F for some displays), 16 columns and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Define pin connections for Ultrasonic Sensor
const int trigPin = 9;
const int echoPin = 8;

// Variables to store the distance
long duration;
float distanceCm;
float distanceInch;

void setup() {
  // Initialize the serial communication
  Serial.begin(9600);

  // Initialize the LCD
  lcd.init();
  lcd.backlight();

  // Set the trigPin as an OUTPUT and echoPin as an INPUT
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Print initial message on the LCD
  lcd.setCursor(0, 0);
  lcd.print("Distance:");
}

void loop() {
  // Clear the trigPin by setting it LOW
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Trigger the sensor by setting the trigPin HIGH for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echoPin and calculate the duration it takes for the signal to return
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance in centimeters and inches
  distanceCm = (duration * 0.0343) / 2;
  distanceInch = distanceCm / 2.54;

  // Display the distance on the LCD
  lcd.setCursor(0, 1);  // Set cursor to second row
  lcd.print("CM: ");
  lcd.print(distanceCm);
  lcd.print("  ");

  lcd.setCursor(8, 1);  // Set cursor to second row, 8th column
  lcd.print("In: ");
  lcd.print(distanceInch);

  // Print the distance on the Serial Monitor
  Serial.print("Distance in CM: ");
  Serial.print(distanceCm);
  Serial.print(" | Distance in Inches: ");
  Serial.println(distanceInch);

  // Small delay before taking the next reading
  delay(500);
}


-----------------------------------------------------------------------------------------------------------------------------

Explanation:

  1. Ultrasonic Sensor: The HC-SR04 sensor sends an ultrasonic pulse and listens for its echo. Based on the time it takes for the pulse to return, the distance is calculated.
  2. Distance Calculation:
    • Distance in centimeters = (duration * 0.0343) / 2
    • Distance in inches = distanceCm / 2.54
  3. LCD Display: The distance is shown on the I2C LCD in both centimeters and inches.

Libraries Required:

  • LiquidCrystal_I2C: This is required for the I2C LCD. You can install it through the Arduino Library Manager.

Let me know if you'd like any adjustments!

Share, Support, and Subscribe!!! 1. PRO KAM EXPLAINED 2. Knowledge KAM 3. PRO KAM Follow us on 1. Facebook 2. Instagram 3. Pinterest 4. Blogspot 5 Twitter If you have any other ideas to make me design, you can describe them in the comment section and if I can, I will make a designing video on it. So I am waiting #prokam #pro #IDM #Solutions #Hack

Post a Comment

0 Comments

Ad Code

Responsive Advertisement