gpiozero / gpiozero

A simple interface to GPIO devices with Raspberry Pi
https://gpiozero.readthedocs.io/
Other
1.92k stars 304 forks source link

DistanceSensor support single pin #842

Open cletusw opened 4 years ago

cletusw commented 4 years ago

Purpose of change: Support HC-SR04 ultrasonic sensors where trigger and echo are connected to the same pin

Where would this change be made: DistanceSensor class

Short description of change: The Picon Zero is a utility HAT with lots of functionality, one of which is a 4 pin header that allows directly inserting an HC-SR04 sensor and it provides the needed 5v/3v voltage divider. To reduce potential conflicts with other HATs, though, it uses a single GPIO pin, 20, for trigger and echo.

See the example code they provide (which uses BOARD numbering so 38 means GPIO 20), which shows how they toggle that pin back and forth from input to output.

If gpiozero could support this that would be amazing!

Please provide example usage of gpiozero with your suggested change:

from gpiozero import DistanceSensor
from time import sleep

sensor = DistanceSensor(echo=20, trigger=20)
while True:
    print('Distance: ', sensor.distance * 100)
    sleep(1)
bennuttall commented 4 years ago

This has been suggested before, though I don't see an issue for it. Not sure how easy it would be, with how gpiozero works with pins, and I'd fear it could throw the timing out. Worth looking into.

@waveform80 what do you think?