Challenge

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 smoothen the sensor data using moving average filter

code:

#include <LiquidCrystal.h>
LiquidCrystal lcd = LiquidCrystal(10,9,8,7,6,5); // Create an LCD object. Parameters: (RS, E, D4, D5, D6, D7):
const int trigPin = 12;
const int echoPin = 11;
float time, distance;
void setup()
{
lcd.begin(16, 2); // Specify the LCD's number of columns and rows. Change to (20, 4) for a 20x4 LCD
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);

}

void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

time = pulseIn(echoPin, HIGH);

distance = (time*.0343)/2;

// For Serial Monitor
Serial.print("Distance:CM ");
Serial.println(distance);

// For LCD Display
lcd.setCursor(0,0);
lcd.print("Distance in CM");
lcd.setCursor(0,1);
lcd.print(distance);

}

THE PROJECT LINK BELOW MENTIONED HERE:

https://www.tinkercad.com/things/2H9BUBj9ZLU-measure-with-distance/editel?sharecode=HwJ82urV4IvqzekS7sDlGx3hf81JbgfJWMtjGuXSuNU

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.