Hi,
I have this Ultrasonic Range Sensor from Lynxmotion:
lynxmotion.com/Product.aspx? … tegoryID=8
and I just adapted this tutorial from Arduino:
arduino.cc/en/Tutorial/Ping
(which is for Ping))) sensor), to come up with a Hello-URS!
I failed
Well, as I understand, for the URS to start to work, I have to first pass a HIGH pulse to the Trigger pin, then set it LOW, and then read the Echo pin to see when it comes LOW from HIGH. The duration of HIGH pulse from ECHO pin can then be converted to distance. Did I get it right? )
If I am right with the idea, then why does my code returns a loop of “0 cm” in the following code?
int pingPin = 3;
int echoPin = 4;
void setup()
{
Serial.begin(9600);
}
void loop()
{
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We 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);
// 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(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
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;
}