You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KY-037声音传感器与树莓派Zero配合时无法正常检测声音

KY-037声音传感器树莓派接线与测试说明

接线详情

  • 接地(Ground):连接树莓派引脚6
  • 输出(Out):连接树莓派引脚7
  • 电源(VCC):尝试过连接引脚2(5V)和引脚1(3.3V)

测试代码

在树莓派Zero上运行Python程序,当前仅实现声音检测时打印提示的功能,后续计划通过Twilio发送短信。代码如下:

import time
import RPi.GPIO as GPIO
from twilio.rest import Client

# Twilio账户信息
account_sid = "123123123"
auth_token = "123123123"
twilio_number = "5551351234"
destination_number = "1121231122"

# 声音传感器配置
sensor_pin = 4

# 设置引脚编号模式为BCM,对应物理引脚7
GPIO.setmode(GPIO.BCM)

# 设置传感器引脚为输入模式
GPIO.setup(sensor_pin, GPIO.IN)

# 初始化Twilio客户端
client = Client(account_sid, auth_token)


while True:
    # 读取声音传感器数值
    value = GPIO.input(sensor_pin)

    # 根据数值判断是否检测到声音
    if value == 1:
        # 检测到声音,打印提示
        print("Sound Detected")
        
        # 以下代码用于发送短信到Twilio账户(暂注释)
        # message = client.messages.create(to=destination_number, from_=twilio_number, body="Sound detected!")

    # 间隔0.1秒再次检测
    time.sleep(0.1)

内容的提问来源于stack exchange,提问作者KinneyKare

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 12:50:23