Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
PROJECT _ 2
Overview of the Project:
In this project the implementation of code for “Measuring distance of an object using ultrasonic distance sensor (28015 Ping) and also smoothen the sensor data using moving average filter”.
Goals:
- 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.
Key Highlights of the Project:
- Measuring the distance travelled by the wave to the object and gets bounced back from the object to sensor.
- Smoothening the sensor data by moving average filter technique, the filter has a window of certain size. That window determines that how many data the filter can take.
Deliverables of this project:
- The distance travelled by a wave to the object for which we want to measure the distance using Ultrasonic Distance Sensor (28015 Ping).
- Eliminate the high frequency jitters from the sensor data by Moving Average filter to smoothen the sensor data.
Expected Outcomes:
The students will get the Knowledge on Ultrasonic sensor (28015 Ping) to measure the distance travelled by wave to the object, the hardware connection of 2 Arduino’s connected to the sensor and smoothening the sensor data by removing high frequency variations using Moving Average Filter Technique.
Solution _
Goal _ 1_a_Measuring the distance in terms of inches of an object using Ultrasonic Distance Sensor (28015 Ping).
Components Needed:
- Arduino Uno
- Ultrasonic Distance Sensor(28015 Ping).
- Potentiometer (simulating an analog sensor)
- Resistor (for potentiometer)
- Jumper wires
Connection Setup:
- Connect the VCC (5V) pin of the Ultrasonic Sensor to the 5V output on the Arduino.
- Connect the GND pin of the Ultrasonic Sensor to the GND on the Arduino.
- Connect the Sig pin of the Ultrasonic Sensor to a digital pin (e.g., D2) on the Arduino.
- This pin is used both for sending the trigger signal and receiving the echo signal.
- Connect one end of the potentiometer to 5V, the other end to GND, and the wiper to analog pin A0 on the Arduino.
- Add a resistor between the wiper and the GND to create a voltage divider.
// Arduino Code
const int signalPin = 2; // Connect the SIGNAL pin of the sensor to digital pin 2
void setup() {
Serial.begin(9600); // Initialize serial communication with a baud rate of 9600
}
void loop() {
// Send a pulse to trigger the Ultrasonic Sensor
pinMode(signalPin, OUTPUT); // Set the SIGNAL pin as an OUTPUT
digitalWrite(signalPin, LOW); // Set the SIGNAL pin to LOW for a short duration
delayMicroseconds(2); // Wait for a moment
digitalWrite(signalPin, HIGH); // Set the SIGNAL pin to HIGH to send a pulse
delayMicroseconds(10); // Keep it HIGH for a short duration
digitalWrite(signalPin, LOW); // Set the SIGNAL pin back to LOW
// Measure the time it takes for the echo signal to return
pinMode(signalPin, INPUT); // Set the SIGNAL pin as an INPUT to receive the echo signal
long duration = pulseIn(signalPin, HIGH); // Measure the time the echoPin stays HIGH
// Convert the time to inches using the speed of sound
int inches = microsecondsToInches(duration);
// Print the measured distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(inches);
Serial.println(" inches");
delay(1000); // Wait for a moment before taking the next measurement
}
long microsecondsToInches(long microseconds) {
// Speed of sound at sea level is approximately 343 meters per second (or 1125 feet per second)
// Dividing by 74 gives the time it takes for the sound to travel round trip (to the target and back)
// Dividing by 2 gives one-way travel time, which is what we need
return microseconds / 74 / 2;
}
Goal _ 1_b_The sensor data is smoothened by using Moving average filter technique to eliminate the high frequency variations from the sensor data.
Moving average filter technique code
const int windowSize = 5; // Define the window size for the moving average filter
int readings[windowSize]; // Array to store sensor readings for the moving average
int index = 0; // Index for the readings array
// Function to calculate the moving average
int movingAverage(int value) {
int total = 0;
// Shift values in the array to make room for the new value
for (int i = windowSize - 1; i > 0; i--) {
readings[i] = readings[i - 1];
total += readings[i];
}
// Add the new sensor value to the array
readings[0] = value;
total += value;
// Calculate and return the moving average
return total / windowSize;
}
void setup() {
Serial.begin(9600); // Begin serial communication at 9600 baud rate
}
void loop() {
int sensorValue = analogRead(A0); // Read the sensor value from analog pin A0
// Smooth the sensor data using the moving average filter
int smoothedValue = movingAverage(sensorValue);
// Print both raw and smoothed sensor values to the Serial Monitor
Serial.print("Raw Sensor Value: ");
Serial.print(sensorValue);
Serial.print("\tSmoothed Value: ");
Serial.println(smoothedValue);
delay(1000); // Wait for a moment before taking the next measurement
}