microbit_hcsr04
microbit_hcsr04 copied to clipboard
hcsr04.py reports double the distance
Using the hcsr04.py sensor with my microbit reports double the distance.
The distance is still wrong, the error is large
Thats crazy... I now get half the distance!
hcsr04.py
from microbit import *
from machine import time_pulse_us
HIGH = 1
LOW = 0
class HCSR04:
def __init__(self, triggerPin=pin1, echoPin=pin2):
self.triggerPin = triggerPin
self.echoPin = echoPin
self.triggerPin.write_digital(LOW)
self.echoPin.read_digital()
def distance_mm(self):
self.triggerPin.write_digital(HIGH)
self.triggerPin.write_digital(LOW)
micro = time_pulse_us(self.echoPin, HIGH)
return (micro / 2) * 0.34
use
import hcsr04
senor = hcsr04.HCSR04()
distance = senor.distance_mm()
@walterandreas Try to use it like this, the accuracy is quite high
Thank you very much!