Challenge

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

The project involves implementing code to measure the distance of an object using the ultrasonic distance sensor (28015 Ping). The sensor emits ultrasonic waves that travel to the object and bounce back to the sensor. By measuring the time taken for the waves to travel back and forth, we can calculate the distance of the object. Additionally, we aim to smoothen the sensor data by using a moving average filter, which eliminates high-frequency variations in the data.

Goals

  1. Measure the distance in inches of an object using the 28015 Ping ultrasonic distance sensor.
  2. Apply a moving average filter to smoothen the sensor data and eliminate high-frequency variations.

Key Highlights of the Project

  1. Measurement of Distance: The project focuses on accurately measuring the distance traveled by the ultrasonic waves from the sensor to the object and back.
  2. Moving Average Filter: The sensor data is smoothened using a moving average filter, which involves taking a window of data points and averaging them to eliminate high-frequency jitters.

Deliverables of the Project

  1. Distance Measurement: The project delivers the capability to measure the distance traveled by the ultrasonic waves using the 28015 Ping sensor.
  2. Smoothened Sensor Data: The project provides a method to eliminate high-frequency variations from the sensor data by applying a moving average filter.

Expected Outcomes

  1. Knowledge on Ultrasonic Sensor: Students will gain an understanding of how the 28015 Ping ultrasonic distance sensor works and its application in measuring distances.
  2. Hardware Connection: Students will learn the proper hardware connections required, including connecting two Arduino boards to the ultrasonic sensor.
  3. Smoothened Sensor Data: Students will be able to apply a moving average filter to the sensor data, resulting in smoothened readings by removing high-frequency variations.

code

const int pingPin = 7; // Connect the signal pin of the Ping sensor to digital pin 7
const int numReadings = 10; // Number of readings to consider for the moving average filter

int readings[numReadings]; // Array to store the sensor readings
int index = 0; // Index for storing the readings in the array
long total = 0; // Variable to store the sum of the readings

void setup() {
Serial.begin(9600); // Initialize the serial communication
}

void loop() {
int distance = getDistance(); // Get the distance measurement using the ultrasonic sensor
int smoothedDistance = getSmoothedDistance(distance); // Apply the moving average filter to smoothen the sensor data

// Print the raw and smoothed distance readings
Serial.print("Raw Distance: ");
Serial.print(distance);
Serial.print(" inches, Smoothed Distance: ");
Serial.print(smoothedDistance);
Serial.println(" inches");

delay(500); // Delay between readings
}

int getDistance() {
long duration, inches;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

inches = microsecondsToInches(duration);

return inches;
}

long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}

int getSmoothedDistance(int distance) {
// Subtract the oldest reading from the total
total -= readings[index];

// Store the new reading in the array and add it to the total
readings[index] = distance;
total += readings[index];

// Move to the next index
index++;
if (index >= numReadings) {
index = 0;
}

// Calculate the average of the readings
int average = total / numReadings;

return average;
}

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.