microbit_hcsr04 icon indicating copy to clipboard operation
microbit_hcsr04 copied to clipboard

hcsr04.py reports double the distance

Open rhubarbdog opened this issue 5 years ago • 5 comments

Using the hcsr04.py sensor with my microbit reports double the distance.

rhubarbdog avatar Mar 28 '19 11:03 rhubarbdog

The distance is still wrong, the error is large

mofada avatar Nov 02 '20 02:11 mofada

Thats crazy... I now get half the distance!

andreas-web avatar Dec 21 '20 07:12 andreas-web

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()

mofada avatar Dec 21 '20 07:12 mofada

@walterandreas Try to use it like this, the accuracy is quite high

mofada avatar Dec 21 '20 07:12 mofada

Thank you very much!

andreas-web avatar Dec 21 '20 08:12 andreas-web