Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
example Arduino code that you can use to measure distance using an ultrasonic distance sensor (Ping 28015) and apply a moving average filter to smoothen the sensor data. This code assumes you have two Arduino boards connected, one for the sensor and one for data processing.
const int pingPin = 7;
const int numReadings = 5; // Number of readings to take for the moving average
unsigned long readings[numReadings]; // Array to store the readings
int index = 0; // Index for the readings array
long total = 0; // Total of the readings
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// establish variables for duration of the ping, and the distance result
// in inches and centimeters:
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the ping
// to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// Convert the time into inches
inches = microsecondsToInches(duration);
// Add the reading to the total
total = total - readings[index] + inches;
readings[index] = inches;
index = (index + 1) % numReadings;
// Calculate the moving average
int smoothedDistance = total / numReadings;
// Print the smoothed distance
Serial.print("Smoothed Distance: ");
Serial.print(smoothedDistance);
Serial.println(" inches");
delay(100); // Delay for stability
}
long microsecondsToInches(long microseconds) {
// According to Parallax's datasheet for the PING))), there are 73.746
// microseconds per inch (i.e. sound travels at 1130 feet per second).
// This gives the distance travelled by the ping, outbound and return,
// so we divide by 2 to get the distance of the obstacle.
// See: https://www.parallax.com/package/ping-ultrasonic-distance-sensor-downloads/
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2;
}
-
In the step function, we initialize the pins and the readings array.
-
In the loop function, we measure the distance using the Ping sensor and convert the time to inches. We then update the readings array and calculate the moving average.
-
Finally, we print the smoothed distance to the serial monitor.
Make sure you have the Ping sensor connected to the appropriate pins on one Arduino board and connect another Arduino board to the serial monitor to view the smoothed distance data.