How do I measure a pulse width with microsecond precision?

minetest2048
Posts: 2
Joined: Thu Oct 31, 2019 7:56 am

How do I measure a pulse width with microsecond precision?

Postby minetest2048 » Wed Nov 13, 2019 9:17 pm

I want to interface a HC-SR04 ultrasonic sensor with ESP32, and to do that, I need to measure the pulse width of the ultrasonic sensor to microsecond precision, and in Arduino there is pulseIn() function which do exactly that. The problem is that this is for a university course and Arduino libraries are banned, so I need to find a way to implement pulseIn in ESP-IDF.

The function need to measure the width of a single pulse, and a timeout if there is no pulse detected. I'm currently thinking to use RMT because there is this thread: viewtopic.php?t=1772 but somewhere I also read that MCPWM capture unit also works, and I'm not sure whether using timers and interrupts also work. In the Arduino pulseIn implementation I think they use assembly code to do cycle counting, but from multiple threads and google it seems that ESP32 assembly is proprietary. Is there any way to do this, or any working code?

mikemoy
Posts: 627
Joined: Fri Jan 12, 2018 9:10 pm

Re: How do I measure a pulse width with microsecond precision?

Postby mikemoy » Thu Nov 14, 2019 5:56 am

This is where I would start.
https://github.com/espressif/esp-idf/tr ... sic_config

In mcpwm_basic_config_example.c, you will see "static void disp_captured_signal(void *arg)" function. I would pick that apart to lean how it works. Then you should be able to time your pulses and get your timing.

Who is online

Users browsing this forum: Baidu [Spider], ok-home and 68 guests