Challenge

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

AIM:

To implement the code for “Measuring distance of an object using ultrasonic distance sensor (28015 Ping) and also smoothen the sensor data using moving average filter”.

COMPONENTS REQUIRED:

  • BREADBOARD
  • ARDUNIO
  • OSCILLOSCOPE
  • FUNCTION GENERATOR
  • CAPACITOR
  • RESISTOR
  • ULTRASONIC DISTANCE SENSOR
  • CONNECTING WIRE

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);

}

Output Screenshot:



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.