How to drive HC-SR04 Ultrasonic Ranging Module on arduino Uno(avr-hal)?

Hi, I'm a beginner at embedded rust and I'm trying to drive a Ultrasonic Ranging Module. I found it's pretty simple to achive this in arduinoC. However, I can't find a function similar to pulseIn in avr-hal. In other words, I can't find a way to time the singal in microsecond. Please help me, thanks.

Here is the datasheet of HC-SR04

1 Like

You can start a new timer when the echo pin goes HIGH, read the timer when the pin goes LOW, and convert the timer count to microseconds. I believe that’s essentially what Arduino’s pulseIn function does.

Thanks for help. However, I found that the minimum precision of the timer is one millisecond, and sometimes the interval can be less than 1 ms.

In this case, the "timer" refers to a hardware timer/counter peripheral on your microcontroller, which counts ticks of the system clock. You can convert those ticks into microseconds.

It looks like the avr-hal project has an example demonstrating how to control the HC-SR04 sensor. That might be helpful for you.

How fool I am. I didn't notice that there even a official example of this sensor. :sweat_smile:

Thanks for being patient on such a silly issue.

1 Like