Challenge

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

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

In this project ultrasonic sensor is used to measure the distance of an object. The sensor used is 28015 Ping.

Working of 28015 Ping

The PING sensor measures distance using sonar; an ultrasonic (well above human hearing) pulse is transmitted from the unit and distance-to-target is determined by measuring the time required for the echo return. Output from the PING sensor is a variable-width pulse that corresponds to the distance to the target.

Schematics

Program

const int sensor= 7;

int x;

long duration, inches, Average_duration,i,distance;

void setup() {

// initialize serial communication:

Serial.begin(9600);

}

void loop() {

long sum=0;

for (i=0;i<5;i++); // for the averaging

{

pinMode(sensor, OUTPUT);

digitalWrite(sensor, LOW);

delayMicroseconds(2);

digitalWrite(sensor, HIGH);

delayMicroseconds(5);

digitalWrite(sensor, LOW);

pinMode(sensor, INPUT);

duration = pulseIn(sensor, HIGH);

sum=sum+duration;

Serial.println(sum);

}

Average_duration=sum/5;

inches = microsecondsToInches(Average_duration);

distance=inches*5.18;

Serial.println(Average_duration);

Serial.print(distance);

Serial.print(" inches");

Serial.println();

delay(100);

}

// to convert duration to distance in inches

long microsecondsToInches(long microseconds) {

return microseconds / 74 / 2;

}

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.