Pages: [1]   Go Down
Author Topic: Help with HC-SR04 Ultrasonic Range Finder  (Read 2329 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just received my HC-SR04 from Hong-Kong and I am having trouble getting it to work with my ATMega2560.  The ATMega2560 is connected to my laptop via USB, and the HC-SR04 is using 5V and ground from the Arduino for power.  I have the trigger connected to Pin 2 and the echo to pin 3.  I have the Ultrasonic library downloaded from ITead and I changed their example a little bit because I don't have an LCD.

I have tried both of the following code, and in both cases I get nothing but "0 CM" being printed to the serial monitor.
Code:
double ping(int outPin, int inPin) //Get CM to obstacle in front of the sensor
{
  long duration;
  pinMode(outPin, OUTPUT);
  pinMode(inPin, INPUT);
  digitalWrite(outPin, LOW);
  delayMicroseconds(2);
  digitalWrite(outPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(outPin, LOW);
  delayMicroseconds(10);
  duration = pulseIn(inPin, HIGH);
  return duration / 29.0 / 2.0;
}

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

void loop()  
{
  Serial.print(ping(2, 3)); Serial.println("cm");
  delay(500);
}

Code:
#include "Ultrasonic.h"
Ultrasonic ultrasonic(2,3);

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

void loop()
{
  Serial.print(ultrasonic.Ranging(CM)); Serial.println("cm");
  delay(100);
}

I read that sometimes people have an issue with the HC-SR04 if you start it up with nothing out in front of it to echo.  I tried with and without a barrier in front and both ways I still only get "0 CM" on the monitor.

Does anyone have any ideas on what I may be doing wrong?  Also is there any way to test the HC-SR04 to know if it is defective?

Please let me know if I need to provide any additional info.

Thanks a lot.

Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 168
Posts: 12431
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

no such sensor but try this
Code:
#include "Ultrasonic.h"

Ultrasonic ultrasonic(2,3);

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

void loop()
{
  float x = ultrasonic.Ranging(CM);
  Serial.print(x,4);
  Serial.println("cm");
  delay(100);
}

What datatype does ultrasonic.Ranging(CM) return?


an the other app similar

Code:
double ping(int outPin, int inPin) //Get CM to obstacle in front of the sensor
{
  long duration;
  pinMode(outPin, OUTPUT);
  pinMode(inPin, INPUT);
  digitalWrite(outPin, LOW);
  delayMicroseconds(2);
  digitalWrite(outPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(outPin, LOW);
  delayMicroseconds(10);
  duration = pulseIn(inPin, HIGH);
  return duration / 29.0 / 2.0;
}

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

void loop()  
{
  float dist = ping(2,3);
  Serial.print(dist, 4);
  Serial.println("cm");
  delay(500);
}


Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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

I returned the first HC-SR04 and the ebay seller from Hong Kong sent me a second one to try.  The new one works great and I can say that both versions of the code above will work with the HC-SR04.

Logged

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

The datasheet looks a bit confused to me.

On the face of it, it looks like a cheap knock-off of Devantech's device (It has a two pin interface with "trig" and "echo"), but the example code looks more like it is a cheap knock-off of the Ping sensor, with a single pin interface.

I don't have one, so I'm going to remain confused.
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.

Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I try it and this error shows:



.cpp:1:24: error: Ultrasonic.h: No such file or directory
:1: error: 'Ultrasonic' does not name a type
.cpp: In function 'void loop()':
:10: error: 'ultrasonic' was not declared in this scope
:10: error: 'CM' was not declared in this scope

no clue, can anyone help?
Logged

Pages: [1]   Go Up
Jump to: