Figuring out how to use the HC-SR04 Ultrasonic sensor


#1

Hi again,

as I am again trying to hack my way into the HC-SR04 ultrasonic sensor, I’ve had modest success in converting the MicroPython library hcsr04.py for my needs. I’ve stumbled now on this line in it, which is crucial apparently:

    try:
        pulse_time = machine.time_pulse_us(self.echo, 1, self.echo_timeout_us)
        return pulse_time

This bit returns the time delay before the Echo pin goes to 1.

The question is, how does one implement this with HAL in Zerynth?

the full file


#2

Hi @Heikki_Hietala,

very nice, when the library is completed it would be nice if you publish it as a community one :slight_smile:
For what concerns the time_pulse_us, I think you should take a look at the Input Capture Unit module (not available at the moment on ESP32).
On which board are you testing your port?


#3

Hi! I have a five ESP32 WROOM DevKitC’s.

I found that ICU by chance the other day and since I am rather dense when it comes to programming, I have not been able to figure out how to use it in the manner of the Arduino example.

When you say it’s not implemented yet on ESP, it get slightly more complicated for me :thinking: :stuck_out_tongue_winking_eye:

I would like to keep working on Zerynth, because the interface is so well done. I did install the tool chain for using Visual Code and Micropython and all that, so as to use the ready made libraries, but it’s so much more complex than Zerynth. But as I need to get the ultrasound part to work, I need to keep looking at that too.

In case you wonder what I am building, it’s an Internet aware version of my Morse Moai statue. Now it runs on Arduino Nano, but I want to be able to change the text via the Web.

See http://www.instructables.com/id/The-Morse-Moai-Statue/ for details.


#4

Hi @Heikki_Hietala,

thank you for appreciating our work and for sharing your project.
It would be really nice to see it completed and working.

Can you tell me if expected pulse time is in the order of hundreds of microseconds or just a few?


#5

Oh absolutely. For someone like me, with no experience on Python or flashing with a Python toolchain (I am an Arduino guy) Zerynth has already surpassed my expectations.

What I have now is this in Arduino:

digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = (duration/2) / 29.1;

This gets me a centimeter accuracy distance, which works fine.

Ideally I could use the same structure in Python, but am unable to get that pulseIn to work.

I will certainly share the final system once I get it done, so that I can use the ESP32 to read messages from a website on WLAN, watch if someone comes closer than 60cm, then show them the message on an LCD and morse it out visually and aurally, just as the Morse Moai now does. It has no Internet access, so that’s why I am moving to ESP2 and Python.


#6

Hi @Heikki_Hietala,

unfortunately to correctly handle microseconds (without ICU), you have to write a small C function where you can call the vosTicks function and convert it to current number of Microseconds passed from startup with *vosTicks()*(_system_frequency/1000000);.
Then you could try to poll for a change in pin value (or better you could place a callback on it even if it’s a bit more difficult to implement) and get the time in micros it took to change it.
After this you will be able to convert that time to a distance.

Let me know :slight_smile:


#7

Okay, this is news, I haven’t combined Python and C yet. But what you are saying is that there is a way to get that pulseIn done on my DevKitC and Zerynth?

While I am not much of a programmer, I will keep at this until I get it done. Many thanks for all the help.


#8

Yes, it may not be the cleanest or most efficient way, but it should work.
Let me know if you need further help :slight_smile:


#9

A quick example of that C + Python arrangement would be nice.


#10

You can start with the C Language Interface example to be cloned from Zerynth Studio


#11

Ah! Of course, sorry for the bother :slight_smile: Man, that looks… interesting. But it’s all a learning experience.