Ultrasonic Sensor HC-SR04 Seemingly Not Detecting Objects

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:

I guess we just have to take your word of honour about that.

I've now attached it.

Duration or distance ?


Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

Is there a particular reason you define duration, distance and inches that way, in loop()?

Why wouldn't you?
They get updated each iteration of loop.

Sure, what do I know? I just don't normally see it done that way. I also define max distance to ping for ahead before setup() and my sensors always work as intended. Maybe their sensor is broken.
I do know that these sensors can't read accurately at 471 inches.

Is everything grounded properly?

Maybe you haven't seen much well-written code that limits scope to the required minimum - what do I know?

Didn't you just say this after OP asserted that "the program I am using is also correct". I didn't try this code and see. Did you? Won't say it's well-written though, there's no need for a delay that long to "stabilize the readings".

There's also no need to take a pin LOW that should be / is already LOW, but that's minor compared to littering code with unnecessary globals, IMO.

It doesn't stabilise anything, but it does limit the rate the pings go out, which is a good thing.

What happens when you load and run a simple HC-SR04 example "demo" sketch? One that is known to work?

BTW
Do not take short cuts.
This

    //==================== 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 =======

Change to this

    //==================== 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 =======

OP's sketch works, must be a bad sensor or wired wrong.

Note:
Serial.println(duration);
should be changed to:
Serial.println(distance);

2 Likes

Impossible. :slight_smile: We were told, "I correctly set up this ultrasonic sensor".

Just ran that code. Readings all over the place. Took out everything besides just reading the sensor and it ran fine. Also changed duration to distance

Larry wins.

It makes it easier to read, I guess. I thought we were supposed to avoid delay whenever possible, no?

Not if there's nothing else to do, no.

Edit: does anyone else get a brief glimpse of an "oops that page doesn't exist" when they open this topic?
This forum software is weird

Oh yeah? Best to be prepared for anything, like a jungle cat. My :robot: could beat up your :robot:, I'll bet.