ULTRASONIC
SENSOR:
#include<LiquidCrystal.h>
LiquidCrystal LCD(12,11,5,4,3,2);
int trigPin = 7; // Trigger Pin of
Ultrasonic Sensor
long echoPin = 6;// Echo Pin of Ultrasonic
Sensor
double t=0,h=0,hp=0;
void setup()
{
Serial.begin(9600);
pinMode(trigPin,
OUTPUT);
pinMode(echoPin, INPUT);
LCD.begin(16,2);
//LCD.print("Sensor");
//
Starting Serial Terminal
}
void loop()
{
digitalWrite(trigPin, LOW);//clears the trigger pin
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);// Sets the trigPin i.e digital pin 7 on
HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(trigPin, LOW);//clears the trigger pin
t=
pulseIn(echoPin, HIGH);// Reads the echoPin, returns the sound wave travel time
in microseconds
h=t*340/20000;// Calculating the distance
hp=22.5-h;//hp is total height from the base of the tank to ultrasonic
sensor minus the distance detected by the sensor from the water surface
LCD.setCursor(0,0);
LCD.print(" ");
LCD.setCursor(0,0);
LCD.print("distance=");
LCD.print(hp);
Serial.print("distance=");
Serial.print(hp);
Serial.print("cm\n");
//
Prints the distance on the Serial Monitor
LCD.print("cm\n");
delay(1000);
}