Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
Overview of the Project:
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”.
Goals:
- Measuring the distance in terms of inches of an object using Ultrasonic Distance Sensor (28015 Ping).
- The sensor data is smoothened by using Moving average filter technique to eliminate the high frequency variations from the sensor data.
Key Highlights of the Project:
- Measuring the distance travelled by the wave to the object and gets bounced back from the object to sensor.
- Smoothening the sensor data by moving average filter technique, the filter has a window of certain size. That window determines that how many data the filter can take.
Deliverables of this project:
- The distance travelled by a wave to the object for which we want to measure the distance using Ultrasonic Distance Sensor (28015 Ping).
- Eliminate the high frequency jitters from the sensor data by Moving Average filter to smoothen the sensor data.
Expected Outcomes:
The students will get the Knowledge on Ultrasonic sensor (28015 Ping) to measure the distance travelled by wave to the object, the hardware connection of 2 Arduino’s connected to the sensor and smoothening the sensor data by removing high frequency variations using Moving Average Filter Technique
Project - 2
Solution
Circuit Diagram

Code
Master code-
//Master
#include
#include //header files
LiquidCrystal lcd(13,12,6,5,4,3); //pin connected to LCD
void setup()
{
lcd.begin(16,2);
Wire.begin();
}//setup
void loop()
{
Wire.requestFrom(9,1); //requesting data from 9 address
while(Wire.available()) //checking availability of data
{
float data=Wire.read(); //reading data available in bus
lcd.print("distance=");
lcd.print(data); //printing distance in inches
lcd.setCursor(0,1);
lcd.print(" inches");
delay(5000);
lcd.clear(); //clearing lcd screen
}
}
Slave code-
//slave
#include
uint8_t distance=0;
float time=0;
float y=0;
float t[4];
float x=0;
//Variaxble initialization
void setup()
{
Wire.begin(9);
// lcd.begin(16,2);
Serial.begin(9600);
Wire.onRequest(doit);//When master will requst the data it will call doit function
}
void loop()
{
}
void doit() //called function
{
for(int i=0;i<5;i++)
{
y=0;
x=ultrasonic_sensor(); //calling function
t[i]=x;
}//reading 5 data
for(int i=0;i<5;i++)
{
y=t[i]+y;
}//adding 5 data
/* sound velocity
v=340 m/sec
v=d/t
d=v*t;"t in sec"
d=340*t; "t in second,m=meter"
d=340*t*100; "t in second ,m=cm"
*/
y=y/(5); //microseconds
time=y/2; //divided by 2 due sound travel twice distace
time=time*0.000001; //time in second
//d=340*t*100
distance=time*340*39.3701; //tim in sec and distance in inches
Wire.write(distance);//sending data to master
Serial.println(distance);
}
int ultrasonic_sensor(void) //called function
{
pinMode(2,OUTPUT); //making pin 2 as output pin
digitalWrite(2,HIGH);
delayMicroseconds(2);
digitalWrite(2,LOW);
pinMode(2,INPUT); //making pin 2 as input pin
float t=pulseIn(2,HIGH); //reading time to get wave back to the sensor
pinMode(2,OUTPUT);
return t;
/* After triggering the ultrasonic sensor we are calculating the time
which will be taken by sound pulse to get reflected back and reach to the
sensor so here sound is travelling twice distance*/
}
Output
Distance 30.2 Inch

Distance 95.9 inch

Link
https://www.tinkercad.com/things/hRaFcIOjGkn-super-gaaris/editel?sharecode=ajZxh-MGO_FhfgtYoWN27-F7i6IfvK-ytzuuzCgTFVw