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”.

Objective:

  • 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.

Circuit Diagram:

Code:

//For Master Arduino
#include<LiquidCrystal.h>    // Header to use LCD functions
#include<Wire.h>          // Header to use I2C communication
LiquidCrystal lcd(2,3,4,5,6,7); //pins connected to LCD
int distance;         //variable to print the data from slave


void setup()
{
  Serial.begin(9600);  //Begin Serial Comm. at buat rate 9600
  Wire.begin();        //To begin I2C communication
  lcd.begin(16,2);        //To begin LCD display
  lcd.clear();
}

void loop()
{
  Wire.requestFrom(8,4);    // 8 is address 4 byte
  distance=Wire.read();     //To print character by character
  lcd.setCursor(0,0);  //To move the cursor to the first line
  lcd.print("Distance: ");
  lcd.setCursor(0,1); //To move the cursor to the second line
  lcd.print(distance);  //To print the received message from slave
  lcd.print(" inches");      //To display in LCD
  Serial.print("Distance="); //To display in serial monitor
  Serial.print(distance);
  Serial.println("Inches");
  delay(50);    //delay 
  Wire.end();   //To end the I2C transmission
}
// For Slave Arduino
#include<Wire.h>                         // Header to use I2C communication

int signal = 2;                                   //Assigning pin2 as sig for readabilty
int duration, inches;                //Declaring as integer type
static int avg;                               //Declaring avg as static int type
void setup()
{
Serial.begin(9600);
Wire.begin(8);                             //To begin I2C communication with address 8
Wire.onRequest(distance);            //Calling function from master
}

void loop()
{
delay(100);
}

void distance()                             //Called function
{
pinMode(signal, OUTPUT);                 //Declaring pin2 as output
digitalWrite(signal, LOW);                 //Sensor is triggered by pin2 with delay
delay(5);
digitalWrite(signal, HIGH);
delay(10);
digitalWrite(signal, LOW);

pinMode(signal, INPUT);                  //Declaring pin2 as input
duration = pulseIn(signal, HIGH);    //To read the pulse

// Speed of sound is around 340m/s or 0.0136inche/microsec.
inches = (duration/2)/74;   //converting time into a distance in Inch
Wire.write(inches);         //Return value to master
delay(10);
}

Output:

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.