Ultrasound sensor malfunction

Hello
I use an ultrasonic sensor HC-SR05 with my Arduino UNO R3 board. But every time I place an object in front of it, it shows me 0 cm whatever the distance of the object. I used 4 different programs but the board always displays 0 cm.
Please help me.

Here we go again. The problem with your code is on line 23. No?
Well please post the code that doesn't work along with a schematic, hand drawn is fine, a Frtzing layout diagram is not. Also post a picture of your whole setup.

You may want to read this before you proceed:-
how to get the best out of this forum

This is the code :

const byte TRIGGER_PIN = 2; // Broche TRIGGER
const byte ECHO_PIN = 3;    // Broche ECHO
 
const unsigned long MEASURE_TIMEOUT = 25000UL; // 25ms = ~8m à 340m/s

const float SOUND_SPEED = 340.0 / 1000;

void setup() {
  Serial.begin(115200);
  pinMode(TRIGGER_PIN, OUTPUT);
  digitalWrite(TRIGGER_PIN, LOW); // La broche TRIGGER doit être à LOW au repos
  pinMode(ECHO_PIN, INPUT);
}
 
void loop() {
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);
  long measure = pulseIn(ECHO_PIN, HIGH, MEASURE_TIMEOUT);
  float distance_mm = measure / 2.0 * SOUND_SPEED;
  Serial.print(F("Distance: "));
  Serial.print(distance_mm);
  Serial.print(F("mm ("));
  Serial.print(distance_mm / 10.0, 2);
  Serial.print(F("cm, "));
  Serial.print(distance_mm / 1000.0, 2);
  Serial.println(F("m)"));
  delay(500);
}

Where is the rest of the information we need?

You could always try:-

float distance_mm = (float)measure / 2.0 * SOUND_SPEED;

try this sketch and post the serial output

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

const byte TRIGGER_PIN = 2; // Broche TRIGGER
const byte ECHO_PIN = 3;    // Broche ECHO
 
const unsigned long MEASURE_TIMEOUT = 250000UL; // 250ms = ~8m à 340m/s

const float SOUND_SPEED = 340.0 / 1000;

void setup() {
  Serial.begin(115200);
  Serial.println( F("Setup-Start") );
  pinMode(TRIGGER_PIN, OUTPUT);
  digitalWrite(TRIGGER_PIN, LOW); // La broche TRIGGER doit être à LOW au repos
  pinMode(ECHO_PIN, INPUT);
  dbg("setup",SOUND_SPEED);
}
 
void loop() {
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);
  dbg("A",micros());
  unsigned long measure = pulseIn(ECHO_PIN, HIGH, MEASURE_TIMEOUT);
  dbg("B",micros());
  
  dbg("1:",measure);
  float distance_mm = measure / 2.0 * SOUND_SPEED;
  dbg("2:",distance_mm);
  Serial.print(F("Distance: "));
  Serial.print(distance_mm);
  Serial.print(F("mm ("));
  Serial.print(distance_mm / 10.0, 2);
  Serial.print(F("cm, "));
  Serial.print(distance_mm / 1000.0, 2);
  Serial.println(F("m)"));
  delay(500);
}

Hi, @yorig78
Have you tried the NewPing library to do all the hardwork to get the measured distance?

Tom... :smiley: :+1: :coffee: :australia:

I have the same result as before

Thank you for the code but the same measurement in cm/mm (0cm:0mm) is displayed.

The interesting part to

analyse the reasons why

is to post the complete serial output

best regards Stefan

As an exception I will take the picture.
For any future posting post the serial output as a code-section

Sorry @TomGeorge, I test the newping library and is not working.

Can you please post a picture of your project?
Can you please post a circuit diagram?
A hand drawn image will be fine, just make sure you include power supplies, component names and pin labels.

Thanks... Tom... :smiley: :+1: :coffee: :australia:

This is for the 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(115200);
}
 
void loop() {
  delay(50);
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm());
  Serial.println("cm");
}

what does "not working" mean?

If I give you the advice "correct your setup"

Do you think with this "advice" you are able to make it measuring correct distancies?

surely not.

In order to really help you provide the informations you were asked for

along with a schematic, hand drawn is fine, a Fritzing layout diagram is not.

Also post a picture of your whole setup.

best regards Stefan

The serial output shows that it takes 175 to 189 milliseconds to complete the pulseIn-function.

Seems that there is no pulse coming in on the echo-pin

Then I have the same response as before:-

What is the matter? Two others have asked for this as well.
Have you not got access to a camera or your hardware? If so then please say so.

Can't you draw a schematic? If so the project is beyond your ability to do and our ability to help you.

Hi, @yorig78
See post #13.

Tom... :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.