Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
#include
#define TrigPin 8
#define EchoPin 7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
long time;
int distance;
void setup()
{
lcd.begin(16,2);
pinMode(TrigPin,OUTPUT);
pinMode(EchoPin,INPUT);
lcd.print(" Ultra sonic");
}
void loop()
{
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
delayMicroseconds(5);
time = pulseIn(EchoPin, HIGH);
distance = time*0.034/2;
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print("cm");
}
Components used
16*2 lcd display
HC-SR04 ultrasonic sensor
Arduino uno
About ultrasonic sensor :
The ultrasonic sensor consists of Transmitter and Receiver modules. Transmitter part ejects the pulse out and the receiver part receives the pulse. If an obstacle is placed before the sensor, the transmitted pulse ejected strike the obstacle and reflects back. The reflected pulse is received by the receiver part. The time between transmission and reception is calculated and this data is processed to calculate distance.
speed of the sound is 340m/s or we can say 0.034cm/microsec
so to calculate the distance
d = time*0.034/2 we are dividing it by 2 as the sound travelled covers both direction(transmitted and received)
