Ultrasonic sensor HC-SR04

I am trying to interface HC-SR04 ultrasonic sensor with arduino. I am making use of the NewPing Library.But then on the serial monitor, it keeps printing some unknown characters and not the distance.
Please help.

What is the serial monitor line speed set to? What is the unposted sketch's line speed set to?

I never use the ping lib. These's no need to just a bit of simple coding does it every time. Try this code remember to change the pins as needed to suit you set up. Let me know how it goes..

Hope it helps, regards.

Mel.

/------------------------------------------------------------------
long scanner(long cm)
{
    const int pingPin=7, EchoPin=8;
    long duration;

    // The SRF005 is triggered by a HIGH pulse of 2 or more microseconds.
    // Give a short LOW pulse before to ensure a clean HIGH pulse:
    pinMode(pingPin, OUTPUT);
    pinMode(EchoPin, INPUT);  

    digitalWrite(pingPin, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin, HIGH);
    delayMicroseconds(2);
    digitalWrite(pingPin, LOW); 
    duration = pulseIn(EchoPin, HIGH);
    delay(100);

    // convert the time into a distance
    // inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration);
    return (cm);
}
//------------------------------------------------------------------
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;
}
//------------------------------------------------------------------

Cactusface: I never use the ping lib.

Neither does the OP.

AWOL: What is the serial monitor line speed set to? What is the unposted sketch's line speed set to?

Thanks, a made a mistake here. Serial montor was set to 9600. But now it gives me 0cm on monitor.

Cactusface: I never use the ping lib. These's no need to just a bit of simple coding does it every time. Try this code remember to change the pins as needed to suit you set up. Let me know how it goes..

Hope it helps, regards.

Mel.

/------------------------------------------------------------------
long scanner(long cm)
{
    const int pingPin=7, EchoPin=8;
    long duration;
// The SRF005 is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse before to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
pinMode(EchoPin, INPUT);  

digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(2);
digitalWrite(pingPin, LOW); 
duration = pulseIn(EchoPin, HIGH);
delay(100);

// convert the time into a distance
// inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
return (cm);

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

I did try to code it myself. But then the monitor always returns 0cm.When i searched online i got to know that there are many people facing the same problem. Some guys were suggesting to use this NewPing library. Now after making corrections to buad rate, i am back to square one as the monitor is again showing 0cm

Hi, Then it must be your code or your wiring, check that ping and echo are on the right I/O pins, not all HC-04 type sensors have the same signals on the same pins..

@Awol ping newping both basicly the same!!

Regards

Mel.

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?

I am attaching the code, along with the screen shot and link of the article that helped me :-

(check the comments section)

ultra_2.ino (1.21 KB)

Cactusface: @Awol ping newping both basicly the same!!

Nope.

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

I normally just hear a click.

Why don't you post your code?

AWOL: I normally just hear a click.

Why don't you post your code?

I have check the file ultra_2.ino

Ashwinr: I have check the file ultra_2.ino

Why don't you just post your code?

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

}

What do you guys feel about the code and the problem of errorneous reading?

Hi, The pulse you're sending out is TOO long! only 2uS needed also I'm not sure but think ECHO should be HIGH and goes LOW, at the end of reading...

digitalWrite(trigPin,LOW); digitalWrite(echoPin,LOW); delayMicroseconds(2);

digitalWrite(trigPin, HIGH); // We send a 10us pulse delayMicroseconds(10); digitalWrite(trigPin, LOW);

Hope it helps,Regards.

Mel.

pinMode(echoPin, OUTPUT); // Then we set echo pin to output mode
digitalWrite(echoPin, LOW); // We send a LOW pulse to the echo pin

What’s that?

Please remember to use code tags when posting code

Cactusface: Hi, The pulse you're sending out is TOO long! only 2uS needed also I'm not sure but think ECHO should be HIGH and goes LOW, at the end of reading...

digitalWrite(trigPin,LOW); digitalWrite(echoPin,LOW); delayMicroseconds(2);

digitalWrite(trigPin, HIGH); // We send a 10us pulse delayMicroseconds(10); digitalWrite(trigPin, LOW);

Hope it helps,Regards.

Mel.

According to all whatever i have read online, trigger pulse should be no less than 10us. Echo should initially remain 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.

For that, 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.

AWOL: pinMode(echoPin, OUTPUT); // Then we set echo pin to output mode digitalWrite(echoPin, LOW); // We send a LOW pulse to the echo pin

What's that?

Please remember to use code tags when posting code

Description of the code :-

Echo defined as input Trigger is defined as output

initially both are low. Then we provide trigger. wait for the echo. Suppose echo doesn't come, the device suffers from time out. 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.

Read my reply to Cactusface.

Are you comfortable with driving another microcontroller's output pin low?

AWOL:
Are you comfortable with driving another microcontroller’s output pin low?

did not get your question.