0

I am working on HC-SR04 ultrasonic sensor for my project. I made the connections given here. Here is my code

int main(){
int count = 0;

if (gpioInitialise() < 0)
{
    qDebug() << "ultrasound gpio initialise Fail!!!";
    //return -1;
}

gpioSetMode(ECHO, PI_INPUT);
gpioSetMode(TRIG, PI_OUTPUT);

while(1)
{
    ultra_distance = getCM();
    qDebug() << "Distance : " << ultra_distance << " --- " << ++count;
}
return 0;
}

The getCM() function is

    int dist;
    int startTime,stopTime;

    gpioWrite(TRIG, 0);

    gpioDelay(1000000);

    gpioWrite(TRIG, 1);

    gpioDelay(10);

    gpioWrite(TRIG, 0);

    while(gpioRead(ECHO) == 0)
    {
        startTime = gpioTick();
    }

    while(gpioRead(ECHO) == 1)
    {
        stopTime = gpioTick();
    }

    dist = ((stopTime - startTime)/29/2);

    return dist;

When I tried this, I get reading for first 10-15 iterations and then it freezes. I tried changing the sensor from a good HC-SR04 to a good JSN HC-SR04. But it still didn't work. Is there any permanent solution to this in c++?

Thanks!

  • Can you be more specific about where or how it freezes? Did you try attaching a debugger to step through? – Brick Dec 20 '18 at 14:42
  • Have you got a voltage divider between your HC-SR04 and your Raspberry because the SR04 runs at 5V and the echo pin will kill your Raspberry stone dead unless you divide it down from 5V to 3.3V. – Dougie Dec 20 '18 at 15:19
  • does the sensor timeout and toggle ECHO even if it never gets a response back from its signal? You may want to have your loop be checking how long its been since starttime, and bail out if its too long. This will let it come back around and try again. – Chad G Dec 20 '18 at 16:23
1

This appears to be an adaptation of a buggy Python script.

Personally I'd use it as a test to distinguish between people who can program and people who can script.

while(gpioRead(ECHO) == 0)
{
    startTime = gpioTick();
}

while(gpioRead(ECHO) == 1)
{
    stopTime = gpioTick();
}

What is meant to happen is a sonar trigger is sent and then ECHO remains high until the return pulse is received. The duration of ECHO being high indicates twice the transit time.

The expectation is that the first loop is reached while ECHO is high. That would normally be true but in Linux the program might have been delayed. In that case the returned pulse has already been received and ECHO is and remains low. That means the loop spins forever. There are similar problems with the second while loop. You need to timeout each loop.

  • I did the timeout exit of loop by using a variable and increment it in each iteration of the while loop. Then if the value of variable exceeds a certain threshold then it exits the while loop and proceeds to the next part of the program. Even after doing that I don't get correct values. Rather it just takes some random very high value. If you have any links that can help me correct my mistake, sharing it would be awesome. Btw, I wrote this program from a previously working Arduino sketch that I had. – Aditya Dec 20 '18 at 17:56
  • +1 for this part: "Personally I'd use it as a test to distinguish between people who can program and people who can script." – Brick Dec 21 '18 at 0:21
  • @Aditya Can you update your question with your amended code that is still not working? – Roger Jones Dec 21 '18 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.