Pages: [1]   Go Down
Author Topic: Ultrasonic Sensor not working - my fault oder malfunction?  (Read 2270 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26621
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. :-D
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34724
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Remove all those print statements they are screwing your timing.
Logged

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26621
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26621
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But the drawing on the first page suggests three pins.
Had a look at it again and yes, you're right.
Quote
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 smiley-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.
Quote
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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34724
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What numbers do you get from this:-
Code:
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?
Quote
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.
« Last Edit: December 25, 2011, 12:54:02 am by Grumpy_Mike » Logged

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-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.35 KB - downloaded 24 times.)
« Last Edit: December 25, 2011, 02:55:30 pm by Andromepat » Logged

Pages: [1]   Go Up
Jump to: