Hi,
I just started with an Ardiuno Uno, and I'm trying to connect an ultrasonic module. It should be pretty simple, but for some reason it isn't working. I'm pretty sure it's properly connected (I use have a sensor board) and I'm using the following code:
#define trigPin 9
#define echoPin 8
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin,HIGH);
distance = (duration/2) / 29.1;
Serial.print("duration: ");
Serial.println(duration);
Serial.print("distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
I copy-pasted it directly from a tutorial, and tried several delays for the trigpin high signal. I keep getting this as output:
duration: 10138
distance: 174 cm
duration: 10142
distance: 174 cm
duration: 10098
distance: 173 cm
...
While it should be more like 10 cm. And the duration doesn't seem to change when I change the distance to the object.
Also, the 29.1 in the distance formula should somehow relate to the speed of sound, but I have no clue how.
Can you help me out? Thanks