Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Ultrasonic sensor HC-SR04 always returns 1

Status
Not open for further replies.

ashR

Junior Member level 3
Joined
Sep 27, 2015
Messages
25
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
264
Ultrasonic sensor HC-SR04 always returns 0cm

I am trying to interface Ultrasonic sensor HC-SR04 with arduino. I am providing the circuit diagram and the related code along.

What is bothering me is that it always returns 0cm(Title mentions 1 instead of 0cm, please ignore). I did search online and got to know that there are many people facing the same problem. People are just uploading new complex codes to solve the problem and no one has provided the reason behind the sensors behaving this way.

I can guarantee that my sensors are working fine since i have tried doing the same with new sensors. Sometimes when i burn the code onto the controller, initially it works properly, but then starts returning 0.
ultrasonic_arduino_wiring.png
 

Attachments

  • ultra1.txt
    947 bytes · Views: 135

hello,



it seems , you miss a step in the init of HC-SR04


after arm the trigger ( pulse at status one during 10 µS)
the HC-SR04 will send a salve of pulses (40KHz) during 2+200µS ...
and ONLY AFTER that , you can test pulsin to do time calculation of echo.

**broken link removed**
 

The problem may be multiplying by a floating point number, .034. Try duration/74/2 for inches or duration/29/2 for centimeters.
 

The problem may be multiplying by a floating point number, .034. Try duration/74/2 for inches or duration/29/2 for centimeters.

not working. Actually if found that duration always returns a value 48 sometimes other values near it. 48/29/2 will be less than 0 hence it is giving me 0. But i wonder why!

- - - Updated - - -

- - - Updated - - -

hello,



it seems , you miss a step in the init of HC-SR04


after arm the trigger ( pulse at status one during 10 µS)
the HC-SR04 will send a salve of pulses (40KHz) during 2+200µS ...
and ONLY AFTER that , you can test pulsin to do time calculation of echo.

**broken link removed**


Actually the pulseIn() function takes care of that. The echo pin goes high only after 200+2 us . Even though i haven't considered this delay , the pulseIn() function will start its timer only after Echo goes high(so need not explicity mention the delay) and wait till it goes low.
 
Last edited:

hello


if found that duration always returns a value 48 sometimes other values near it

something is wrong in duration calculus ..
what is the unit of you duration value .. mS, µS
on my application i get 9748 µS for 1.7 meter @ 17°C

i don't know Arduino..
did you check your FOSC value ?
A timer used as counter must be used somewhere to count elementary systeme pulses during the travel of ultrasound

you multiply by 0.034/2 => results in centimeter ?
 

The sketch I used to test the sensor I have is given below. This works fine with the sensor I have.


Code:
/* HC-SR04 Sensor
   https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
  
   This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   The circuit:
	* VCC connection of the sensor attached to +5V
	* GND connection of the sensor attached to ground
	* TRIG connection of the sensor attached to digital pin 2
	* ECHO connection of the sensor attached to digital pin 4


   Original code for Ping))) example was created by David A. Mellis
   Adapted for HC-SR04 by Tautvidas Sipavicius

   This example code is in the public domain.
 */
 
 
const int trigPin = 8; // 2
const int echoPin = 9; // 4
 
void setup() {
  // initialize serial communication:
  Serial.begin(115200);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}
 
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;

- - - Updated - - -

Testing your sketch also works fine with the sensor I have. Perhaps there is an issue with the sensor, connections, etc.
 
Last edited:

I came across an article. So i changed my code accordingly. I am finally getting the distance on serial monitor.

This is what is happening now.

- When there is no obstacle, it keeps on printing the distance depending on how far the wave travels. As soon as i introduce an obstacle, it gives me the distance but then it stops printing values for sometime and doesnt start until i make the obstacle move a bit. Dont know why this is happening.

- When i introduce an obstacle at some distance, say 8cm away. gives this output on the monitor 9 9 9 9 8 8 8 4 4 5 5 9 9 9 9 4 4 5 5 9 9 9 9 4
(not great, since i want to measure the obstacle distance with accuracy. Even though calibration is possible , what bothers me is the erronous readings like 4 and 5 coming in big numbers ).

- As soon as i start taking readings , i hear a ringing sound coming from the sensors. Reason?


Ill post the code in a while. This is the screen
comment.png
shot and link of the article that helped me :-

https://therandomlab.blogspot.in/2015/05/repair-and-solve-faulty-hc-sr04.html

(check the comments section)

- - - Updated - - -


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const int trigPin = 3;
const int echoPin = 2;
 
long duration, distance;
 
void setup()
{
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
digitalWrite(trigPin,LOW);    
digitalWrite(echoPin,LOW);
Serial.begin(9600);
}
 
void loop()
{
 
digitalWrite(trigPin,LOW);    
digitalWrite(echoPin,LOW);
delayMicroseconds(2);
 
digitalWrite(trigPin, HIGH); // We send a 10us pulse
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
 
duration = pulseIn(echoPin, HIGH, 20000); // We wait for the echo to come back, with a timeout of 20ms, which corresponds approximately to 3m
// pulseIn will only return 0 if it timed out. (or if echoPin was already to 1, but it should not happen)
if(duration == 0) // If we timed out
{
pinMode(echoPin, OUTPUT); // Then we set echo pin to output mode
digitalWrite(echoPin, LOW); // We send a LOW pulse to the echo pin
delayMicroseconds(200);
pinMode(echoPin, INPUT); // And finaly we come back to input mode
}
 
distance = (duration/2) / 29.1; // We calculate the distance (sound speed in air is aprox. 291m/s), /2 because of the pulse going and coming
 
if (distance>3) // Sensors are not capable for measuring small distances so i have avoided them 
{
Serial.print("Distance: ");
Serial.println(distance);
}
 
}
 
/*



Description of the code :-

Echo defined as input
Trigger is defined as output

Initially both are low. Once triggered, the module will send 8 pulses of ultrasound and then makes Echo pin high after 2us and starts a timer. Now it will wait till the sound travels back to the module and then makes the echo pin low .

Problem with my earlier code was that for every sound pulse that is sent, a return pulse or echo is not guranteed. So if the module doesn't receive the echo, it fails to make the ECHO pin low and the entire device goes into a wait state. Now no matter what how many times you trigger after this the device wont work until the ECHO pin is made low. The original HC-SR04 is designed to take care of this. But the one i have is most probably a duplicate.

To solve this, the if loop is used that redefines the ECHO pin as ouput, we make the ECHO pin go low and then define it back as input.
For this, we redefine the Echo as output. We manually make it Low and then define back the pin as input so that normal operation can continue.

- - - Updated - - -

The sketch I used to test the sensor I have is given below. This works fine with the sensor I have.


Code:
/* HC-SR04 Sensor
   https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
  
   This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   The circuit:
	* VCC connection of the sensor attached to +5V
	* GND connection of the sensor attached to ground
	* TRIG connection of the sensor attached to digital pin 2
	* ECHO connection of the sensor attached to digital pin 4


   Original code for Ping))) example was created by David A. Mellis
   Adapted for HC-SR04 by Tautvidas Sipavicius

   This example code is in the public domain.
 */
 
 
const int trigPin = 8; // 2
const int echoPin = 9; // 4
 
void setup() {
  // initialize serial communication:
  Serial.begin(115200);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}
 
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;

- - - Updated - - -

Testing your sketch also works fine with the sensor I have. Perhaps there is an issue with the sensor, connections, etc.

hey,

Can you tell me how good your sensors are working? I mean say you have an obstacle at 12cm from the module. Can you provide me with the values of distance that gets printed onto the monitor. Just curious about the accuracy of these modules.
 
Last edited by a moderator:

The sensor I have gives the distance fairly accurately at distances form 3 to 11 cm, using your original sketch. Beyond that distance it seems to read the distance as too long. At 20 cm it returns 24 cm. The distance it gives seems to represent the distance from the circuit board of the sensor rather than the front screen.
 

The sensor I have gives the distance fairly accurately at distances form 3 to 11 cm, using your original sketch.

What about the results using your sketch, are they same?
 

hello,


.... (sound speed in air is aprox. 291m/s)
??
not 340 m/sec at 20°C ?

in your main loop,
i didn't see any delay to avoid bad echo detection
maybe a delay of 200mS between each measure could improve it ..

you can also add temperature measurement to correct air speed factor.

On industrial ultrasonic sensor, a tempearture sensor is included to compensate temp effect on the distance measured.
ex: Baumer ultrasonic sensor used to measure more than 1 meter .. accuracy +-2mm
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top