I have been running an ultrasonic sensor on a raspberry pi for a while, with quite some good results.
I wanted to port this on an esp8266 and, it looks like the result are a bit less accurate than on the pi.
Here is the graphs usage of each one :
The line is quite smoother on the pi.
Here is the code i use on the esp8266 to get the values :
Code: Select all
import time import machine trigger_pin=5 echo_pin=4 trigger=machine.Pin(trigger_pin, machine.Pin.OUT) echo=machine.Pin(echo_pin, machine.Pin.IN) def get_distance(): trigger.low() time.sleep_us(5) trigger.high() time.sleep_us(10) trigger.low() while echo.value()==0: start=time.ticks_us() while echo.value()==1: delta=time.ticks_diff(time.ticks_us(), start) distance=(delta/2)/29 return distance
I suspect that the tick is less reliable on the esp8266 than on the pi. For example, in an other project, using a timer of 90000 milliseconds yield in a run of 82 seconds.
Any idea on how the code could be getting better result out of this sensor on esp8266?