Ultrasonic Sensor not working - my fault oder malfunction?

Hi everybody,

yesterday I got my ultrasonic sensor HC-SR04 (from eBay, see here). But I couldn't get it work till now. There is a library from ITead Studios for this 4 pin model (there you find the datasheet, too). Because that one didn't deliver anything else than zeros, I wrote my own sketch for debugging:

int trigger = 5;
int echo = 6;
unsigned int duration;

void setup() {
  Serial.begin(9600);
  pinMode(trigger, OUTPUT);
  digitalWrite(trigger, LOW);
  pinMode(echo, INPUT);
}

void loop() {
  Serial.print("Trigger: ");
  Serial.print(digitalRead(trigger));
  Serial.print(" - ");
  digitalWrite(trigger, HIGH);
  Serial.print(digitalRead(trigger));
  Serial.print(" - ");
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);
  delayMicroseconds(5);
  Serial.println(digitalRead(trigger));
  
  duration = pulseIn(echo, HIGH);
  Serial.print("Echo: ");
  Serial.println(duration);
}

It still doesn't work. The board is a Duemilanove, the triggerPin wired to digital pin 5, the echoPin to 6. Vcc comes from the board, but the board is connected to a 5V power supply (e.g. my LAN breakout board works fine with this solution). I measured 5V stable at the sensor, but there seems to be no current.
Is there something I might have missed? Or is it a malfunction of the sensor?

Thx for your answers and a merry christmas!

The pdf suggests that the device is more like a Ping, with trigger and echo sharing a single pin, but your code is more like it is intended for a SRF04.

I think you just looked at the timing diagram, which is kind of confusing. Look at the photo on the first page, it's the 4 pin type.
Besides, my sketch does the same as the one provided by ITead studio - or actually, I think it does. :smiley:

Remove all those print statements they are screwing your timing.

Grumpy_Mike:
Remove all those print statements they are screwing your timing.

I'm not very optimistic, because as mentioned before, the library (without the monitoring via serial) did not work either. Nevertheless I'll give that a try tomorrow.

But the drawing on the first page suggests three pins.The timing diagram also suggests a thee pin device.
What do the manufacturers suggest the problem is?

But the drawing on the first page suggests three pins.The timing diagram also suggests a thee pin device.
What do the manufacturers suggest the problem is?

AWOL:
But the drawing on the first page suggests three pins.

Had a look at it again and yes, you're right.

The timing diagram also suggests a thee pin device.

That's what I mentioned above. The timing of two pins is drawn in one line. But you can be sure my hardware is of 4 pin type :wink: and the library (have a look at the "documentation" here) is written for that type. If you analyze the lib (I did so) you see this, too.

What do the manufacturers suggest the problem is?

My sensor comes from Hong Kong, I don't believe there is a possibility to contact the manufacturer. I read several forum posts about sensors being defectiv and being replaced by seller - but as I'm a fair buyer, I try to be sure the part is really defectiv and it's not me handling it wrong.

greetz Andromepat

PS: @admin: Just saw I used the wrong forum, please move the thread.

What numbers do you get from this:-

int trigger = 5;
int echo = 6;
unsigned int duration;

void setup() {
  Serial.begin(9600);
  pinMode(trigger, OUTPUT);
  digitalWrite(trigger, LOW);
  pinMode(echo, INPUT);
    digitalWrite(echo, HIGH); // turn on internal pull ups
}

void loop() {
  Serial.print("Trigger: ");
  digitalWrite(trigger, HIGH);
  Serial.print(digitalRead(trigger));
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);
  
  duration = pulseIn(echo, HIGH);
  Serial.print("Echo: ");
  Serial.println(duration);
 delay(800);
}

Are you sure the ground is connected?

but there seems to be no current.

I think you will only see something as you ping and this might be too fast for your meter.

Grumpy_Mike:
Remove all those print statements they are screwing your timing.

Ok, there may have been 2 problems. I removed the debugging statements and now it works fine. Wonder why it did not with the library provided, because that one, of course, didn't include these lines.

So far so good. I made a little test series, using a book holding about 5cm away from the sensor. What I got was a bit sobering: The middle value of all measurements is 4.4cm (see attached pdf). And when nothing in front (it's about 2m to the wall), I get values round 360ms, what means 6 meters.
Edit: As the 360 are microseconds, the distance is 6cm (some additional mistake by 10 :wink: ).

Does anyone know how precise the Ping sensor is? It should be better as it costs 10 times as much as mine!?

thx so far for your help
Andromepat

test_series_us.pdf (87.4 KB)