Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hcsr04.py reports double the distance #2

Open
rhubarbdog opened this issue Mar 28, 2019 · 5 comments
Open

hcsr04.py reports double the distance #2

rhubarbdog opened this issue Mar 28, 2019 · 5 comments

Comments

@rhubarbdog
Copy link
Contributor

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

@mofada
Copy link

mofada commented Nov 2, 2020

The distance is still wrong, the error is large

@andreas-web
Copy link

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

@mofada
Copy link

mofada commented Dec 21, 2020

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
Copy link

mofada commented Dec 21, 2020

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

@andreas-web
Copy link

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants