These are widely available on eBay. I sell them here.
Sample Routine - PICAXE
#picaxe 20X2 #terminal 9600 ' SR04.Bas (PICAXE-20X2) ' ' Illustrates an interface with the SR04 ' Launches pulse using PulsOut Command and measures the width of the Echo ' pulse. ' ' PICAXE-20X2 SR04 ' ' Trig (B.0) --------------------- Trigger ' Echo (B.1) <--------------------- Echo ' ' GRD ------- GRD ' +5 VDC ---- 5VDC ' ' copyright, Peter Anderson, Baltimore, MD, Nov 9, '10 Symbol Tm = W0 Symbol RInches_10 = W1 dirsB = %00000001 low B.0 Pause 1000 ' allow system to settle when booting Do PulsOut B.0, 1 ' sonar output, 10 us PulsIn B.1, 1, Tm If Tm = 0 Then SerTxD("OverRange", CR, LF) Else RInches_10 = 6 * Tm /10 RInches_10 = 7 * Tm / 100 + RInches_10 RInches_10 = 8 * Tm / 1000 + RInches_10 SerTxD (#Tm, " ", #RInches_10, CR, LF) Endif Pause 1000 Loop
Sample Routine - Arduino
// SR04 Ultrasonic Ranging Module HC SR04 - Arduino // // SC SR04 is available at eBay or http://www.satistronics.com in China // // copyright, Peter Anderson, Baltimore, MD, Nov 3, '10 void display_q(long x); long microsecondsToInches_10(long d_microseconds); long InchesToCentimeters_10(long inches_10); #define trigPin 13 #define echoPin 12 void setup() { pinMode(trigPin, OUTPUT); digitalWrite(trigPin, LOW); pinMode(echoPin, INPUT); Serial.begin(9600); delay(2000); } void loop() { long duration, inches_10, cm_10; digitalWrite(trigPin, HIGH); // output a pulse on trigPin delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); inches_10 = microsecondsToInches_10(duration); cm_10 = InchesToCentimeters_10(inches_10); display_q(inches_10); Serial.print(" in, "); display_q(cm_10); Serial.print(" cm"); Serial.println(); delay(1000); } long microsecondsToInches_10(long d_microseconds) { long inches_10; // 1130 feet / second = 0.1356 tenths of inches / us // dividing by 2 for the round trip; // // inches_10 = 0.0678 * d_microseconds inches_10 = 6 * d_microseconds / 100 + 7 * d_microseconds / 1000 + 8 * d_microseconds / 1000; return inches_10; } long InchesToCentimeters_10(long inches_10) { long cm_10; cm_10 = 2 * inches_10 + 5 * inches_10 / 10 + 4 * inches_10 / 100; // cm = 2.54 * in return cm_10; } void display_q(long x) { int whole, fract; whole = x / 10; fract = x % 10; Serial.print(whole, DEC); Serial.print("."); Serial.print(fract, DEC); }