Challenge

Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter

AIM:

Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter.

MATERIALS REQUIRED:

1.Arduino Uno R3.

2.BreadBoard.

3.LCD display (16*2).

4.Ultrasonic sensor

5.Resistor(1kohm).

6.Jumping Wires.

CODE:

#include<LiquidCrystal.h>//Library for I2C communications
LiquidCrystal lcd(1,2,3,4,5,6);//Define LCD module pins

const int pingPin=7;//Ping pin is connected to digital pin 7
long duration;//Duration is declared
int distanceCm,distanceInch;//Declaration of distance in cm and inch
float sum=0;//Sum =0
float avg=0;//AVerage =0
int i;//Integer i is declared

void setup()
{
lcd.begin(16,2);//Initialize the LCD display
lcd.setCursor(0,0);//Set the cursor to first line
lcd.print("Ultrasonic");//Display the cricuit Digest in LCD
lcd.setCursor(0,1);//Set the Cursor to the Second line
lcd.print("Sensor");//Print the given in second line
delay(1000);//Delay is 1 second
lcd.clear();//Clear the LCD
}

void loop()
{

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin,OUTPUT);
digitalWrite(pingPin,LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(5);
digitalWrite(pingPin,LOW);

pinMode(pingPin,INPUT);//Read the signal from the sensor
duration=pulseIn(pingPin,HIGH);//Using pulseIn,Returns the length of the signal in microseconds
distanceCm=duration*0.034/2;//Calculate the distance in Cm,Speed of sound =343m/s,=0.034
distanceInch=duration*0.0135/2;Calculate the distance in Inches,343m=135 inches/1million =0.0135

for(i=0;i<=5;i++)
{
sum=sum+distanceInch;//to calculate the sum of 5
}
avg=(sum/5.0);//To calculate the average
lcd.setCursor(0,0);//Set the cursor to first line
lcd.print("Distance:");//Prints the distance in LCD
lcd.print(distanceCm);//Prints the distance value in Cm
lcd.print("Cm");//Prints the Cm
delay(1000);//Delay is 1 sec
lcd.setCursor(0,1);//Sets the cursor to second line
lcd.print("Distance:");//Prints the distance
lcd.print(distanceInch);//Prints the value of distance in inches
lcd.print("Inch");//Prints inch

Serial.begin(9600);//Start the serial Monitor
Serial.print("avg:");//Prints the average
Serial.println(avg);//prints the avg value
Serial.print("Distance:");//Prints distance in serial monitor
Serial.println(distanceCm);//Prints the value in cm
Serial.print("Distance:");//Prints distance
Serial.println(distanceInch);//Prints the value in inches

delay(1000);//Delay is 1 second
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.