Code Monkey home page Code Monkey logo

micropython-hcsr04's Introduction

#HC-SR04 Sensor driver in micropython

Micropython driver for the well-known untrasonic sensor HC-SR04

The driver has been tested on Wemos D1 mini PRO, but It should work on whatever other micropython board, if anyone find problems in other boards, please open an issue and we'll see.

##Motivation

The existing drivers in micropython are a bit old and they don't use the relatively new method machine.time_pulse_us() which Is more accurate that whatever other method using pure python, besides the code is compliant with "standard" micropython, there is no code for specific boards.

Finally I've added a method, distance_mm() that don't use floating point operations, for environments where there is no floating point capabilities.

##Examples of use:

###How to get the distance

The distance_cm() method returns a float with the distance measured by the sensor.

from hcsr04 import HCSR04

sensor = HCSR04(trigger_pin=16, echo_pin=0)

distance = sensor.distance_cm()

print('Distance:', distance, 'cm')

There is another method, distance_mm(), that returns the distance in milimeters (int type) and no floating point is used, designed for environments that doesn't support floating point operations.

distance = sensor.distance_mm()

print('Distance:', distance, 'mm')

The default timeout is based on the sensor limit (4m), but we can also define a different timeout, passing the new value in microseconds to the constructor.

from hcsr04 import HCSR04

sensor = HCSR04(trigger_pin=16, echo_pin=0, echo_timeout_us=1000000)

distance = sensor.distance_cm()

print('Distance:', distance, 'cm')

###Error management

When the driver reaches the timeout while is listening the echo pin the error is converted to OSError('Out of range')

from hcsr04 import HCSR04

sensor = HCSR04(trigger_pin=16, echo_pin=0, echo_timeout_us=10000)

try:
    distance = sensor.distance_cm()
    print('Distance:', distance, 'cm')
except OSError as ex:
    print('ERROR getting distance:', ex)

micropython-hcsr04's People

Contributors

okch-codes avatar rsc1975 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.