Challenge

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

In this project 2 arduino's are connetced using I2c communication one(master) arduino is used to read the ultrasonic distance sensor and 2nd(slave) arduino is used to write this data to LCD screen

What is an Ultrasonic Sensor?

An apparatus that uses ultrasonic sound waves to gauge an object's distance is called an ultrasonic sensor. An ultrasonic sensor transmits and receives ultrasonic pulses using a transducer to determine the proximity of an item. Different echo patterns are created when high-frequency sound waves reflect across barriers.

How Ultrasonic Sensors Work

For ultrasonic sensors to function, a sound wave that is higher above the human hearing range must be sent out. The sensor's transducer receives and transmits ultrasonic sound like a microphone.

Moving Average Filter

A moving average filter is a type of finite impulse response (FIR) filter that is used to smooth out signals that have noisy fluctuations or short-term overshoots while maintaining a crisp step response or accurate representation of the signal. It is a straightforward yet sophisticated statistical technique for time-domain signal de-noising.

code for the given project:

code for master

//I2C master code

//I2c communication between two arduino

#include<Wire.h>//libraray for I2C communication function

float inches = 0;

float cm = 0;

float x=0;

//function to read and write ultrasonic distance sensor//

long readUltrasonicDistance(int triggerPin, int echoPin)

{

pinMode(triggerPin, OUTPUT); // Clear the trigger

digitalWrite(triggerPin, LOW);

delayMicroseconds(2);

// Sets the trigger pin to HIGH state for 10 microseconds

digitalWrite(triggerPin, HIGH);

delayMicroseconds(10);

digitalWrite(triggerPin, LOW);

pinMode(echoPin, INPUT);

// Reads the echo pin, and returns the sound wave travel time in microseconds

return pulseIn(echoPin, HIGH);

}

void setup()

{

Serial.begin(9600);//begine serail communication at 9600 boundrate

Wire.begin();//begine serail communication at pin(A4,A5)

}

void loop() {

// measure the ping time in cm

x = 0.01723 * readUltrasonicDistance(A0, A0);

cm = x;

inches = (cm / 2.54);// convert to inches by dividing by 2.54

byte MasterSend = x;

Wire.beginTransmission(8);//start transmit to slave arduino(8)

Wire.write(MasterSend);//send one byte converted POT value to slave

Wire.endTransmission();//stop transmitting

Serial.print(inches);

Serial.print("in, ");

Serial.print(cm);

Serial.println("cm");

delay(100); // Wait for 100 millisecond(s)

}

code for slave :

//I2C slave code

//I2C communication between two arduino

#include<Wire.h>//libraray for I2C communication function

#include<LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);//define LCD module pin(RS,EN,D4,D5,D6,D7)

byte SlaveReceived = 0;

byte SlaveReceived1 = 0;

const int numReadings = 10; // Number of readings to average

int readings[numReadings]; // Array to store sensor readings

int index = 0; // Index for circular buffer

int total = 0; // Running total of sensor readings

int average = 0; // Smoothed sensor reading

void setup()

{

lcd.begin(16,2);//initilize LCD display

lcd.setCursor(0,0);//sets cursor at first line of display

lcd.print("skill lync");//prints skill lync in LCD

lcd.setCursor(0,1);//sets cursor at secound line of display

lcd.print("I2C arduino 2 arduino");//prints I2C arduino 2 arduino in LCD

delay(5000);

lcd.clear();

Serial.begin(9600);//begins serial communciation at 9600 baudrate

Wire.begin(8);//begine I2C communication with slave address as 8 at pin(A4,A5)

Wire.onReceive(receiveEvent);//function call when slave receive value from master

for (int i = 0; i < numReadings; i++) {

readings[i] = 0;

}

}

void loop()

{

lcd.setCursor(0,0);//sets currsor at line one of LCD

lcd.print("Distance:");

lcd.print(SlaveReceived);

lcd.setCursor(12,0);

lcd.print("cm");

Serial.println("slave received from msater");

Serial.print(SlaveReceived);

Serial.print("cm, ");

SlaveReceived1 = (SlaveReceived / 2.54);

lcd.setCursor(0,1);//sets cursor at line two of LCD

lcd.print("Distance:");//prints master value in LCD

lcd.print(SlaveReceived1);//prints slave received value in LCD recieved from master

lcd.setCursor(12,1);

lcd.print("inch");

Serial.print(SlaveReceived1);

Serial.print("inch");

Serial.println();

delay(500);

lcd.clear();

}

void receiveEvent(int howMany)//this function is called when slave received from master

{

SlaveReceived = Wire.read();//used to read value received from master and store in varaibale slavereceived

//function to smoothen the sensor data using moving average filter

total -= readings[index];

// Add the new reading to the total

total += SlaveReceived;

// Store the new reading in the readings array

readings[index] = SlaveReceived;

// Increment the index in a circular manner

index = (index + 1) % numReadings;

// Calculate the average

average = total / numReadings;

SlaveReceived = average;

}

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.