3.Distance Tracker with LCD

Distance Tracker with LCD

   Creating an ultrasonic sensor project can be a great way to learn about distance measurement and object detection using Arduino. Here’s a simple project outline that demonstrates how to use an ultrasonic sensor, such as the HC-SR04, with an Arduino to measure distances and display the readings on an LCD.

 Components Needed:

- Arduino board (e.g., Arduino Uno)
- HC-SR04 ultrasonic sensor
- 16x2 LCD (with I2C backpack for easier connection)
- Breadboard and jumper wires
- Resistor (1kΩ) for the ultrasonic sensor (optional, depending on your setup)

Circuit Connections:

1. **HC-SR04 Connections:**

   - VCC to 5V on Arduino
   - GND to GND on Arduino
   - TRIG to a digital pin (e.g., pin 9)
   - ECHO to another digital pin (e.g., pin 10)

2. **LCD Connections:**

   - Connect the SDA and SCL pins of the LCD to the corresponding pins on the Arduino (A4 for SDA, A5 for SCL on Arduino Uno).
   - VCC to 5V and GND to GND on Arduino.

 Code:

Here’s a simple code example that measures the distance using the ultrasonic sensor and displays it on the LCD.

cpp

#include <LiquidCrystal_I2C.h>

#define TRIG_PIN 9
#define ECHO_PIN 10

LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust address according to your LCD

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  
  lcd.begin();
  lcd.backlight();
  lcd.print("Distance: ");
}

void loop() {
  long duration, distance;
  
  // Send a 10us pulse to trigger
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  // Read the echo time
  duration = pulseIn(ECHO_PIN, HIGH);
  
  // Calculate distance in cm
  distance = duration * 0.034 / 2; // Speed of sound is 0.034 cm/us

  // Display distance on LCD
  lcd.setCursor(0, 1);
  lcd.print("                "); // Clear previous data
  lcd.setCursor(0, 1);
  lcd.print(distance);
  lcd.print(" cm");
  
  delay(500); // Update every half second
}
```

How It Works:

1. **Triggering the Sensor:** The `TRIG_PIN` sends a short pulse to the ultrasonic sensor, which emits a sound wave.
2. **Measuring Echo Time:** The `ECHO_PIN` reads the time it takes for the sound wave to return after hitting an object.
3. **Calculating Distance:** The time is converted to distance using the speed of sound formula.
4. **Displaying on LCD:** The distance is printed on the LCD in centimeters.

 Additional Ideas:

- **LED Indicator:** Add an LED that lights up when an object is detected within a certain distance.
- **Buzzer Alert:** Use a buzzer to alert when an object is too close.
- **Data Logging:** Store distance data in an SD card for later analysis.

Comments

Popular posts from this blog

esp8266

1.Using an LDR (Light Dependent Resistor) with Arduino to Measure Light Intensity