I have a project I am working on, but I can not get the ultrasonic sensor to work.
It's the US-100
with 5Volt+, Trig, Echo, Ground, Ground
I have all five wires hooked up correctly.
It returns:
"Out of range
0 cm"
Code as follows:
#define trigPin 5
#define echoPin 7
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
int reset = (3000);
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(500); //was 1000 - does not appear to make a difference
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0)
{
Serial.println("Out of range");
Serial.print(distance); //Added
Serial.println(" cm"); //Added
}
else
{
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}