Challenge

Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter

Here is the Arduino code to measure the distance using the 28015 Ping Ultrasonic Distance Sensor and smoothen the data using a Moving Average Filter.

---

Arduino Code:

#define TRIGGER_PIN 9 // Pin to trigger the ultrasonic sensor

#define ECHO_PIN 10 // Pin to receive echo signal

#define WINDOW_SIZE 5 // Moving Average Window Size

float distanceArray[WINDOW_SIZE]; // Array to store recent distance readings

int index = 0; // Index to track latest reading

void setup() {

Serial.begin(9600); // Initialize Serial Monitor

pinMode(TRIGGER_PIN, OUTPUT);

pinMode(ECHO_PIN, INPUT);

}

void loop() {

float distance = measureDistance(); // Get raw distance from sensor

float smoothDistance = movingAverageFilter(distance); // Apply moving average filter

Serial.print("Raw Distance: ");

Serial.print(distance);

Serial.print(" inches | Smoothed Distance: ");

Serial.print(smoothDistance);

Serial.println(" inches");

delay(500); // Wait before next measurement

}

// Function to measure distance using Ultrasonic Sensor

float measureDistance() {

digitalWrite(TRIGGER_PIN, LOW);

delayMicroseconds(2);

digitalWrite(TRIGGER_PIN, HIGH);

delayMicroseconds(10);

digitalWrite(TRIGGER_PIN, LOW);

long duration = pulseIn(ECHO_PIN, HIGH); // Get echo time

float distance = duration * 0.0133 / 2; // Convert to inches (Speed of Sound = 0.0133 inch/μs)

return distance;

}

// Function to apply Moving Average Filter

float movingAverageFilter(float newDistance) {

distanceArray[index] = newDistance; // Store new distance reading

index = (index + 1) % WINDOW_SIZE; // Move index circularly

float sum = 0;

for (int i = 0; i < WINDOW_SIZE; i++) {

sum += distanceArray[i];

}

return sum / WINDOW_SIZE; // Return smoothed value

}

---

Explanation of Code:

1. Measuring Distance:

The 28015 Ping Ultrasonic Sensor sends a sound pulse.

The time for the pulse to bounce back determines the distance.

Formula used:

\text{Distance (inches)} = \frac{\text{Echo Time} \times 0.0133}{2}

2. Smoothing Data with Moving Average Filter:

Maintains a fixed window (default: 5 readings).

Averages the last 5 readings to reduce noise.

This eliminates high-frequency jitters in the sensor output.

---

How to Test:

1. Connect the 28015 Ping Sensor:

Trigger Pin → Arduino Pin 9

Echo Pin → Arduino Pin 10

VCC → 5V, GND → GND

2. Upload the Code to Arduino.

3. Open the Serial Monitor (9600 baud) to see raw and smoothed distance readings.

4. Move an object in front of the sensor and observe the smoothened distance output.

---

Expected Outcomes:

Students will learn how to interface an ultrasonic sensor with Arduino.

They will understand sensor noise reduction using a moving average filter.

The smoothed output provides accurate distanc

e measurements for real-world applications.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.