Challenge

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

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

int readings[numReadings]; // array to store the readings

int index = 0; // current position in the array

int total = 0; // running total of the readings

void setup() {

// initialize the ultrasonic sensor and serial communication

}

void loop() {

// read the distance from the ultrasonic sensor

int distance = readDistance();

// add the new reading to the total and subtract the oldest reading

total = total - readings[index] + distance;

readings[index] = distance;

index = (index + 1) % numReadings;

// calculate the moving average and print it to the serial monitor

int average = total / numReadings;

Serial.println(average);

// delay for some time to prevent rapid changes in the readings

delay(100);

}

int readDistance() {

// send a pulse to the ultrasonic sensor to start the measurement

// wait for the pulse to return and measure the duration

// calculate the distance using the formula above

// return the distance

}

An ultrasonic sensor is a device that uses sound waves to measure distance. It emits a high-frequency sound wave and measures the time it takes for the wave to bounce back after hitting an object, which can then be used to calculate the distance between the sensor and the object.

To use an ultrasonic sensor to measure distance, you will need an Arduino board, an ultrasonic sensor (such as the 28015 Ping sensor), and some jumper wires to connect them. Here are the steps to measure the distance of an object using an ultrasonic sensor and an Arduino:

Connect the VCC pin of the ultrasonic sensor to the 5V pin of the Arduino, the GND pin to the GND pin of the Arduino, and the Echo and Trig pins to two digital pins of the Arduino, respectively.

Write a sketch in the Arduino IDE to read the data from the ultrasonic sensor using the pulseIn() function. The sketch should first send a pulse on the Trig pin to start the measurement, and then read the duration of the pulse on the Echo pin to calculate the distance using the formula:

distance = duration * 0.034 / 2;

where duration is the time in microseconds for the sound wave to travel from the sensor to the object and back.

To smoothen the sensor data, you can use a moving average filter technique. This involves taking an average of the last few sensor readings to remove high-frequency variations in the data. Here's an example of how to implement a moving average filter in your Arduino sketch.

In this example, the sketch reads the distance from the ultrasonic sensor in the readDistance() function, which should be similar to the one mentioned in step 2. The distance is then added to the total and the oldest reading in the readings array is subtracted. The index is updated to wrap around the array, and the moving average is calculated by dividing the total by the number of readings. The average is then printed to the serial monitor, and a delay is added to prevent rapid changes in the readings.

Overall, by following these steps, you should be able to measure the distance of an object using an ultrasonic sensor and smoothen the sensor data using a moving average filter technique.

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.