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
Overview of the Project:
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”.
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.
Using sound waves, the ultrasonic sensor calculates the distance between itself and an object and displays on a LCD. How does an Ultrasonic Distance Sensor work? The Ultrasonic Sensor sends out a high-frequency sound pulse and then times how long it takes for the echo of the sound to reflect back.
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.
Ultrasonic sensors are great tools to measure distance and detect objects without any actual contact with the physical world. It is used in several applications, like in measuring liquid level, checking proximity and even more popularly in automobiles to assist in self-parking or anti-collision systems. Previously we have also build many Ultrasonic Sensor projects like water level detecting, Ultrasonic Radar etc . This is an efficient way to measure small distances precisely. In this project, we have used the HC-SR04 Ultrasonic Sensor with Arduino to determine the distance of an obstacle from the sensor. The basic principle of ultrasonic distance measurement is based on ECHO. When sound waves are transmitted in the environment then waves return back to the origin as ECHO after striking on the obstacle. So we only need to calculate the traveling time of both sounds means outgoing time and returning time to origin after striking on the obstacle. As the speed of the sound is known to us, after some calculation we can calculate the distance. We are going to use this same technique for this Arduino distance measurement project, so let's get started.
If you are completely new check out this Arduino Ultrasonic Sensor tutorial to learn the basics of ultrasonic sensor and its working. That being said lets get started....
Components Used
- Arduino Uno or Pro Mini
- Ultrasonic sensor Module
- 16x2 LCD
- Scale
- Bread board
- 9 volt battery
- Connecting wires
Ultrasonic Sensor Module
There are many types of Arduino distance sensors, but in this project we have used the HC-SR04 to measure distance in range of 2cm-400cm with an accuracy of 3mm. The sensor module consists of an ultrasonic transmitter, receiver and control circuit. The working principle of ultrasonic sensor is as follows:
- High level signal is sent for 10us using Trigger.
- The module sends eight 40 KHz signals automatically, and then detects whether pulse is received or not.
- If the signal is received, then it is through high level. The time of high duration is the time gap between sending and receiving the signal.
Distance= (Time x Speed of Sound in Air (340 m/s))/2

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 for the “Measuring distance of an object using ultrasonic distance sensor (28015 Ping) and also smoothen the sensor data using moving average filter”.
#include<Wire.h>
#include <LiquidCrystal_I2C.h>int range=5,index=0;
float val[5];
float avarage,con=0;
float opPin=9;
float value=0,valueCm=0;
LiquidCrystal_I2C lcd(32,16,2);void setup()
{
lcd.begin(16,2);
lcd.init();
lcd.backlight();
Serial.begin(9600);
lcd.setCursor(0,0);
}void loop()
{
distance();
smothingData(valueCm);
lcd.setCursor(0,0);
lcd.print("Distance:");
lcd.print(valueCm);
lcd.setCursor(0,1);
lcd.print("Avarage:");
lcd.print(avarage);
}void distance()
{
pinMode(opPin,OUTPUT);
digitalWrite(opPin,LOW);
delayMicroseconds(2);
digitalWrite(opPin,HIGH);
delayMicroseconds(10);
digitalWrite(opPin,HIGH);
pinMode(opPin,INPUT);
value=pulseIn(opPin,HIGH);
valueCm=value/29/2;
}void smothingData(float cValue)
{
con=con-val[index];
val[index]=cValue;
con=con+val[index];
index=index+1;
if(index>=range)
{
index=0;
}
avarage=con/range;
}