Challenge

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

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

ANSWER:-

AIM:-

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

COMPONENTS REQUIRED:-

  • BREADBOARD
  • ARDUNIO
  • RESISTOR
  • ULTRASONIC DISTANCE SENSOR
  • CONNECTING WIRE
  • POTENTIOMETER
  • LCD 16*2

CODE OF THE PROJECT:-

int trigger_pin = 2;//Connecting trigger pin at pin 2
float echo_pin = 3;//Connecting Echo pin at pin 3
float distance,duration,inches,Centimeter;//Initializaton of parameters
void setup()
{
pinMode(trigger_pin,OUTPUT);//Declaring trigger_pin as output
pinMode(echo_pin,INPUT);//Declaring echo_pin as input
Serial.begin(9600);//Initializing Serial Monitor
}
void loop()
{
digitalWrite(trigger_pin, LOW); //Clearing trigger pin
delayMicroseconds(2);
digitalWrite(trigger_pin, HIGH); //Making trigger pin as high for 10microseconds
delayMicroseconds(10);
digitalWrite(trigger_pin, LOW);//Making low as the trigger pin
long duration = pulseIn(echo_pin, HIGH);//Measuring duration from echopin
Serial.print("Duration in Microseconds: ");
Serial.println(duration);
inches = microsecondsToInches(duration);//Finding the distance in inches
Serial.print("Distance in Inches: " );
Serial.print(inches);
Serial.println( "inches");

delay(10);
static float average_value = -1.0 ;//Declaration of average_value
if (average_value == -1.0) // special case for first reading.
{
average_value = inches ;
}
//+= adds the value of the right operand to a variable and assigns the result to the
//variable.
average_value += 0.1 * (inches - average_value) ; // 0.1 for approx 10 sample
averate, 0.01 for 100 sample etc.
Serial.print("Average value after filteration: ");
Serial.println(average_value);
delay(10);
}
long microsecondsToInches(long microseconds)//To convert the measured distance
into inches
{
Centimeter = ((microseconds / 29)/2);//Calculating the value in microseconds
Serial.print("Distance Centimeter: ");//Printing distance in centimeter
Serial.print(Centimeter);
Serial.println(" cm");
inches = Centimeter * 0.393701;//Converting centimeter into inches
return inches;// Formula to find distance in inches
}

OUTPUT:-

it was showing the distance of an object in the LCD.

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.