Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
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.
TINKERCAD LINK FOR THIS PROJECT :-
https://www.tinkercad.com/things/ccxDjTbf6CF-ultrasonic/editel?sharecode=ro6-nxBIYNRlcfnEw6m0LYOEJJtI2-4INfm9jpXX1Nw
Circuit :-

Schematic view :-

Components :-

PROGRAM :-
Master Code :-
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // initialize serial communication
}
void loop()
{
Wire.beginTransmission(8); // transmit to slave device with address 8
Wire.write(0); // sends a byte
Wire.endTransmission(); // stop transmitting
delay(500); // wait for 500ms
}
Slave Code :-
#include <Wire.h>
float inches = 0;
float cm = 0;
float pingTravelTime;
#define SAMPLESIZE 10 // Filter Size Or Window Size
int INDEX = 0;
int VALUE = 0;
int SUM = 0;
int avg[SAMPLESIZE]={0};
int AVERAGE = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT);
// Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // initialize serial communication
}
void loop()
{
// do nothing here
}
// function that executes whenever data is received from master
void receiveEvent(int howMany)
{
while (Wire.available())
{
int val = Wire.read(); // receive byte as an integer
pingTravelTime = readUltrasonicDistance(7, 7);
// measure the ping time in cm
cm = 0.01723 * pingTravelTime;
// convert to inches by dividing by 2.54
inches = (cm / 2.54);
// Moving average algorithm
SUM = SUM - avg[INDEX]; // subtract previous values
VALUE = pingTravelTime; // Read from ultrasonic sensor
SUM = SUM + VALUE;
INDEX = (INDEX + 1) % SAMPLESIZE; // Increment the index and wrap it to zero if sample size is exceeded
AVERAGE = SUM / SAMPLESIZE; // Moving average formula
Serial.print("Travel Time to Target (ms) : ");
Serial.println(pingTravelTime);
Serial.print("Distance to Target (inches) : ");
Serial.println(inches);
Serial.print("Distance to Target (cm) : ");
Serial.println(cm);
Serial.print("\n");
}
}
OUTPUT :-

SERIAL MONITOR OUTPUT :-
