1、参考下面连接好设备:
树莓派 有源蜂鸣器
GPIO17 SIG(信号、数据针脚)
3.3V VCC(+、正极)
GND GND (负极、—)
我的有源蜂鸣器没有SIG,但是有I/O,也是连接的GPIO17,也可以正常工作
树莓派引脚图
2、修改下列文件实现自己需要的功能
#!/usr/bin/env python
# shumeipai.net
import RPi.GPIO as GPIO
import time
Buzzer = 11 # pin11
def setup(pin):
global BuzzerPin
BuzzerPin = pin
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(BuzzerPin, GPIO.OUT)
GPIO.output(BuzzerPin, GPIO.HIGH)
def on():
GPIO.output(BuzzerPin, GPIO.LOW)
def off():
GPIO.output(BuzzerPin, GPIO.HIGH)
def beep(x):
on()
time.sleep(x)
off()
time.sleep(x)
def loop():
while True:
beep(0.5)
def destroy():
GPIO.output(BuzzerPin, GPIO.HIGH)
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup(Buzzer)
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
版权声明:本文为weixin_44575660原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。