Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
OVERVIEW:
This code uses two Arduino boards to measure distance using an ultrasonic sensor(28015 Ping). and display the average distance on an LCD. The master board reads the distance data from the slave board using I2C communication and calculates the average distance by taking multiple readings. The distance readings are displayed on the serial monitor and the LCD. The slave board triggers the ultrasonic sensor, measures the duration of the pulse, and calculates the distance in centimeters and inches.
MASTER CODE:
// MASTER
#include <LiquidCrystal.h> // Library for controlling LCD displays
#include <Wire.h> // Library for I2C communication
// Define the I2C slave address
#define SLAVE_ADDRESS 8
// Define the number of samples to take for averaging
#define SAMPLES 10
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);//define pins rs,en,d4,d5,d6,d7
// Declare variables to store the readings
int readings[SAMPLES]; // Array to store the readings
int index = 0; // Index of the current reading
int total = 0; // Sum of all the readings
int average = 0; // Average of the readings
void setup()
{
Wire.begin(); // Initialize I2C communication
lcd.begin(16, 2); // Initialize the LCD display
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop()
{
int distance = getDistance(); // Get the current distance reading
total = total - readings[index];// Subtract the oldest reading from the total
readings[index] = distance; // Store the new reading in the array
total = total + readings[index];// Add the new reading to the total
//Increment the index of the current reading,
index = (index + 1);
if(index>=SAMPLES)
{
index=0;// if samples exceeds 10 index becomes 0
}
average = total / SAMPLES; // Calculate the new average distance
int inch = average * 0.393701;// Convert average distance to inches
//Print the current distance and average distance to the serial monitor
Serial.print("Distance (cm): ");
Serial.print(distance);
Serial.print(", Average Distance (cm): ");
Serial.println(average);
// Display the average distance on the LCD
lcd.setCursor(0, 0);
lcd.print("Distance:");
lcd.print(average);
lcd.print(" cm");
// Display the distance in inches on the second line of the LCD
lcd.setCursor(9, 1);
lcd.print(inch);
lcd.print(" inch");
}
// Function to read the distance from the ultrasonic sensor
int getDistance()
{
// Send a request to the I2C slave for the distance reading
Wire.requestFrom(SLAVE_ADDRESS, 1);
// Wait until the slave responds with a distance reading
while (Wire.available() == 0);
// Read the distance reading from the I2C slave
int distance = Wire.read();
// Return the distance reading
return distance;
}
SLAVE CODE:
//SLAVE
#include <Wire.h> // Library for I2C communication
#define INPUT_PIN 2 //input pin 2 of ultrasonic sensor
// Initialize variables for distance and average distance
int distance;
static int avg;
void setup()
{
Wire.begin(8); // Initialize the I2C communication as a slave device with address 8
Wire.onRequest(sendDistance);// sendDistance() function to be called when the master requests data
}
void loop()
{
delay(500);// Delay for 500 milliseconds between readings
}
// This function is called when the master requests data
void sendDistance()
{
pinMode(INPUT_PIN, OUTPUT);// Set the input pin to output mode
// Send a 10 microsecond pulse to trigger the ultrasonic sensor
digitalWrite(INPUT_PIN, LOW);
delayMicroseconds(10);
digitalWrite(INPUT_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(INPUT_PIN, LOW);
// Set the input pin back to input mode and measure the duration of the pulse
pinMode(INPUT_PIN, INPUT);
distance = pulseIn(INPUT_PIN, HIGH);
// Convert the duration to a distance in centimeters (assumes speed of sound is 343 meters per second)
avg = (distance / 58);
// Send the distance in centimeters to the master over I2C
Wire.write(avg);
}
OUTPUT:
