Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
1.
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.
const int windowSize = 10;
int trigPin = 7;
int echoPin = 8;
int sensorValue;
int sensorReadings[windowSize];
int sensorIndex = 0;
int sensorTotal = 0;
long duration;
int distance;
void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration *0.0342)/ 2;
// Add the distance to the sensor readings array
sensorTotal = sensorTotal - sensorReadings[sensorIndex];
sensorReadings[sensorIndex] = distance;
sensorTotal = sensorTotal + sensorReadings[sensorIndex];
sensorIndex = sensorIndex + 1;
// If we've reached the end of the array, reset the index
if (sensorIndex >= windowSize)
{
sensorIndex = 0;
}
// Calculate the average distance
distance = sensorTotal / windowSize;
//print it to the serial monitor
Serial.println(distance);
delay(200);
}