HC SR04 + NodeMCU without delay function

Hi folks, I am working with a HC-SR04 sensor and a nodemcu board, want to share my experience.

My experience was that using the most basic sketch, the board’s behavior is not the most appropiate, watching the serial monitor, it seems to be “lagged” on certain opportunities.

I thougth that can be the sketch, and reading it, I think that the “delay” function can be doing something weird with thinger communication, so searching for a way to make the measurements without use of “delay” function, found the “new ping” library, specially for this kind of sensor, now the board is working flawlessly.

So I want to take advantage of the opportunity and ask, are there knowed limitations with the use of “delay” function and thinger communication? (the delay used in the original sketch was about 10 microseconds, yes, microseconds not milliseconds :worried: )

The link to download the library:
https://bitbucket.org/teckel12/arduino-new-ping/downloads/

It has a simple example… pretty simple and easy to use, and have a example to handle 15 sensor, simultaneously! :wink:

Bests

Hi @ega!

the delays inside the loop, effectively takes effect on the behaviour and on the lags. Just think that any delay inside the loop, will stop any I/O communication, so, the platform cannot communicate with the platform properly and any request will be delayed. 10 microseconds is just nothing, but for sure the library will be doing some kind of while waiting the echo response (accumulating several 10 microseconds delays), so it will be useful to measure how many time a ping is taking by adding a millis before and after de ping. I suppose, that depending on the measured distance, the ping will take more or less…

So, the approach without delays will definitely provide much more performance, as the micro can handle communications while waiting for the echo response.