Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
Working of a PING Sensor:
PING sensor consists of a chip 40KHz signal generator, an ultrasonic speaker and an ultrasonic microphone. Speaker ultrasonic converts the signals into 40 KHz ultrasonic sound while the microphone is used to detect the reflection suaranya.Sensor PING detect the distance of an object by emitting ultrasonic waves (40 kHz) for tBURST (200 μs) and then detect the reflection. PING sensors emit ultrasonic waves in accordance with the control of the microcontroller control (pulse trigger with Tout min. 2 μs).
This ultrasonic wave through the air at a speed of 344 meters per second, about the object and bounces back to the sensor. PING issued a high output pulse at pin SIG after emit ultrasonic waves and the reflected waves detected after the PING will create a low output on pin SIG. High pulse width (TIN) will be in accordance with the long travel time of ultrasonic waves to 2x measuring the distance to the object.
Then the measured distance is :
S = (tIN x V) ÷ 2
Where :
S = distance between the ultrasonic sensor with the object being detected
V = Propagation of ultrasonic waves in air (344 m / s)
TIN = Difference in time transmitting and receiving the reflected waves.
Moving Average Filter:
The most common filter in DSP is the moving average filter (or moving mean filter), which slides a window of size N over a raw signal, computes the average over that window, and uses this average as the smoothed value.
MA=X1+X2+…+X/N
MA=(X1+X2+…+Xn/N)
This filter is a type of low-pass filter because it smooths out (eliminates) the high frequency oscillations in the signal.
You can control the filter’s performance by tweaking the size of the sliding window.
Arduino Code:
// C++ code
int inches = 0;
#define SAMPLESIZE 10 // Filter Size Or Window Size
const int UL_PIN = 7;
int INDEX = 0;
int VALUE = 0;
int SUM = 0;
int avg[SAMPLESIZE];
int AVERAGE = 0;
int cm = 0;
void setup()
{
//pinMode(UL_PIN, INPUT); //ultrasonic sensor as input pin
Serial.begin(9600);
}
void loop()
{
pinMode(UL_PIN, OUTPUT);
digitalWrite(UL_PIN, LOW);
delayMicroseconds(2);
digitalWrite(UL_PIN, HIGH);
delayMicroseconds(5);
digitalWrite(UL_PIN, LOW);
//Moving average algorithm
SUM = SUM - avg[INDEX]; // subtract previous values
VALUE = analogRead(UL_PIN);// Read from ultrasonic sensor
SUM = SUM+VALUE;
INDEX = (INDEX+1)%SAMPLESIZE;//Increment the index and wrap it to zero if sample size is exceeded
AVERAGE = SUM / SAMPLESIZE; //Moving average formula
Serial.print(VALUE);
Serial.print(",");
//Serial.print("Smoothed:");
Serial.println(AVERAGE);
delay(5);
}
Conclusion: Distance is measured using ultrasonic PING sensor and sesnor waveform is smoothened using DSP low pass filter which eliminates the higher frequency.