How do I check if ive got a faulty HC-SRO4?

I had an HC-SR04 Sensor delivered today, ive tried to test it but when monitoring serial data i get that its out of range. The TX led flashes on the Arduino but the RX one doesn't. Suggesting its not receiving?

Its wired as follows

VVC - 5v
GND - GND
ECHO - Pin 13
TRIG - Pin 12

and im using this code.

/*
 HC-SR04 Ping distance sensor]
 VCC to arduino 5v GND to arduino GND
 Echo to Arduino pin 13 Trig to Arduino pin 12
 More info at: http://goo.gl/kJ8Gl
 */

#define trigPin 12
#define echoPin 13

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

I'm new to this so before I send it back id like to make sure I havent missed something obvious.

Try to use

digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

instead of your

digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);

You're sending out a signal for far too long, the signal might be back before you try to get it (if your wall is within about 20cm from your sensor).

Hi thanks ive changed the code but im getting this now.

java.io.IOException: Bad file descriptor in nativeavailable
	at gnu.io.RXTXPort.nativeavailable(Native Method)
	at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
	at processing.app.Serial.serialEvent(Serial.java:215)
	at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
	at gnu.io.RXTXPort.eventLoop(Native Method)
	at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)

There is a problem with your serial port, this is not a compile error.

Quit the IDE, disconnect the Arduino, reconnect it again and start the IDE.

If this doesn't help, reboot your PC and try again.

You need to try this new library, it is very nice! http://arduino.cc/forum/index.php/topic,106043.0.html

cyclegadget:
You need to try this new library, it is very nice! http://arduino.cc/forum/index.php/topic,106043.0.html

Very good suggestion! :wink:

Also, I would try using an echo pin other than 13. Because the Arduino has an LED on pin 13, that can cause problems when using pin 13 as an input. Typically, this is not an issue with ultrasonic sensors. But, I've started to see odd things with just some SR04 sensors and I'm starting to guess the problem has to do with using ping 13 for input, not the sensor at all.

Worth a shot, but I'd still use the NewPing library anyway. If for no other reason the one pin mode that's being added in the next release.

Tim

Thanks guys, ill try all suggestions later today and let you know how I get on.

I did try newping but it didnt make much sense to me, im very much a newbie and this is the first thing ive done othr than making an LED flash and other very simple bits.

MancunianLee:
Thanks guys, ill try all suggestions later today and let you know how I get on.

I did try newping but it didnt make much sense to me, im very much a newbie and this is the first thing ive done othr than making an LED flash and other very simple bits.

NewPing has fairly simple examples. But, maybe in my side-quest to steer people to an event-driven programming paradigm I've confused some. Below is an example NewPing sketch using a more common to Arduino, line-by-line with delays which works well for a "hello world" sample:

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     13  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                // Wait 50 milliseconds between pings (ping about 20 times per second).
  int cm = sonar.ping_cm(); // Send out the ping, get the results in centimeters.
  Serial.print("Ping: ");
  Serial.print(cm);         // Print the result (0 = outside the set distance range, no ping echo)
  Serial.println("cm");
}

As you're new to the Arduino I believe the above sketch would make more sense. Let me know if it's not clear. I would suggest, however, that you consider an event-driven paradigm instead of the all too common line-by-line with delays. Line-by-line with delays may be easy to understand and works fine for a example. But, it doesn't work well when you try to build something complex. Changing your mindset now to an event-driven paradigm will make it much easier down the road when you want to incorporate motors, servos and sensors in your project and have your sketch do complex tasks.

Tim

Hi, that sketch is brilliant, I understand every line of it. But its still saying Ping 0cm!

MancunianLee:
Hi, that sketch is brilliant, I understand every line of it. But its still saying Ping 0cm!

What happens if you wire it to pins 11 and 12 (don't use pin 13)? Here's the full sketch:

http://code.google.com/p/arduino-new-ping/wiki/Simple_NewPing_Example

Be sure 12 is wired to trigger and 11 wired to echo, if you switch them, it won't work for sure. Also, 0cm is the correct result if there's nothing within the ping range, so put a box or something foot away. I have 4 SR04 sensors and they "work" with the above sketch. 3 of my SR04 sensors do have a problem where they don't work well beyond about 50cm. It seems to me that this may be a common problem with the SR04, so be sure your test object is fairly close to the sensor.

Also, just to make sure we're both using the same library, attached is my development version of NewPing (v1.4 pre-release). Just replace these files with the ones already in the NewPing folder.

Tim

NewPing.h (7.57 KB)

NewPing.cpp (8.87 KB)