Project 2 - Measuring the distance of an object using ultrasonic sensor 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.
Components Used
1.Arduino Uno or Pro Mini
2.Ultrasonic sensor Module
3.16x2 LCD
6.9 volt battery
7.Connecting wires
connections of circuit diagram
1.The circuit diagram for arduino and ultrasonic sensor is shown above to measure the distance.
2.In circuit connections Ultrasonic sensor module’s “trigger” and “echo” pins are directly connected to pin 18(A4) and 19(A5) of arduino.
3.A 16x2 LCD is connected with arduino in 4-bit mode.Control pin RS, RW and En are directly connected to arduino pin 2, GND and 3.And data pin D4-D7 is connected to 4, 5, 6 and 7 of arduino.
First of all we need to trigger the ultrasonic sensor module to transmit signal by using arduino and then wait for receive ECHO. Arduino reads the time between triggering and Received ECHO. We know that speed of sound is around 340m/s. so we can calculate distance by using given formula:
Distance= (travel time/2) * speed of sound
Where speed of sound around 340m per second
code
//Header to use LCD functions
#include<LiquidCrystal>
#define trigger 18
#define echo 19
//pins connected to LCD
LiquidCrystal lcd(2,3,4,5,6,7);
float time=0,distance=0;
void setup()
{
Serial.begin(9600);
//To begin LCD display
lcd.begin(16,2);
//declaring pin2 as output
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
//To display in LCD
lcd.print(" Ultra sonic");
lcd.setCursor(0,1);
//To display in LCD
lcd.print("Measuring distance");
delay(2000);
lcd.clear();
//To display in LCD
lcd.print(" Using ultra sonic sensor");
//delay 2 sec
delay(2000);
}
void loop()
{
lcd.clear();
//sensor is triggered by pin 2 as output
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
time=pulseIn(echo,HIGH);
//calculating distance using formula.
distance=time*340/20000;
lcd.clear();
//to display in LCD
lcd.print("Distance:");
lcd.print(distance);
//to display in LCD
lcd.print("cm");
lcd.setCursor(0,1);
lcd.print("Distance:");
//To display in LCD
lcd.print(distance/2.54);
lcd.print("inches");
//delay 1 sec
delay(1000);
Serial.print("Distance:");
Serial.println(distance);
Serial.print("inches");
delay(1000);
}
code for Moving Average Filter
#define IN_PIN A0
#define WINDOW_SIZE 5
int INDEX = 0;
int VALUE = 0;
int SUM = 0;
int READINGS[WINDOW_SIZE];
int AVERAGED = 0;
void setup() {
pinMode(IN_PIN, INPUT);
Serial.begin(9600);
}
void loop() {
SUM = SUM - READINGS[INDEX]; // Remove the oldest entry from the sum
VALUE = analogRead(IN_PIN); // Read the next sensor value
READINGS[INDEX] = VALUE; // Add the newest reading to the window
SUM = SUM + VALUE; // Add the newest reading to the sum
INDEX = (INDEX+1) % WINDOW_SIZE; // Increment the index, and wrap to 0 if it exceeds the window size
AVERAGED = SUM / WINDOW_SIZE; // Divide the sum of the window by the window size for the result
Serial.print(VALUE);
Serial.print(",");
Serial.println(AVERAGED);
delay(25);
}