Ultrasonic module HC-SR04 is telling everything is more than 33 meters away

First of all, sorry for my english, isn't my first language.

Well, yesterday I decided to test my ultrasonic sensor, but when I loaded the code into my arduino and tried to test, it always say that everything is more than 33 meters away. I tried to put other things near it and it didn't worked at all. I tried lots of different codes and it's always saying its 33 meters away for some reason. I tried a code that just give the distance when something is 60cm away from it I think, and the serial monitor was blank all the time. This is normal, or I have to buy other HC-SR04?

Thanks, Conrado.

As I understand it the HC-SR04 has a maximum range of 4m (400cm). So if you think you are seeing more than 33m you are doing something wrong. For us to work out what is wrong you need to post your code and a diagram showing how you have connected everything.

Steve

Well, I'm using the example code of the library, here it is:

/*

  • HCSR04Ultrasonic/examples/UltrasonicDemo/UltrasonicDemo.pde
  • SVN Keywords

  • $Author: cnobile $
  • $Date: 2011-09-17 02:43:12 -0400 (Sat, 17 Sep 2011) $
  • $Revision: 29 $

*/

#include <Ultrasonic.h>

#define TRIGGER_PIN 12
#define ECHO_PIN 13

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

void setup()
{
Serial.begin(9600);
}

void loop()
{
float cmMsec, inMsec;
long microsec = ultrasonic.timing();

cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
Serial.print("MS: ");
Serial.print(microsec);
Serial.print(", CM: ");
Serial.print(cmMsec);
Serial.print(", IN: ");
Serial.println(inMsec);
delay(1000);
}

And click here to see the connection and what's showing in the serial monitor...

Conrado

Conrado_Rigotti:
Well, I'm using the example code of the library, here it is:

/*

  • HCSR04Ultrasonic/examples/UltrasonicDemo/UltrasonicDemo.pde
  • SVN Keywords

  • $Author: cnobile $
  • $Date: 2011-09-17 02:43:12 -0400 (Sat, 17 Sep 2011) $
  • $Revision: 29 $

*/

#include <Ultrasonic.h>

#define TRIGGER_PIN 12
#define ECHO_PIN 13

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

void setup()
{
Serial.begin(9600);
}

void loop()
{
float cmMsec, inMsec;
long microsec = ultrasonic.timing();

cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
Serial.print("MS: ");
Serial.print(microsec);
Serial.print(", CM: ");
Serial.print(cmMsec);
Serial.print(", IN: ");
Serial.println(inMsec);
delay(1000);
}

And click here to see the connection and what's showing in the serial monitor...

Conrado

I think the problem is with this line " #define ECHO_PIN 13 "
I'm not quite sure, but I think that pin 13 is connected to internal LED indicator so this pin is already busy.
Try to change it to another pin and also change that in your code, just the be sure.

In other tests I tried doing it with other pins like 6 and 7 but still don't works...

That seems to be a very old library that you're using. Have you tried your sensor with something like NewPing.h instead? Arduino Playground - NewPing Library

Steve

Steve, I added the library and tested it, but still not working for some reason... Now it says 0cm, sometimes says other distances, but they are "random" or something like that...

Try using this code:

// Use the HC-SR04 to detect distance in 100 CM

#define trigPin 6                       //Define the HC-SR04 trigger on pin 6 on the Arduino
#define echoPin 5                     //Define the HC-SR04 echo on pin 5 on the Arduino

void setup()
{
 Serial.begin (9600);                //Start the serial monitor
 pinMode(trigPin, OUTPUT);           //set the trigpin to output
 pinMode(echoPin, INPUT);            //set the echopin to input
}


void loop()
{
 int duration, distance;             //Define two integers duration and distance to be used to save data
 digitalWrite(trigPin, HIGH);        //write a digital high to the trigpin to send out the pulse
 delayMicroseconds(500);             //wait half a millisecond
 digitalWrite(trigPin, LOW);         //turn off the trigpin
 duration = pulseIn(echoPin, HIGH);  //measure the time using pulsein when the echo receives a signal set it to high
 distance = (duration / 2) / 29.1;   //distance is the duration divided by 2 because the signal traveled from the trigpin then back to the echo pin, then devide by 29.1 to convert to centimeters

 Serial.print(distance);             //Display the distance on the serial monitor
 Serial.println(" CM");              //in centimeters
 delay(100);                         //delay 100 milliseconds
}

lkram, same thing... Now it's saying everything is 0cm...

Well you could consider the possibility that your sensor is broken. I believe they are inexpensive and you could get another for cheap.

Yet another variation on the loop:

void loop() {
  float duration, distance;
  digitalWrite (trigPin, LOW);
  delayMicroseconds(2); 
  digitalWrite (trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite (trigPin, LOW);
  delayMicroseconds(2);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1; 
  Serial.println(String("duration: " + String(duration) + ", distance: " + String(distance) + " cm."));
  delay(200);
}

10 microseconds is the minimum according to the specifications, so no need to wait longer there. The actual trigger point is the moment trigpin is dropping low, so indeed you can keep that pin high for very long time if you want, it's the falling edge that's important.

If nothing works, it's indeed possible the sensor is at fault.

Well... As I can see it is broken... At the first moment that I tested it I already thought it was broken... But it is pretty cheap here... So the problem is that in my city don't have this type of stores and I need to buy from the internet... So I'll buy probably next month or something like that... Anyways, thank you all!

Conrado