Design Overview
The Arduino UNO in this circuit has the following pin connections and purposes:
These connections facilitate the operation of the LCD display and the ultrasonic sensor with the Arduino UNO.
Working Principle
The HC-SR04 is a low-cost, simple distance measurement sensor with a range of 2cm to 400cm (about an inch to 13 feet). The sensor consists of two ultrasonic transducers. One is a transmitter that emits ultrasonic sound pulses, while the other is a receiver that listens for reflected waves. It is essentially a SONAR, which is used in submarines to identify underwater things. As a result, the circuit can detect distances in inches and centimeters, and when an item is identified, the distance is shown on the LCD. The distance measuring circuit is appropriate for depth measurement, fluid level measurement, security systems like burglar alarms, home automation, and robotics.
Code (Tested with Arduino Uno)
/* LCD Configuration Guide
The circuit:
* LCD RS pin to digital pin 9
* LCD Enable pin to digital pin 8
* LCD D4 pin to digital pin 7
* LCD D5 pin to digital pin 6
* LCD D6 pin to digital pin 5
* LCD D7 pin to digital pin 4
* LCD R/W pin to ground //write to the register
* LCD VSS pin to ground
* LCD VCC pin to 5V (4.7V – 5.3V)
* Contrast adjustment: LCD VO/VEE pin (pin 3) to variable resistor such as a 1K to 10k potentiometer, ends to +5V and ground
*/
/*
* Sensor HC-SR04 Guide
*
* Minimum Range - 2cm / 1 inch
*Maximum Range - 400cm / 13 feet
Ultrasonic sensor Pins:
VCC: +5VDC
Trig : Trigger (INPUT) - Digital Pin
Echo: Echo (OUTPUT) - Digital Pin
GND: GND
*/
// include the library code:
#include <LiquidCrystal.h>
int trigPin = 3; //Trig Pin
int echoPin = 2; //Echo Pin
float duration, cm, inches;
// Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Clears the LCD screen
lcd.clear();
}
void loop()
{
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
inches = distanceToInches(duration);
cm = distanceToCentimeters(duration);
// set the cursor to column 1, row 1
lcd.setCursor(0, 0);
// Print a message to the LCD.
lcd.print(inches);
lcd.print(" Inches");
// set the cursor to column 1, row 2
lcd.setCursor(0, 1);
// Print a message to the LCD.
lcd.print(cm);
lcd.print(" Centimeter");
//Uncomment this section for serial monitor
/*
Serial.print(inches);
Serial.print(" Inches");
Serial.print(" || ");
Serial.print(cm);
Serial.print(" Centimeter");
Serial.println();
*/
delay(250);
}
float distanceToInches(float duration)
{
float distanceInch;
distanceInch = duration * (0.0133/2); // Use this
// CM to inches formula
//1 cm = (1/2.54)? = 0.3937007874
// inches = cm x 0.3937007874
//float distanceCm;
//distanceCm = duration * (0.034 / 2);
//distanceInch = distanceCm * 0.3937007874;
return distanceInch;
}
float distanceToCentimeters(float duration)
{
float distanceCm;
distanceCm = duration * (0.034/2);
return distanceCm;
}