Ultrasonic Sensor & LCD Shield

Components Used:

  • 1x Arduino Uno R3
  • 1x Ultrasonic Sensor
  • 1x LCD Shield

First Send Trigger Signal HIGH for 10 µSec and then Sense the Echo Pulse. Sensed HIGH time duration on Echo Pulse gives the time required for Sound Waves to travel from Ultrasonic Sensor to Object and back from Object to Ultrasonic Sensor.

Ultrasonic-Sensor-DiagramUltrasonic-Sensor-Equasionsultrasonic sensor LCD Shield

 

Program:

/*
Program by: ANKIT Modi
* Ultrasonic Sensor HC-SR04 and Arduino Tutoril
*/
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
const int trigPin = 5;
const int echoPin = 6;
long duration;
int distanceCm, distanceInch;
void setup() {
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distanceInch);
lcd.print(" inch");
delay(10);
}
Advertisements
%d bloggers like this: