Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
Topic: The distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter.
Aim 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.
Hardware Requirements:
- Arduino UNO board
- USB cable connecter for Arduino UNO
- 16X2 LCD Screen
- I2C Controller
- Ultra Sonic Sensor HC-SR04
- Jumper wires Male/Female
Software requirements:
- Arduino software(Arduino IDE)
Explanation:
The ultrasonic sensor emits a high-frequency sound pulse and calculates the distance depending upon the time taken by the echo signal to travel back after reflecting from the desired target. The speed of sound is 341 meters per second in air. After the distance is calculated, it will be displayed on the LCD display.
Circuit Connection:

Pin Configuration of ULTRASONIC SENSOR:

16X2 LCD SCREEN PIN DETAILS:
- LCD PIN 1-GND
- LCD PIN 2-VCC
- LCD PIN 3-VEE
- LCD PIN 4-RS (Register Select)
- LCD PIN 5-RW (Read/Write)
- LCD PIN 6-E (Enable)
- LCD PIN 7 to PIN 14-Data Bits
- LCD PIN 15-LED Positive
- LCD PIN 16-LED Negative
CODING:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define trigPin 9 //Sensor trig pin connected to Arduino pin 9
#define echoPin 8 //Sensor echo pin connected to Arduino pin 8
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Skill Lync");
delay(1500);
lcd.setCursor(0,0); //Set LCD cursor to column 0, row 0
lcd.print("Target Distance:"); //Print the Message on First Row
}
void loop()
{
long duration, distance; // Initialize duration and distance
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
lcd.setCursor(0,1); //Set cursor to first column of second row
lcd.print(" "); //Print blanks to clear the row
lcd.setCursor(0,1); //Set Cursor again to first column of second row
lcd.print(distance); //Print measured distance
lcd.print(" cm"); //Print units.
delay(300); //pause to let things settle
}
OUTPUT:
Experimental setup:

Experimental setup-switched ON:

:
Measured distance-6cm:

Measured distance-9cm:
