将音频插孔从Raspberry Pi插入音频界面停止传感器工作
我正在运行一个非常简单的Python脚本,使用HC-SR04超声波距离传感器进行注册和打印距离。当3.5mm电缆从Raspberry插入并插入音频接口时,该脚本可以工作。
但是,当电缆也插入音频接口时,GPIOZERO库会给我带来错误,
/usr/lib/python3/dist-packages/gpiozero/input_devices.py:978: DistanceSensorNoEcho: no echo received
warnings.warn(DistanceSensorNoEcho('no echo received'))
这可能是一个扎根的问题吗? 我还认为也许是电力输送问题?即,当音频插孔被插入时,由于放大器必须开始工作时,没有足够的功率运行传感器?...但是当我的耳机插入时,代码会运行。因此,应该折扣它。
在展览的截止日期之前,大约有两个星期的时间来弄清楚这个问题……因此,任何帮助或指示都将不胜感激!
这是Python代码:
#!/usr/bin/python3
# https://www.youtube.com/watch?v=JvQKZXCYMUM
import RPi.GPIO as GPIO
import pygame
from threading import Thread
from signal import signal, SIGTERM, SIGHUP, pause
from time import sleep
from gpiozero import DistanceSensor
pygame.mixer.init()
pygame.mixer.Sound("raspi-files/audio/1khz.wav")
reading = True
sensor = DistanceSensor(echo=24, trigger=21)
def safe_exit(signum, frame):
exit(1)
def read_distance():
while reading:
print("Distance: ", sensor.distance)
sleep(0.1)
signal(SIGTERM, safe_exit)
signal(SIGHUP, safe_exit)
try:
reader = Thread(target=read_distance, daemon=True)
reader.start()
pause()
except KeyboardInterrupt:
pass
finally:
reading = False
sensor.close()
I'm running a very simple Python script to register and print distance using the HC-SR04 ultrasonic distance sensor. The script works when the 3.5mm cable is plugged out of the Raspberry and not plugged into the audio interface.
But when the cable is plugged into the audio interface as well, the gpiozero library throws me the error
/usr/lib/python3/dist-packages/gpiozero/input_devices.py:978: DistanceSensorNoEcho: no echo received
warnings.warn(DistanceSensorNoEcho('no echo received'))
Could this maybe be a grounding issue?..
I also thought maybe a power delivery problem? ie, when the audio jack is plugged in, there isn't enough power to run the sensor as the amplifier has to start working?... But the code runs when my headphones are plugged in. So that should discount that.
Got about two weeks to figure this one out before the deadline for an exhibition... So any help or pointers would be much appreciated!
This is the python code:
#!/usr/bin/python3
# https://www.youtube.com/watch?v=JvQKZXCYMUM
import RPi.GPIO as GPIO
import pygame
from threading import Thread
from signal import signal, SIGTERM, SIGHUP, pause
from time import sleep
from gpiozero import DistanceSensor
pygame.mixer.init()
pygame.mixer.Sound("raspi-files/audio/1khz.wav")
reading = True
sensor = DistanceSensor(echo=24, trigger=21)
def safe_exit(signum, frame):
exit(1)
def read_distance():
while reading:
print("Distance: ", sensor.distance)
sleep(0.1)
signal(SIGTERM, safe_exit)
signal(SIGHUP, safe_exit)
try:
reader = Thread(target=read_distance, daemon=True)
reader.start()
pause()
except KeyboardInterrupt:
pass
finally:
reading = False
sensor.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,最终,代码中的扳机销数字是错误的...触发器应该是23
。传感器仍然连续注册距离,并按照预期的方式将其报告回控制台...因此,我没有真正的理由认为代码是错误的...
也在GPIOZERO github github上,人们对此感到困惑。非常奇怪。
So in the end, the trigger pin numbering in the code was wrong... Trigger should've been 23.
But the crazy thing is that the code and sensor still worked (but only when the audio cable was left unplugged)! The sensor still registered distance continuously and reported it back to the console as expected... So I had no real reason to think the code was wrong...
Also on the gpiozero github people stumped by that. Very odd.