Go Down

Topic: HC-SR04 Ranging always return 0 (Read 838 times) previous topic - next topic

KirieZ

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: [Select]
#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

LarryD

Does this work?
Code: [Select]

/*
* 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);
  }
The way you have it in your schematic isn't the same as how you have it wired up!

KirieZ

#2
Jul 24, 2013, 10:11 pm Last Edit: Jul 24, 2013, 11:39 pm by KirieZ Reason: 1
After downloading the library created by cnobile the code runs, but only prints 0:
Code: [Select]
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

LarryD

What is the voltage measured from +5 to GND at the sensor?
Show us a good picture of the wiring.
The way you have it in your schematic isn't the same as how you have it wired up!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy