0

I am a front-end developer so I don't really know Arduino. Anyway, my teacher gave me a project.

In short, I have to measure distance with HC-SR04. But the problem is that the teacher wants me to use a timer. But I can't understand how can I do that.

Thank you for helping.

1
  • 2
    It sounds like you are being assigned to read the ATmega data sheet or programmer's manual and make direct use of the timer peripheral, ie, do what the pulseIn code does, but do it yourself. If you ask someone else to give you the answer, how is that more beneficial than just looking at the implementation of the pulseIn function? Commented Jan 7, 2017 at 22:05

2 Answers 2

3

Rather than give you the straight answer in code form, consider this approach. If you learn how the unit works, you will be better prepared to answer questions presented to you.

The background of an ultrasonic distance measuring device is that it sends out a signal, in the form of sound you cannot hear. The device then listens for any echoes (bounce-back) of that pulse within a certain time period. This time period represents the distance range that the device can register. If the electronics are of poor quality, the minimum distance will be larger than if the device was of higher quality, as the amount of time for the pulse to bounce from a close-in object is going to be very short. The electronics have to be very fast to read such a short period of time.

There are libraries for ultrasonic devices such as the one you note, but that's the "easy way" to use it. If you perform appropriate searches, you will find there are references how to use direct timing for this device, which will satisfy the requirement you have been given.

I could perform the search and provide you a link, but that's half the work. Consider to use terms including your device identifier along with code and timing and you may find useful links. It is also very possible that you will find someone's pre-written code using the timing method.

1
  • One feature of the commonly used module is that shortly after the TRIG pin falls, the the module raises it's ECHO pin and holds it HIGH until it detects the end of the echo. The pulse on the ECHO pin isn't the actual echo of the pulse, it is a pulse manufactured by the module which measures from the end of the transmitted pulse to the end of the reflected pulse.
    – Dave X
    Commented May 9 at 13:53
0

The solution is, You need to implement custome pulse in funtion

unsigned long rdPulseIn(int pin, int value, int timeout) { // the following comments assume that we're passing HIGH as value. timeout is in milliseconds

    unsigned long now = micros();
    while(digitalRead(pin) == value) { // wait if pin is already HIGH when the function is called, but timeout if it never goes LOW
        if (micros() - now > (timeout*1000)) {
            return 0;
        }
    }

    now = micros(); // could delete this line if you want only one timeout period from the start until the actual pulse width timing starts
    while (digitalRead(pin) != value) { // pin is LOW, wait for it to go HIGH befor we start timing, but timeout if it never goes HIGH within the timeout period
        if (micros() - now > (timeout*1000)) { 
            return 0;
        }
    }

    now = micros();
    while (digitalRead(pin) == value) { // start timing the HIGH pulse width, but time out if over timeout milliseconds
        if (micros() - now > (timeout*1000)) {
            return 0;
        }
    }
    return micros() - now;
}

You can read distance

long mm = rdPulseIn(echoPin, HIGH, 37)/5.82; // distance in mm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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