Challenge

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

#include

LiquidCrystal lcd(12,10,9,8,7,6);

int trigerpin=5;

float echo=4 ;

float speedOfSound=346; //meter per sec

float pingtime;

float dist;

void setup()

{

lcd.begin(16,2);

lcd.setCursor(3,0);

lcd.print("DISTANCE=");

pinMode(trigerpin,OUTPUT);

pinMode(echo,INPUT);

}

void loop()

{

digitalWrite(trigerpin,LOW);

delayMicroseconds(2000);

digitalWrite(trigerpin,HIGH);

delayMicroseconds(15);

digitalWrite(trigerpin,LOW);

delayMicroseconds(10);

pingtime=pulseIn(echo,HIGH);

dist=pingtime*speedOfSound/20000;

lcd.setCursor(5,1);

lcd.print(dist);

delay(1000);

}

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.