0%

树莓派控制低电平触发蜂鸣器

连线如下图所示

Python代码(欢乐颂的第一段)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)


GPIO.output(12,GPIO.LOW)

def INT1(time1):
p = GPIO.PWM(12,523)
p.start(1)
time.sleep(time1)

def INT2(time1):
p = GPIO.PWM(12,587)
p.start(1)
time.sleep(time1)

def INT3(time1):
p = GPIO.PWM(12,659)
p.start(1)
time.sleep(time1)

def INT4(time1):
p = GPIO.PWM(12,698)
p.start(1)
time.sleep(time1)

def INT5(time1):
p = GPIO.PWM(12,784)
p.start(1)
time.sleep(time1)

def INT6(time1):
p = GPIO.PWM(12,880)
p.start(1)
time.sleep(time1)

def INT7(time1):
p = GPIO.PWM(12,988)
p.start(1)
time.sleep(time1)

def INT11(time1):
p = GPIO.PWM(12,1046)
p.start(1)
time.sleep(time1)


INT3(0.5)
INT3(0.5)
INT4(0.5)
INT5(0.5)

INT5(0.5)
INT4(0.5)
INT3(0.5)
INT2(0.5)

INT1(0.5)
INT1(0.5)
INT2(0.5)
INT3(0.5)

INT3(1)
INT2(0.5)
INT2(0.5)



p.stop()
GPIO.cleanup()

运行

1
python3 laba.py