Challenge

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

https://www.tinkercad.com/things/4b5rNyLHOBX-bodacious-maimu/editel?sharecode=S0s_BnznzWD5RsZMd41FJm3Kaux3OI2Cmrmmfd8WN2c

Sensor Features

*The circuit:
+V connection of the PING))) attached to +5V
GND connection of the PING))) attached to ground
SIG connection of the PING))) attached to digital pin 7 this constant won't change.                    It's the pin number of the sensor's output:

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.

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.

const int Pin = 7;

void setup()

{

Serial.begin(9600);
}

void loop()

{

long duration, inches, cm;
pinMode(Pin, OUTPUT);
digitalWrite(Pin, LOW);
delayMicroseconds(2);
digitalWrite(Pin, HIGH);
delayMicroseconds(5);
digitalWrite(Pin, LOW);

pinMode(Pin, INPUT);
duration = pulseIn(Pin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)

{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {

return microseconds / 29 / 2;
}

https://www.tinkercad.com/things/4b5rNyLHOBX-bodacious-maimu/editel?sharecode=S0s_BnznzWD5RsZMd41FJm3Kaux3OI2Cmrmmfd8WN2c

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.