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 the 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 the Moving average filter technique to eliminate the high-frequency variations from the sensor data.

Key Highlights of the Project:

  • Measuring the distance traveled by the wave to the object and getting bounced back from the object to the sensor.
  • Smoothening the sensor data by moving average filter technique, the filter has a window of a certain size. That window determines how much data the filter can take.

Deliverables of this project:

  • The distance traveled 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.

PING))) Ultrasonic Distance Sensor (28015)

This PING)) 28015 ultrasonic sensor has three pins one for power, ground, and signal. The signal pin is connected to the microcontroller it first takes input command from the microcontroller as a trigger to trigger ultrasonic waves then it takes command pulse in to receive echo wave and synthesize this information in terms of time taken by an ultrasonic wave from triggering to echo received following formula and calculation need to be done to get the distance.

distance = speed * time
total_time= time of transmission + time of reflection
distance = speed * total_time/ 2
1sec = 1000000 micro sec.
speed of Ultrasonic waves is 344.6 m/s = 34460cm/s = 34460/1000000 = 0.03446 cm/us
=> distance = speed * total_time/ 2
distance = 0.03446*total_time/ 2 cm

Schematic:

Simulation

Code:

#define triggerPin 5 // transmit ultrasonic waves
#define echoPin 5 // receive ultrasonic waves

int cm = 0;// for distance measurement
long int totaltime;// time taken by wave from transmited till it received back at echoPin

void setup()
{
Serial.begin(9600);
}

void loop()
{
int i;
long int sum = 0;
int avg_value = 0;
// for smoothining the value of moving object distance
for(i=0; i<5; i++)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT); // set pin in input mode to receive reflected wave.

// Reads the echo pin, and returns the sound wave toatl travel time in microseconds
totaltime = pulseIn(echoPin, HIGH);
// measure the ping time in cm
/* distance = speed * time
* total_time= time of transmission + time of refflection
* distance = speed * total_time/ 2
* 1sec = 1000000 micro sec.
* speed of Ultasonic waves is 344.6 m/s = 34460cm/s = 34460/1000000 = 0.03446 cm/us
* => distance = speed * total_time/ 2
* distance = 0.03446*total_time/ 2 cm
*/
cm = totaltime*0.03446/2;
// summing 5 consecutive value of sensor
sum += cm;
delay(50);
}
// calculate average value of distance of moving object
avg_value = sum/5;

Serial.print(avg_value);
Serial.println("cm");
delay(100); // Wait for 100 millisecond(s)
}

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.