d03n3rfr1tz3 / HC-SR04

Library for measuring distance with the HC-SR04 UltraSonic Sensor, by utilizing temperature correction and multiple echo pins in parallel.
https://www.arduino.cc/reference/en/libraries/hc-sr04/
MIT License
40 stars 6 forks source link

Slow down program when ce cover sensor #4

Closed sp3swj closed 10 months ago

sp3swj commented 10 months ago

Normally functinon works fine, even with 10 ms "delay" but when we have big distance, or closed sensor by hand - proces slow down CPU cycle to about 150 ms

https://youtube.com/shorts/To0vZjwTudM

ESP32 S3 - 240 MHz

d03n3rfr1tz3 commented 10 months ago

thats very likely because of timing out. the library sends a short signal to the sensor to initiate a ultrasonic pulse. then it waits for the pulse to travel and come back. at close range it comes back a few milliseconds earlier then at further range. the measurement method call is blocking while that happens.

problem is, when it does not recognize the reflected pulse. which can happen, if the reflecting object is too close or too far (see datasheet of your sensor), because then it waits until a specified timeout. the default timeout is 100ms, which is roughly 17 meters traveled distance, which means its slightly over the typical max range of 7-8 meters of most sensors (8 meters to the reflecting object and 8 meters back = 16 meters the pulse has to travel). you can lower the timeout, but be aware that a shorter timeout might lead to unexpected measurements. its theoretically possible, that with for example a timeout of 50ms, the pulse comes back right after a second pulse was sent (reflecting object at distance of about 4-8 meters), which would trick the library into thinking the second signal just came back very quickly and therefore giving out a result with a too short distance. afaik its not possible to reduce the pulse strength or send a different pulse (that could differentiate the first and second pulse).

if you need measurements not blocking your code, you would have to move it into a separate task. if you need a different range (e.g. shorter range), you need a different sensor. for shorter range I can recommend ToF (time of flight) sensors, which are based on light instead of ultrasonic sound, which obviously travels faster.

sp3swj commented 10 months ago

:-) thank you for your answer

Of course, I know how this sensor works and what the physics entails, but I did not look in detail at the code you wrote. I like idea to return "-1" as "out of range".

I think it may be worth including in the library or somewhere information that it "may cause a slowdown in the program code" and whether it should be used with caution.

The light-based sensor is ordered and on the way to me :-)

I was also thinking about narrowing down the operation by program limit (exact as you proposed) because, for example, I am interested in the range to one meter and at this point I could no longer wait for the measurement and ignore it, but in turn there will be a problem that it cannot be reset SENSOR with the sonar and we will have overlapping impulses and cheated action. ... and even we reset sensor - sound was sended and back to us, if we repeat to fast measurment will be overlaped.

I like to test hardware limit - to avoid later strange problems :-)

Laser sensor should solve my problem :-) need ful process not slower than 10 ms - and not blocking by hardware.

Very Very thank for your fast reaction :-)

and next problem is - that some object is with shape 45 degree is just stealth for sonic sensor - no proper deflector.

sp3swj commented 10 months ago

Hardware limitation :-)