Challenge

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

MOVING AVERAGE FILTER:

A moving average filter is a commonly used digital signal processing technique in embedded systems and other applications to smooth or reduce noise in a time-series data stream. It's particularly useful for filtering out high-frequency noise or fluctuations from a signal, making it easier to analyse or work with the underlying trend or information.

In embedded systems, such filters are often implemented in software or hardware to process data from sensors, such as temperature sensors, accelerometers, or audio input. Here's how a moving average filter typically works:

1. Data Collection:  The embedded system continuously collects data from a sensor or another data source.

2. Windowing: The filter uses a fixed-size window or buffer to store a certain number of the most recent data points. The size of this window is a user-defined parameter and determines how many data points are considered in each filtering operation.

3. Data Averaging: At each time step, the filter calculates the average (mean) of the data points within the window. This average becomes the filtered value for that time step.

4. Data Output: The filtered value is then used as the output of the filter, replacing the raw or noisy data point from the sensor for further processing or display.

The key idea behind a moving average filter is that it provides a smoothed representation of the data by averaging out short-term variations or noise while preserving the longer-term trends in the signal. The larger the window size, the smoother the output, but it also introduces more delay in the system's response to changes in the input data.

There are variations of moving average filters, such as:

<!-- [if !supportLists]-->·         <!--[endif]-->Simple Moving Average (SMA): The most basic form of moving average, where each data point within the window contributes equally to the average.

<!-- [if !supportLists]-->·         <!--[endif]-->Weighted Moving Average: In this variation, different weights are assigned to data points within the window, allowing more recent data to have a stronger influence on the filtered result.

<!-- [if !supportLists]-->·         <!--[endif]-->Exponential Moving Average (EMA): This filter gives more weight to recent data points and less weight to older ones, making it responsive to changes while still providing smoothing.

The choice of which moving average filter to use depends on the specific requirements of your embedded system and the characteristics of the data you are processing. It's a common technique for filtering noise from sensor data in applications like IoT devices, control systems, and real-time data analysis.

example waveform with code:

Ultrasonic sensor:

 

 

 

Ultrasonic sensors work by emitting sound waves at a frequency that is too high for humans to hear. The sensor's transducer acts as a microphone to send and receive the ultrasonic sound.

                The sound pulse travels through the air at the speed of sound (about 344 meters per second) until it encounters an object. The sound pulse bounces off the object and returns to the sensor as an "echo".

 The sensor calculates the distance based on the time it takes for the sound to be reflected back. Ultrasonic sensors work similarly to sonar or radar. They evaluate the target's attributes by understanding the received echoes from sound waves.

Ultrasonic sensors have a range of 20cm to 16.5m. However, they cannot provide feedback on where the object is in the sensing area, nor any features such as shape or color. They also do not work in a vacuum.

Environmental conditions can change the precision of the measurements. The speed of sound is dependent on temperature and humidity. However, this tends to be inconsequential in all but the harshest of environments.

HOW IT WORKS:

Ultrasonic sensors calculate distance by measuring the time it takes for the sound to travel to the object and back to the sensor. The formula for calculating distance is:

   Distance = 1/2 * t * c

t: is the time it takes for the ultrasound to travel to the object and return to the sensor.

c: is the speed of sound.

The distance is multiplied by half so that it's only counted once.

Aurduino Code:

#include // I2C LCD library

LiquidCrystal_I2C lcd(0x20, 16, 2); // LCD display address and row and column

int Echo =2;  // pin 2

int trigger = 3; // pin 3

float Wavetravelled_T = 0; // variable for wave travelled time

float distance = 0; // to hold distance

int moving_avg_width = 10; // for moving average window width

float arr_Moving_avg[10]; // variable for hold the elements  

int i = 0;  // for main loop iterations

void setup()

{ 

  // LCD setup

  lcd.init(); 

  lcd.backlight();

  // pin mode configuration

  pinMode(trigger,OUTPUT);

  pinMode(Echo,INPUT);

  Serial.begin(9600);

  lcd.setCursor(0, 0);// cursor at 0,0     

  lcd.print("Object distance:"); // print the "Object distance:"

}

void loop()

{ 

      digitalWrite(trigger, HIGH); // make the pin high

      delayMicroseconds(10); // delay 10 micro seconds

      digitalWrite(trigger, LOW); // make the pin low 

      Wavetravelled_T = pulseIn(Echo,HIGH);

      distance = Wavetravelled_T * 344/20000;//calculating the distance

      arr_Moving_avg[i] = distance; // assigning the distance value to moving average filter array

      i++;

  if(i == moving_avg_width){// After moving average width 

    distance = Moving_Average(arr_Moving_avg,moving_avg_width);

    i = 0; 

  Serial.println(distance);

    lcd.setCursor(0,1);

    lcd.print(distance);

    lcd.print(" cm");

  }

}

//Moving Average filter routine and returning the average value

float Moving_Average(float *arr, int size){

 float moving_avgfltr_val = 0;

  for (int i = 0; i<= size-1; i++){

  moving_avgfltr_val += arr[i];}

  return moving_avgfltr_val/(float)size;

}

CIRCUIT CONNECTION:

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.