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:
This project involves implementing code for measuring the distance of an object using an ultrasonic distance sensor (28015 Ping) and smoothing the sensor data using a moving average filter.
Goals:
- Measure the distance in inches of an object using the Ultrasonic Distance Sensor (28015 Ping).
- Smooth the sensor data using the Moving Average Filter technique to eliminate high-frequency variations.
Key Highlights of the Project:
- Measure the distance traveled by the wave to the object and back to the sensor after bouncing off the object.
- Implement a Moving Average Filter technique to smoothen the sensor data. The filter utilizes a window of a certain size to determine the number of data points it considers.
Deliverables of this project:
- Distance traveled by a wave to the object, measured using the Ultrasonic Distance Sensor (28015 Ping).
- Elimination of high-frequency variations from the sensor data using the Moving Average filter to smoothen the data.
Expected Outcomes:
Students will gain knowledge on:
- Utilizing Ultrasonic sensor (28015 Ping) for measuring the distance traveled by a wave to the object.
- Hardware connection of two Arduino boards connected to the sensor.
- Smoothing sensor data by removing high-frequency variations using the Moving Average Filter Technique.
CODE:
#define TRIGGER_PIN 7 #define ECHO_PIN 6 #define MAX_DISTANCE 200 #define MOVING_AVERAGE_WINDOW_SIZE 5 NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); float movingAverage(float newValue) { static float window[MOVING_AVERAGE_WINDOW_SIZE]; static int index = 0; static float total = 0; total -= window[index]; total += newValue; window[index] = newValue; index = (index + 1) % MOVING_AVERAGE_WINDOW_SIZE; return total / MOVING_AVERAGE_WINDOW_SIZE; } void setup() { Serial.begin(9600); } void loop() { int distance = sonar.ping_in(); float smoothedDistance = movingAverage(distance); Serial.print("Smoothed Distance: "); Serial.print(smoothedDistance); Serial.println(" inches"); delay(100); }