Pages: [1]   Go Down
Author Topic: HC-SR04 Ranging always return 0  (Read 610 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I'm trying to use Ultrasonic Ranging Module HC-SR04 with a Arduino UNO, I downloaded the library from ITed and tested with the following code:
Code:
#include "Ultrasonic.h"

Ultrasonic ultrasonic(12,13);

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

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

My cables are connected this way:
GND -> GND (Power)
ECHO -> 13 (Digital)
TRIG -> 12 (Digital)
VCC -> 5V (Power)

The problem is that it always print 0, either there is a object next to it or nothing. What can I do?
Thanks in advance
Logged

Offline Offline
Faraday Member
**
Karma: 68
Posts: 2813
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this work?
Code:
/*
 * 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);
  }
Logged

The way you have it in your schematic isn't the same as how you have it wired up! That goes for me too.

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

After downloading the library created by cnobile the code runs, but only prints 0:
Code:
MS: 0, CM: 0.00, IN: 0.00

@edit
I noticied that when I disconnect and connect again the echo pin in runtime sometimes it returns a value (but a very high value), but I tried to change the echo cable and the same problem continues
« Last Edit: July 24, 2013, 04:39:31 pm by KirieZ » Logged

Offline Offline
Faraday Member
**
Karma: 68
Posts: 2813
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the voltage measured from +5 to GND at the sensor?
Show us a good picture of the wiring.
Logged

The way you have it in your schematic isn't the same as how you have it wired up! That goes for me too.

Pages: [1]   Go Up
Jump to: