GithubHelp home page GithubHelp logo

sommersoft / adafruit_circuitpython_hcsr04 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adafruit/adafruit_circuitpython_hcsr04

0.0 1.0 0.0 601 KB

CircuitPython library for controlling HC-SR04 ultrasonic range sensors

License: MIT License

Python 100.00%

adafruit_circuitpython_hcsr04's Introduction

Introduction

Documentation Status Discord Build Status

HC-SR04 Product Image

The HC-SR04 is an inexpensive solution for measuring distances using microcontrollers. This library provides a simple driver for controlling these sensors from CircuitPython.

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-hcsr04

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-hcsr04

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .env
source .env/bin/activate
pip3 install adafruit-circuitpython-hcsr04

Usage Example

Warning

The HC-SR04 uses 5V logic, so you will have to use a level shifter between it and your CircuitPython board (which uses 3.3V logic).

Note

If you want to use an HC-SR04 with MicroPython, I recommend checking out this library.

You'll need to dedicate two pins to communicating with the HC-SR04. The sensor communicates in a very rudimentary manner, so it doesn't matter which pins you choose, as long as they're digital IO pins (pins that start with "D" are digital).

There are two ways of instantiating a HCSR04 object: with or without using a context manager.

Note

It is technically possible to communicate with the HC-SR04 using only one wire since the trigger and echo signals aren't ever active at the same time. Once I have a chance to determine a safe way to do this, I plan to add this as a feature to the library.

See Also:

Adafruit's guide on Lifetime and ContextManagers
Gives more info on using context managers with CircuitPython drivers.
board

A list of pins available on your device. To view this list, first get a REPL (the guide linked was written for the pyboard, but it still works), then input the following:

import board
dir(board)

Without a Context Manager

In the example below, we create the HCSR04 object directly, get the distance every 2 seconds, then de-initialize the device.

from adafruit_hcsr04 import HCSR04
sonar = HCSR04(trig, echo)
try:
    while True:
        print(sonar.dist_cm())
        sleep(2)
except KeyboardInterrupt:
    pass
sonar.deinit()

With a Context Manager

In the example below, we use a context manager (the with statement) to create the HCSR04 instance, again get the distance every 2 seconds, but then the context manager handles de-initializing the device for us.

from adafruit_hcsr04 import HCSR04
with HCSR04(trig, echo) as sonar:
    try:
        while True:
            print(sonar.dist_cm())
            sleep(2)
    except KeyboardInterrupt:
        pass

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

Documentation

For information on building library documentation, please check out this guide.

adafruit_circuitpython_hcsr04's People

Contributors

tannewt avatar ladyada avatar kattni avatar siddacious avatar brennen avatar evaherrada avatar mmabey avatar dhalbert avatar junhe avatar jerryneedell avatar sommersoft avatar

Watchers

James Cloos 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.