简体   繁体   中英

Is there any example of how to use the digital input or analog input of GPIO on google aiy voice board?

I can not read the input from the motion sensor (HC-SR501) which connects to PIN_A of the expansion pins on the voice bonnet of my aiy voice kit.

Below are the code and the error message, please shed some light.

Code:

from gpiozero import MotionSensor
from aiy.pins import (PIN_A, PIN_B, PIN_C, PIN_D)
pir = MotionSensor(PIN_A)
pir.wait_for_motion()
print("Motion detected!")

Error message:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/input_devices.py", line 51, in __init__
    self.pin.pull = pull
  File "/usr/lib/python3/dist-packages/gpiozero/pins/__init__.py", line 279, in <lambda>
    lambda self, value: self._set_pull(value),
  File "/opt/aiy/projects-python/src/aiy/pins.py", line 569, in _set_pull
    'Only pull up is supported right now (%s)' % pull)
gpiozero.exc.PinFixedPull: Only pull up is supported right now (down)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "motion_sensor.py", line 4, in <module>
    pir = MotionSensor(PIN_A)
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 95, in __call__
    self = super(GPIOMeta, cls).__call__(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/gpiozero/input_devices.py", line 451, in __init__
    pin_factory=pin_factory
  File "/usr/lib/python3/dist-packages/gpiozero/input_devices.py", line 163, in __init__
    pin, pull_up, pin_factory=pin_factory
  File "/usr/lib/python3/dist-packages/gpiozero/mixins.py", line 164, in __init__
    super(EventsMixin, self).__init__(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/gpiozero/input_devices.py", line 53, in __init__
    self.close()
  File "/usr/lib/python3/dist-packages/gpiozero/input_devices.py", line 185, in close
    super(SmoothedInputDevice, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 393, in close
    self.pin_factory.release_pins(self, self._pin.number)
AttributeError: 'HatPin' object has no attribute 'number'
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 483, in _shutdown
    _devices_shutdown()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 476, in _devices_shutdown
    dev.close()
  File "/usr/lib/python3/dist-packages/gpiozero/input_devices.py", line 185, in close
    super(SmoothedInputDevice, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 393, in close
    self.pin_factory.release_pins(self, self._pin.number)
AttributeError: 'HatPin' object has no attribute 'number'
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 122, in __del__
    self.close()
  File "/usr/lib/python3/dist-packages/gpiozero/input_devices.py", line 185, in close
    super(SmoothedInputDevice, self).close()
  File "/usr/lib/python3/dist-packages/gpiozero/devices.py", line 393, in close
    self.pin_factory.release_pins(self, self._pin.number)
AttributeError: 'HatPin' object has no attribute 'number'

Did you solve it?

By looking at the API and your error message, try changing line 3 of your code

From :

pir = MotionSensor(PIN_A)

To :

pir = MotionSensor(PIN_A, pull_up=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM