Challenge

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

AIM:

To implement  this project the embedded 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.

Main code :

#define SAMPLESIZE 10 // Filter Size Or Window Size
const int UL_PIN = 10;

int INDEX = 0;//By using index, we can access the array elements
int VALUE = 0;// initilaize the value
int SUM = 0;// initilaize the sum
int avg[SAMPLESIZE];//array of sensor data readings and average them.
int AVERAGE = 0;// initilaize the Average
int cm = 0;// initilaize the cm
int inches = 0;//initilaize the inches
void setup()
{
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate

}
void loop()
{
pinMode(UL_PIN, OUTPUT);
digitalWrite(UL_PIN, LOW);
delayMicroseconds(2);
digitalWrite(UL_PIN, HIGH);
delayMicroseconds(5);
digitalWrite(UL_PIN, LOW);
pinMode(UL_PIN, INPUT);
//Moving average algorithm
VALUE = analogRead(UL_PIN)*0.01723;// Read from ultrasonic sensor
SUM = SUM+VALUE;
INDEX = ++INDEX % SAMPLESIZE;//Increment the index 1 and wrap it to zero if sample size is exceeded
SUM = SUM - avg[INDEX]; // subtract previous values
AVERAGE = SUM / SAMPLESIZE; //Moving average formula
Serial.print(VALUE);
Serial.print(",");
Serial.println(AVERAGE);
delay(5);
}

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.