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: 

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.

Code:

const int trigPin = 2;  // Trig pin of the Ultrasonic Sensor

const int echoPin = 3;  // Echo pin of the Ultrasonic Sensor

const int numReadings = 5;  // Number of readings to average

int readings[numReadings];   // Array to store sensor readings

int index = 0;  // Index for storing readings in the array

long total = 0;  // Variable to store the sum of readings

int averageDistance = 0;  // Variable to store the smoothed distance

void setup() {

  Serial.begin(9600);

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  // Initialize the array with initial readings

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

    readings[i] = 0;

  }

}

void loop() {

  // Trigger the ultrasonic sensor to send a pulse

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  // Measure the duration of the pulse from the echo pin

  long duration = pulseIn(echoPin, HIGH);

  // Calculate the distance in centimeters

  int distance = duration / 58;

  // Update the moving average filter

  total = total - readings[index];

  readings[index] = distance;

  total = total + readings[index];

  index = (index + 1) % numReadings;

  // Calculate the average distance

  averageDistance = total / numReadings;

  // Print the raw and smoothed distance to the Serial Monitor

  Serial.print("Raw Distance: ");

  Serial.print(distance);

  Serial.print(" cm, Smoothed Distance: ");

  Serial.println(averageDistance);

  // Delay for stability

  delay(100);

}

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.