Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
Aim:-
To Measuring distance of an object using ultrasonic distance sensor (28015 Ping) and also smoothen the sensor data using moving average filter.
Required Hardware:-
1.Arduino Board(Arduino Uno)
2.ultrasonic distance sensor (28015 Ping)
3.Bread Board
4.Potentiometer
5.Resistor(330ohm)
6.Jumper Wires
About ultrasonic distance sensor (28015 Ping)
The Parallax PING))) ultrasonic distance sensor provides precise, non-contact distance measurements from about 2 cm (0.8 inches) to 3 meters (3.3 yards). It is very easy to connect to microcontrollers such as the BASIC Stamp®, SX or Propeller chip, requiring only one I/O pin. The PING))) sensor works by transmitting an ultrasonic (well above human hearing range) burst and providing an output pulse that corresponds to the time required for the burst echo to return to the sensor. By measuring the echo pulse width, the distance to target can easily be calculated.

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.
Code:-
#include
LiquidCrystal lcd = LiquidCrystal(10,9,8,7,6,5); // Create an LCD object. Parameters: (RS, E, D4, D5, D6, D7):
const int pingPin = 12;
long time, distance;
void setup()
{
lcd.begin(16, 2); // Specify the LCD's number of columns and rows. Change to (20, 4) for a 20x4 LCD
Serial.begin(9600);
}
void loop()
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
time = pulseIn(pingPin, HIGH);
Serial.print(time);
distance = (time*.0343)/2;
// For Serial Monitor
Serial.print("Distance:CM ");
Serial.println(distance);
// For LCD Display
lcd.setCursor(0,0);
lcd.print("Distance in CM");
lcd.setCursor(0,1);
lcd.print(distance);
}
