Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
AIM:In this project the implementation of code for “Measuring distance of an object using ultrasonic distance sensor (28015 Ping) and also smoothen the sensor data using moving average filter”
Detail view about ULtrasonic sensor:

Our PING)))™ ultrasonic sensor provides an easy method of distance measurement. This sensor is perfect for any number of applications that require you to perform measurements between moving or stationary objects.
Interfacing to a microcontroller is a snap. A single I/O pin is used to trigger an ultrasonic burst (well above human hearing) and then “listen” for the echo return pulse. The sensor measures the time required for the echo return, and returns this value to the microcontroller as a variable-width pulse via the same I/O pin.
Features:
- Provides precise, non-contact distance measurements within a 3 cm to 3 m range
- Ultrasonic measurements work in any lighting condition, making this a good choice to supplement infrared object detectors
- Simple pulse in/pulse out communication requires just one I/O pin
- Burst indicator LED shows measurement in progress
- 3-pin header makes it easy to connect to a development board, directly or with an extension cable, no soldering required
Theory:
This sketch reads a PING ultrasonic rangefinder and returns the distance to the closest object in range.
To do this, it sends a pulse to the sensor to initiate a reading, then listens for a pulse to return.
The length of the returning pulse is proportional to the distance of the object from the sensor.
The circuit:
* +V connection of the PING))) attached to +5V
* GND connection attached to ground
* SIG connection attached to digital pin 7
Schematic diagram:

CODE:
int inches = 0;
int cm = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
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);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(7, 7);
// convert to inches by dividing by 2.54
inches = (cm / 2.54);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.println("cm");
delay(100); // Wait for 100 millisecond(s)
}
OUTPUT:

This sketch reads a PING ultrasonic rangefinder and returns the distance to the closest object in range.
the range detects the in/cm

Output Graph:

CONCLUSION:
The PING))) sensor detects objects by emitting a short ultrasonic burst and then "listening" for the echo. Under control of a host microcontroller (trigger pulse), the sensor emits a short 40 kHz (ultrasonic) burst. This burst travels through the air, hits an object and then bounces back to the sensor. The PING))) sensor provides an output pulse to the host that will terminate when the echo is detected, hence the width of this pulse corresponds to the distance to the target.
Application Ideas:
- Security systems
- Interactive animated exhibits
- Parking assistant systems
- Robotic navigation