Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
In this project the implementation of code for “Measuring distance of an object using ultrasonic distance sensor (28015 Ping) and also smoothen the sensor data using moving average filter”.
COMPONENTS USED:-
- ARDUINO
- ULTRASONIC DISTANCE SENSOR
- LCD 16*2
- POTENTIOMETER
- RESISTOR
- CONNECTING WIRE
CIRCUIT DIAGRAM:

CODE:
#include
#define trigger 18
#define echo 19
LiquidCrystal lcd(2,3,4,5,6,7);
float time=0,distance=0;
void setup()
{
lcd.begin(16,2);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
lcd.print(" Ultra sonic");
lcd.setCursor(0,1);
lcd.print("Distance Meter");
delay(2000);
lcd.clear();
lcd.print(" Circuit Digest");
delay(2000);
}
void loop()
{
lcd.clear();
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*340/20000;
lcd.clear();
lcd.print("Distance:");
lcd.print(distance);
lcd.print("cm");
lcd.setCursor(0,1);
lcd.print("Distance:");
lcd.print(distance/100);
lcd.print("m");
delay(1000);
}
OUTPUT:

DESCRIPTION:
The analog pins A4 and A5 in master arduino and slave arduino are used for I2C communication.
In master arduino, the digital pin 2 is connected to RS of LCD. Digital pin 3 connected to E of LCD. The Digital pins 4, 5, 6, 7 are connected to DB1, DB2, DB3, DB4 of LCD. Input supply 5v and Gnd are connected to the LCD from arduino. In LCD, the anode is connected through an 1k ohm resistor and the cathode is connected to the Gnd.
The digital pin 2 is used to send and receive data from the sig pin from ultrasonic distance sensor (28015 Ping). The output is the distance in ultrasonic distance sensor is adjusted to around 45.22cm or 0.45m and the value is displayed in the LCD connected to master.