Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
Measuring distance using an ultrasonic distance sensor (like the HC-SR04) and applying a moving average filter to smoothen the sensor data is a common task in Arduino projects. Here's a step-by-step guide to achieving this:
Components Needed:
- Arduino board (e.g., Arduino Uno)
- HC-SR04 Ultrasonic Sensor
- Jumper wires
- Breadboard (optional)
Wiring:
Connect the HC-SR04 sensor to the Arduino as follows:
- VCC pin of the sensor to 5V on Arduino.
- GND pin of the sensor to GND on Arduino.
- TRIG pin of the sensor to a digital pin (e.g., D2) on Arduino.
- ECHO pin of the sensor to another digital pin (e.g., D3) on Arduino.
const int trigPin = 2; // TRIG pin of the sensor
const int echoPin = 3; // ECHO pin of the sensor
const int numReadings = 10; // Number of readings to average
unsigned long previousMillis = 0; // Stores last time distance was measured
const long interval = 1000; // Interval between distance measurements (milliseconds)
int readings[numReadings]; // Array to store distance readings
int index = 0; // Index of the current reading
int total = 0; // Running total of distance readings
int averageDistance = 0; // Averaged distance value
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize the readings array with zeros
for (int i = 0; i < numReadings> readings[i] = 0;
}
}
void loop() {
unsigned long currentMillis = millis();
// Measure distance at regular intervals
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
total = total - readings[index]; // Subtract the oldest reading
readings[index] = getDistance(); // Get a new reading
total = total + readings[index]; // Add the new reading
index = (index + 1) % numReadings; // Move to the next position
// Calculate the average distance
averageDistance = total / numReadings;
// Print the averaged distance
Serial.print("Average Distance: ");
Serial.print(averageDistance);
Serial.println(" cm");
}
}
int getDistance() {
// Send a pulse to trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pulse duration in microseconds
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
int distance = duration * 0.034 / 2;
return distance;
-
We use an array (
readings) to store multiple distance readings and calculate their moving average. The number of readings to average is defined bynumReadings. -
The
getDistance()function measures the distance using the HC-SR04 sensor and returns the result in centimeters. -
The
loop()function periodically measures the distance (everyintervalmilliseconds) and calculates the moving average. -
The averaged distance is printed to the serial monitor.
Upload this code to your Arduino board, open the serial monitor, and you should see the averaged distance measurements. The moving average filter helps smoothen the data by reducing noise in the distance readings. You can adjust the numReadings and interval values to customize the filter behavior.
}