This example is for Wiring version 0027+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know.
SRF04/SRF05 Devantech Ultrasonic Ranger Finder by BARRAGAN
Demonstrates reading data from the Devantech Utrasonic Rangers SFR04/SFR05
int echoPin = 5; // digital pin to receive echo pulse
int triggerPin = 3; // digital pin to send trigger pulse
unsigned long distance = 0;
void setup()
{
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
}
void loop()
{
digitalWrite(trigPin, HIGH); // set HIGH for 15us to trigger ranging
delayMicroseconds(15);
digitalWrite(trigPin, LOW); // set pin LOW
distance = pulseIn(echoPin, HIGH); // read in pulse length
distance = distance/58; // calculate distance from pulse length
Serial.println(distance, DEC);
delay(50); // wait 50ms for next ranging
}