Challenge

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

CIRCUIT

CODE

Master

//MASTER CODE

#include <Wire.h> // library for I2C communication
#define signalPin 8

long duration, inches, cm;

void setup()

{
Wire.begin(); //initializing the I2C communication
Serial.begin(9600);
}


void loop()
{

// The Signalpin is triggered by a HIGH pulse of 2 microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(signalPin, OUTPUT);

digitalWrite(signalPin, LOW);
delayMicroseconds(2);
digitalWrite(signalPin, HIGH);
delayMicroseconds(5);
digitalWrite(signalPin, LOW);

//The same pin is used to read the signal from the Signalpin:
//a HIGH pulse whose duration is the time(in microseconds) from the sending of the pin
// to the reception of its echo off of an object.
pinMode(signalPin, INPUT);
duration = pulseIn(signalPin, HIGH);

//there are 73.746 microseconds per inch ,we divide by 2 to get the distance of the obstacle.
inches = (duration/74)/2;

//The speed of sound is 340 m/s or 29 microseconds per centimeter
cm = (duration/29)/2;

Wire.beginTransmission(8); //start th transmission
Wire.write(inches); //sending the value to slave
Wire.endTransmission(); //ending the transmission
Serial.println("Distance"); //printing on serial monitor
Serial.print(inches);
Serial.print(" inches, ");
Serial.print(cm);
Serial.println(" cms ");

delay(500);
}

Slave

//SLAVE CODE

#include <Wire.h> //library for I2C communication
#include <LiquidCrystal.h> //library for LCD

LiquidCrystal lcd(12,11,6,5,4,3); //initializing the LCD pins

int inches = 0; //global variable to store master data
int avg;

void setup()
{
Serial.begin(9600); //to begin serial communitcation at baud rate 9600
lcd.begin(16,2); //settinG up the number of columns and rows of the LCD
Wire.begin(8); //Initializing the I2C communication
Wire.onReceive(receiveEvent); //function call on receiving the data from master
}

void loop()
{
//printing inches and average on Serial monitor
Serial.print("Data in inches: ");
Serial.println(inches);
Serial.print(" Average ");
Serial.println(avg);

//printing Inches on LCD
lcd.setCursor(0,0); //setting the cursor of LCD to the sarting
lcd.print("Ultrasonic Sensor"); //printing on the LCD
lcd.setCursor(0,1);
lcd.print("distance:");
lcd.print(inches);
lcd.print(" inch");
delay(500);
lcd.clear();
}


void receiveEvent(int howMany) //called function definition
{
inches = Wire.read(); //storing the data from master
//processing inches to get average
avg = 0;
for(int i=0; i<5; i++)
{
avg = avg + inches;
delay(5);
}
avg = avg/5;
}

OUTPUT

Serial Monitor Output of Master

Serial Monitor Output of Slave

LINK

https://www.tinkercad.com/things/lc0vEd7skg8-funky-waasa/editel?sharecode=M9mvmRphJt2b2w7NhI0pxlD5PM4avxKXm2XH0JDjgWQ

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.