HC-SR04 Ranging always return 0

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:

#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

Does this work?

/*
 * 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);
  }

After downloading the library created by cnobile the code runs, but only prints 0:

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

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

the solution to the sensor being stuck at zero is in this link. its the 2. post, by docdoc. You will need the NewPing library which is far better.

A working code:

#include <NewPing.h>

#define TRIGGER_PIN 12

#define ECHO_PIN 11

#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {

Serial.begin(9600);

}

void loop() {

delay(50);

unsigned int uS = sonar.ping();

pinMode(ECHO_PIN,OUTPUT);

digitalWrite(ECHO_PIN,LOW);

pinMode(ECHO_PIN,INPUT);

Serial.print("Ping: ");

Serial.print(uS / US_ROUNDTRIP_CM);

Serial.println("cm");

}

link: http://forum.arduino.cc/index.php?topic=55119.15

NewPing link: Arduino Playground - NewPing Library

mzbas:
the solution to the sensor being stuck at zero is in this link. its the 2. post, by docdoc. You will need the NewPing library which is far better.

A working code:

...................................../quote]
@mzbas, how about posting your code between code tags in future?

I've just seen two posts by you in a row in which you posted this code inline without code tags.
Not good. :frowning:

Did you happen to notice that everyone else in this thread took the trouble to do it properly?