Library for HC-SR04

Hello there
I’m trying to use an ultrasonic device (HC-SR04) with my esp32 in zerynth studio.
I have tried several workarounds but I wasn’t able to use pulseIn function (which works under c compilers). Unfortunately esp32 under zerynth environment isn’t compatible with icu and I wasn’t successful in finding any library working under zerynth with esp32.
Could you help me?
Thanks in advance
Alessandro

Hi @Alessandro1
Since the ICU is not yet implemented for the ESP32 based boards, It will be tricky to calculate the appropriate time delay between the pulses and to calculate the distance.
Without the ICU, these calculations would use the ticks done by the OS to roughly measure the time.
If you are interested, Here is a simple function to have a delay in C.

static void poll_delay(uint32_t ticks) {
    // polled delay
    volatile uint32_t *now = vosTicks();
    uint32_t start = *now;
    uint32_t end = start+ticks;
    if(end>start) {
    	while(*now>=start && *now<end);
    } else {
    	while(*now>=start || *now<end);
    }
}

Using the same idea, you can measure the time of Flight and calculate the distance.
Let me know what you think.

Thanks for your suggestion, I was successful in writing a distance calculator function, the only issue I’m encountering is in the use of microseconds, from HC-SR04 datasheet it is clear that I have to use IO trigger for at least 10us high level signal. Since I’m using an esp32 I red that microseconds are not supported but using sleep(“number”,MICROS) my code works (even thought I’m not sure about its precision) while using MILLIS it doesn’t work.
Here there is my code

def read_distance():

digitalWrite(trigger,HIGH) 
sleep(10,MICROS)
digitalWrite(trigger,LOW)
counter = 0
while digitalRead(echo) == LOW:
    sleep(10,MICROS)
    counter += 1
    if counter >= 2500:
        return -1

counter = 0
while digitalRead(echo) == HIGH:
    sleep(10,MICROS)
    counter += 1
    if counter >= 3800:
        break
    
return counter/5.8

If you want to sleep for 10 Milli seconds, you can use sleep without the other argument:
sleep(1000) #sleeps 1000 milliseconds.
But I am not sure I understand the problem, if the problem is not resolved, could you tell me again which part of code that throws errors.

Sorry I wasn’t very clear with my previous answer. The code works just fine, but my device (esp32) doesn’t support microseconds apparently, so I was looking for another way to implement a sleep with microseconds since I’m not sure that using sleep(10,MICROS) really waits 10 microseconds before continuing with the rest of the code

Since the MICROS is not yet supported with ESP32.
You can implement a polling delay by using the vosTicks() method.

static void poll_delay(uint32_t ticks) {
    // polled delay
    volatile uint32_t *now = vosTicks();
    uint32_t start = *now;
    uint32_t end = start+ticks;
    if(end>start) {
    	while(*now>=start && *now<end);
    } else {
    	while(*now>=start || *now<end);
    }
}

In a c file, Make a function that calls this Macro or implement a similar function then call this function from your python code. (as in C language interface example)
But you should make an evaluation of the right ticks value, check it with an oscilloscope for better timing.