I have an ultrasonic distance sensor (HC-SR04) attached to an Arduino Uno. Pins 11, 12, and 13 are attached to VCC, Trigger, and Echo respectively and the Ground pin is grounded. All the ping sketches I've found online result in a distance reading of zero no matter where i point the sensor. In particular the following sketch:
#define trigPin 12
#define echoPin 13
#define VCC 11
void setup() {
pinMode(VCC, OUTPUT);
digitalWrite(VCC, HIGH);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin (9600);
}
void loop() {
digitalWrite(VCC, HIGH);
long duration, distance;
// Send trigger pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(12);
digitalWrite(trigPin, LOW);
// Get pulse length
duration = pulseIn(echoPin, HIGH);
// Convert and print distance
Serial.print(duration);
Serial.print(" us - ");
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
// Every 100 ms
delay(100);
}
Also the sketch found here http://forum.arduino.cc/index.php/topic,106043.0.html results in similar output, but with 19 microseconds being the most common return value as opposed to 23 us as seen in the attached screenshot.
The sensors sounds like it's working - I hear a high frequency clicking noise coming from the speakers. I have also tried setting the trigger pulse length to various values including 3, 5, 10, 12, and 15 microseconds but all have the same result. The product page indicates that a pulse of 10 microseconds is required to activate the ping.