cant get the HC-SR04 ulstrasonic sensor to work, need help.

It has been working before, but now I have tested it first with my main program where I am trying to measure the volume in a reservoir and it didn't work.Then I created a second program for the purpose of only testing the device and the only thing I am getting for distance is 0.00. I really do not understand.Perhaps the sensor is damaged?
See below my test program for the sensor:

int trigPin=12;
int echoPin=10;
long soundSpeed=343;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
  float duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // put your main code here, to run repeatedly:
 duration = pulseIn(echoPin, HIGH);//value is in microsconds
 duration=duration/1000000;//convert to seconds
 distance = (duration / 2) * soundSpeed;
 Serial.print(distance*100);//convert from meters to centimeters
 delay (100);
}

Does your code print something every 100mS or once every second (the default timeout for pulseIn() ) ?

I got mine working with code here see post #71 . I had to solve a missing timeout problem which some varieties of these sensors demonstrate.

Why don't you print the value of "duration"?

Why is an integer number of microseconds represented by a float?

6v6gt:
Does your code print something every 100mS or once every second (the default timeout for pulseIn() ) ?

I got mine working with code here see post #71 . I had to solve a missing timeout problem which some varieties of these sensors demonstrate.

I pasted your code right into my sketch and got only this out: wait1 out of range

auting82:
I pasted your code right into my sketch and got only this out: wait1 out of range

In that case, it got nothing back on the echo pin (within a reasonable period of time).

Are you using exactly the same sensor you used when you said that it was working? There is quite a variety of those sensors and that thread in which my post appeared is full of tricks for getting these to work, including small hardware modifications.

And, as suggested above, print the value of duration (the one directly derived from the pulseIn() call).

I got this code to work after some rewiring of the cables, I either lost connection or something like that. I really don’t know why it stopped working.Now its finally working again.
This is the code that I got working:

#define seconds_in_ms(s) ((s)*1000UL)
#define minutes_in_ms(m) ((m)*60UL*1000UL)
#define hours_in_ms(h)   ((h)*60UL*60UL*1000UL)
#define days_in_ms(d)    ((d)*24UL*60UL*60UL*1000UL)
#define weeks_in_ms(w)   ((w)*7UL*24UL*60UL*60UL*1000UL)

#include <dht11.h>
#include <PhotoResistor.h>
#include <SoilMoisture.h>
#include <Ethernet.h>
#include <Time.h>

#define trigPin 12
#define echoPin 13


 float radius=4.7;  // radius of reservoir bottle
float volume; // volume of the reservoir
float reservoir_height=15.8;
float sensor_poistion=21;
float bottleshape=200;// take in account loss of volume(not a straight cylinder) because of the coca-cola bottle shape


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


void loop() //Main program
{
volume=volume*1;
float duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);

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

  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
  volume= ((sensor_poistion-distance)*3.14159*radius*radius)-bottleshape;
  Serial.println(distance);
  if (distance >= 400 || distance <= 2){
    Serial.print("Distance = ");
    Serial.println("Out of range");
  }
   else if (distance<400 || distance > 2 ){
     Serial.println(distance);
   }
   
  }

The confusing part is I dont undertand why it wasnt working before. I dont have an definite precise answer :o .Its the same code :confused:

The confusing part is I dont undertand why it wasnt working before. I dont have an definite precise answer :o .Its the same code :confused:

Maybe the answer is that in the code that fails, the echo pin is defined as 10 and in the working code, it is defined as 13.