Challenge

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

The distance traveled by a wave to the object for which we want to measure the distance using the Ultrasonic Distance Sensor (28015 Ping)

It seems like you're interested in measuring the distance traveled by the ultrasonic waves from the sensor to the object. Typically, the ultrasonic sensor measures the time it takes for the sound wave to travel to the object and back, and you can calculate the distance based on the speed of sound in the air.

Here's a basic outline of how you might approach this using an ultrasonic sensor like the HC-SR04:

1. Measure the Time of Flight (TOF): The sensor provides the time it takes for the ultrasonic wave to travel to the object and back. This time is often given in microseconds.

2. Convert TOF to Distance: You can use the formula \( \text{Distance} = \frac{\text{TOF} \times \text{Speed of Sound}}{2} \), where the Speed of Sound is typically around 343 meters per second in dry air at 20 degrees Celsius.

Here's a simplified example of Arduino code to measure and calculate the distance:

const int trigPin = 2; // Trig pin of the ultrasonic sensor

const int echoPin = 3; // Echo pin of the ultrasonic sensor

void setup() {

Serial.begin(9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

}

void loop() {

trigger the ultrasonic sensor

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

Read the time it takes for the ultrasonic waves to return

long duration = pulseIn(echoPin, HIGH);

Calculate the distance

float distance = duration * 0.0343 / 2; The speed of sound in air is 343 m/s

Print the distance to the Serial Monitor

Serial.print("Distance: ");

Serial.print(distance);

Serial.println(" cm");

delay(1000); // Adjust the delay based on your needs

}

This code sends a pulse to the ultrasonic sensor, measures the time it takes for the echo signal to return, and calculates the distance using the speed of sound.

EXPECTED OUTCOMES:

The students will learn how to use an ultrasonic sensor (28015 Ping) to measure the distance traveled by wave to the object, the hardware connection of two Arduino’s connected to the sensor, and smoothing the sensor data by removing high frequency variations using the Moving Average Filter Technique.

Certainly! It sounds like you're working on a project involving ultrasonic sensors, Arduino boards, and data smoothing techniques. Let me break down the key components and steps for you.

Ultrasonic Sensor (HC-SR04):

The HC-SR04 ultrasonic sensor is a commonly used module for measuring distance. It works by sending out ultrasonic waves and measuring the time it takes for the waves to bounce back after hitting an object. Here's a basic connection guide:

1. Connect the VCC and GND pins:

VCC to 5V on Arduino

GND to GND on Aruino

2. Connect the Trig and Echo pins:

- Trig to a digital pin (e.g., D2) on Arduino.

echo to another digital pin (e.g., D3) on Arduino.

Hardware Connection for Two Arduinos:

If you're using two Arduinos, you might want to establish communication between them. One possible way is by using serial communication (UART). Here's a simple setup:

Arduino 1 (Sender):

void setup() {

Serial.begin(9600);

}

void loop() {

Your distance measurement code

Send data to the other Arduino

Serial.print(distance);

delay(1000); // Adjust the delay based on your needs

}

Arduino 2 (Receiver):

void setup() {

Serial.begin(9600);

}

void loop() {

if (Serial.available() > 0) {

// Read the distance data from Arduino 1

distance = Serial.parseInt();

Smoothing and other processing

// ...

}

}

### Smoothing with a Moving Average Filter:

A moving average filter helps smooth out variations in data. You can implement it in the Arduino code to filter out high-frequency noise in the sensor readings. Here's a simple example:

const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input

int index = 0; // the index of the current reading

int total = 0; // the running total

int average = 0; // the average

void setup() {

// Initialize the serial communication

Serial.begin(9600);

// Initialize the readings array

for (int thisReading = 0; thisReading < numReadings; thisReading++) {

readings[thisReading] = 0;

}

}

void loop() {

Read the sensor value

int sensorValue = analogRead(A0);

Subtract the last reading

total = total minus readings [index];

Add the new reading

readings[index] = sensorValue;

total = total + readings [index];

// Move to the next position in the array

index = (index + 1) % numReadings;

Calculate the average

average = total / numReadings;

Output the smoothed value

Serial.println(average);

delay(100); // Adjust the delay based on your needs

}

This code maintains a rolling average of the last `numReadings` sensor values, providing a smoother output.

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.