I have the Arduino Mega 2560 and I correctly set up this ultrasonic sensor. The program I am using is also correct. When I run the program, I have it print the duration (returned from pulseIn(echoPin, HIGH)), and delay for 100 milliseconds. The echoPin is set to 7 and the trigPin is set to 8, which correspond to where they are on the board. Below I've attached the code:
#define trigPin 8
#define echoPin 7
#define Rled 2
#define Yled 3
#define Gled 4
#define buzzer 10
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Rled, OUTPUT);
pinMode(Yled, OUTPUT);
pinMode(Gled, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
long duration, distance, inches;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo signal
duration = pulseIn(echoPin, HIGH); // Read duration for roundtrip distance
distance = (duration /2) * 0.0135 ; // Convert duration to one way distance in units of inches
if (distance <= 12) { // Outer IF statement units of inches
if (distance <=6){ // Alert range condition
digitalWrite(Rled, HIGH); // Alert red LED on
digitalWrite(Yled, LOW);
digitalWrite(Gled, LOW);
}
if (distance <12 and distance > 6){ // Warning range condition
digitalWrite(Rled, LOW);
digitalWrite(Yled, HIGH); // Warning yellow LED on
digitalWrite(Gled, LOW);
}
//==================== Beeping Rate Code Start ======
digitalWrite(buzzer,HIGH);
for (int i= distance; i>0; i--)
delay(10);
digitalWrite(buzzer,LOW);
for (int i= distance; i>0; i--)
delay(10);
//==================== Beeping Rate Code End =======
}
else{ //Safe range condition
digitalWrite(Rled, LOW);
digitalWrite(Yled, LOW);
digitalWrite(Gled, HIGH); // Safe distance green LED on
digitalWrite(buzzer, LOW);
}// end of outer IF statement
//if (distance < 156) // Filter noise to show readings only less than the sensor range of 13 ft = 156 inches
Serial.println(duration); // print distance to show in Serial Monitor
delay(100); //pause program to stabilize ultrasonic sensor readings
} //end of loop
The outputs I get are very large values that fall very closely with one another:
69825
69829
69831
69826
69828
69829
69822
69818
Converted, this means that the object it detects is 471 inches away. It keeps outputting this value no matter what I put in front of it or around it. I also find that if I touch particular transistors then the values drop significantly, although again, they remain consistent regardless of what else is done.
What could be the problem?
Edit:
Here is the circuit schematic: