Running two ultrasound detections independently on one ESP32

heikki.hietala
Posts: 17
Joined: Tue Sep 04, 2018 6:47 am

Running two ultrasound detections independently on one ESP32

Postby heikki.hietala » Sun Dec 13, 2020 10:16 am

Hi all,

I have a question on using two HC-SR04s independently.

Our University has two doors and we want to count people going in and out. Doing one door is rather easy with two HC-SR04s sensing distance change and checking the millis when this occurred, and that is no problem to decipher which direction the person went.

But when I try to think of a way of running two such dual HC-SR04s so as to have one device manage both doors, I seem to run aground when I try to think of them operating independently so that both sensing units could be detecting at the some time. Parallel loops don't seem readily available in C.

Is Python a better choice for this?

Thanks for your time.

davidzuhn
Posts: 17
Joined: Fri Sep 20, 2019 1:50 am

Re: Running two ultrasound detections independently on one ESP32

Postby davidzuhn » Sun Dec 13, 2020 6:27 pm

Parallel loops are indeed possible, but it's done through FreeRTOS constructs instead of being built in to C.

You can start up multiple tasks, each of which is running their own main processing loop. If you have one for each of the two devices you're managing, you can take the results from each of these tasks and pass them into a data handling task (probably using a queue or ESP-IDF events) for doing whatever you do with the results (save them, pass them upstream to another device, whatever).

Take care to use task local storage and not globals, or else you'll have a lot of confusion as the different tasks run roughshod over the globals without any coordination.

This case is pretty simple -- I suspect you'll be running the exact same code in each of the two detector tasks, one each controlling a specific device. Take a look at examples/system/esp_event/user_event_loops for how to get the different loops running. Your version of "application_task" can do more than just run the event loop processor and sleep.

Are your two ultrasonic detectors able to work in the same physical space without interfering with each other?

heikki.hietala
Posts: 17
Joined: Tue Sep 04, 2018 6:47 am

Re: Running two ultrasound detections independently on one ESP32

Postby heikki.hietala » Sun Dec 13, 2020 6:36 pm

This looks very promising indeed, many thanks for your reply!

The two sets of ultrasound sensors are in two separate doorways in a back to back layout. I will look into the example code in the morning.

I figured out a backup plan of using two Arduinos to handle the measurements, and passing their data onto the ESP32 via two serial ports.

Thanks again!

becorey
Posts: 92
Joined: Sat Mar 28, 2020 4:18 pm

Re: Running two ultrasound detections independently on one ESP32

Postby becorey » Sun Dec 13, 2020 9:20 pm

You could use an interrupt for each ultrasonic device. In the interrupt code, just note the timestamp (in ms or us) for that device. Then in the code main loop, check those timestamps. Should be no problem for 4 sensors.

Who is online

Users browsing this forum: No registered users and 42 guests