You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A *passive* buzzer emits a tone when a voltage is applied across it. It also requires a specific signal to generate a variety of tones. The *active* buzzers are a lot simpler to use, so these are covered here.
7
+
## 连接蜂鸣器
6
8
7
-
## Connecting a buzzer
9
+
有源蜂鸣器用起来就像LED一样,但是有源蜂鸣器更加稳健,不需要串联电阻来保护有源蜂鸣器:
8
10
9
-
An *active* buzzer can be connected just like an LED, but as they are a little more robust, you won't be needing a resistor to protect them.
10
-
11
-
Set up the circuit as shown below:
11
+
按照下图连接电路:
12
12
13
13

14
14
15
-
1.Add`Buzzer` to the `from gpiozero import...`line:
15
+
1.导入`Buzzer` to the `from gpiozero import...`类:
16
16
17
17
```python
18
18
from gpiozero import Buzzer
19
19
from time import sleep
20
20
```
21
21
22
-
1. Add a line below your creation of `button`and`lights` to add a `Buzzer`object:
22
+
1. 在你创建按钮和灯后面添加`Buzzer`对象。:
23
23
24
24
```python
25
25
buzzer = Buzzer(17)
26
26
```
27
27
28
-
1. In GPIO Zero, a `Buzzer` works exactly like an `LED`, so try adding a `buzzer.on()`and`buzzer.off()` into your loop:
One powerful feature of the Raspberry Pi is the row of GPIO pins along the top edge of the board. GPIO stands for General-Purpose Input/Output. These pins are a physical interface between the Raspberry Pi and the outside world. At the simplest level, you can think of them as switches that you can turn on or off (input) or that the Pi can turn on or off (output).
The GPIO pins allow the Raspberry Pi to control and monitor the outside world by being connected to electronic circuits. The Pi is able to control LEDs, turning them on or off, run motors, and many other things. It's also able to detect whether a switch has been pressed, the temperature, and light. We refer to this as physical computing.
8
7
9
-
There are 40 pins on the Raspberry Pi (26 pins on early models), and they provide various different functions.
If you have a RasPiO pin label, it can help to identify what each pin is used for. Make sure your pin label is placed with the keyring hole facing the USB ports, pointed outwards.
12
15
13
16

14
17
15
-
If you don't have a pin label, then this guide can help you to identify the pin numbers:
18
+
如果你没有引脚标签的话,下面的示意图可以帮助你确定引脚编号
16
19
17
20

18
21
19
-
You'll see pins labelled as 3V3, 5V, GND and GP2, GP3, etc:
22
+
你可以看到引脚有3V3,5V,GND,GP2,GP3等标签:
23
+
20
24
21
25
||||
22
26
|---|---|---|
23
-
| 3V3 | 3.3 volts | Anything connected to these pins will always get 3.3V of power |
24
-
| 5V | 5 volts | Anything connected to these pins will always get 5V of power |
25
-
| GND | ground | Zero volts, used to complete a circuit |
26
-
| GP2 | GPIO pin 2 | These pins are for general-purpose use and can be configured as input or output pins |
27
-
| ID_SC/ID_SD/DNC | Special purpose pins ||
27
+
| 3V3 | 3.3V | 链接到这个引脚可以得到3.3V电压 |
28
+
| 5V | 5V | 连接到这个引脚可以得到5V电压 |
29
+
| GND | 接地 | 0V接地用来完成电路 |
30
+
| GP2 | GPIO 针脚 2 | 这些针脚是通用针脚,可以配置为输入或者输出针脚 |
31
+
| ID_SC/ID_SD/DNC | 特殊用途引脚
32
+
||
28
33
29
-
**WARNING**: If you follow the instructions, then playing about with the GPIO pins is safe and fun. Randomly plugging wires and power sources into your Pi, however, may destroy it, especially if using the 5V pins. Bad things can also happen if you try to connect things to your Pi that use a lot of power; LEDs are fine, motors are not. If you're worried about this, then you might want to consider using an add-on board such as the [Explorer HAT](https://shop.pimoroni.com/products/explorer-hat) until you're confident enough to use the GPIO directly.
LEDs are delicate little things. If you put too much current through them they will pop (sometimes quite spectacularly). To limit the current going through the LED, you should always use a resistor in series with it.
1.You can switch an LED on and off by typing commands directly into the Python interpreter window (also known as the Python **shell**). Let's do this by first importing the GPIO Zero library. You also need to tell the Pi which GPIO pin you are using - in this case pin 17. Next to the chevrons `>>>`, type:
58
+
1.从主菜单打开IDLE(`菜单`>`编程`>`Python 3 (IDLE)`.
54
59
55
-
```python
60
+
1.你可以通过直接在Python解释器里输入命令来控制LED的亮暗。(或者说Python **shell**). Let's do this by first importing the GPIO Zero library.在控制LED等之前首先导入GPIO Zero库。你还需要告诉树莓派你正在用哪个针脚-这个例子用的是17号针脚。紧接着大于号 `>>>`, 输入
61
+
62
+
```python
56
63
from gpiozero import LED
57
64
led = LED(17)
58
65
59
66
```
60
-
Press **Enter** on the keyboard.
67
+
敲 **回车**.
61
68
62
-
1.To make the LED switch on, type the following and press**Enter**:
69
+
1.为了让LED亮起来,输入下面代码并敲**Enter**:
63
70
64
71
```python
65
72
led.on()
66
73
```
67
74
68
-
1.To make it switch off you can type:
75
+
1.让LED灭可以输入:
69
76
70
77
```python
71
78
led.off()
72
79
```
73
80
74
-
1. Your LED should switch on and then off again. But that's not all you can do.
81
+
1. Your LED should switch on and then off again. But that's not all you can do.LED就会先亮后灭,但是你能做的不止于此。
75
82
76
-
## Flashing an LED
83
+
## 让LED闪烁
77
84
78
-
With the help of the `time` library and a little loop, you can make the LED flash.
85
+
通过使用`time`库,和一个简单的循环,我们可以让LED不断闪烁。
79
86
80
-
1.Create a new file by clicking **File > New file**.
87
+
1.单击 **文件 > 新建文件**创建新的Python文件.
81
88
82
-
1.Save the new file by clicking **File > Save**. Save the file as `gpio_led.py`.
89
+
1.单击 **文件 > 保存**保存文件为`gpio_led.py`.
83
90
84
-
1.Enter the following code to get started:
91
+
1.输入以下代码:
85
92
86
93
```python
87
94
from gpiozero importLED
@@ -96,47 +103,48 @@ With the help of the `time` library and a little loop, you can make the LED flas
96
103
sleep(1)
97
104
```
98
105
99
-
1. Save with**Ctrl + S**and run the code with**F5**.
106
+
1. 按 **Ctrl + S**保存文件并按 **F5** 执行代码.
100
107
101
-
1. The LED should be flashing on and off. To exit the program press **Ctrl + C**on your keyboard.
108
+
1. LED灯开始闪烁. 按 **Ctrl + C**退出程序.
102
109
103
-
## Using buttons to get input
110
+
## 利用按钮获取输入
104
111
105
-
Now you're able to control an output component (an LED), let's connect and control an input component: a button.
112
+
现在你可以控制输出元件(一个LED),现在让我们连接并控制一个输入元件:按钮。
106
113
107
-
1. Connect a button to another GND pin andGPIO pin 2, like this:
114
+
115
+
1. 按图把一个按钮连接到另外一个GND引脚和2号GPIO引脚:
108
116
109
117

110
118
111
-
1. Create a new file by clicking **File > New file**.
119
+
1. 点击 **File > New file**新建文件.
112
120
113
-
1. Save the new file by clicking **File > Save**. Save the fileas`gpio_button.py`.
121
+
1. 点击 **File > Save**. 把新建文件保存为`gpio_button.py`.
114
122
115
-
1. This time you'll need the `Button` class, and to tell it that the button is on pin 2. Write the following code in your new file:
1. Now you can get your program to do something when the button is pushed. Add these lines:
130
+
1. 现在当按钮按下的时候你的程序就可以做一些事情。添加以下代码:
123
131
124
132
```python
125
133
button.wait_for_press()
126
-
print('You pushed me')
134
+
print('你按我了')
127
135
```
128
-
1. Save with**Ctrl + S**and run the code with**F5**.
129
-
1. Press the button and your text will appear.
136
+
1. 按 **Ctrl + S**保存代码并按 **F5**执行代码.
137
+
1. 单击按钮文本就会出现.
130
138
131
-
## Manually controlling the LED
139
+
## 手动控制LED
132
140
133
-
You can now combine your two programs written so far to control the LED using the button.
141
+
You can now combine your two programs written so far to control the LED using the button.现在可以目前为止写的两个程序结合起来通过按钮来控制LED。
134
142
135
-
1. Create a new file by clicking **File > New file**.
143
+
1. 单击 **File > New file**新建文件.
136
144
137
-
1. Save the new file by clicking **File > Save**. Save the fileas`gpio_control.py`.
145
+
1. 单击 **File > Save**保存文件为`gpio_control.py`.
138
146
139
-
1. Now write the following code:
147
+
1. 输入以下代码:
140
148
141
149
```python
142
150
from gpiozero importLED, Button
@@ -151,13 +159,13 @@ You can now combine your two programs written so far to control the LED using th
151
159
led.off()
152
160
```
153
161
154
-
1. Save and run your program. When you push the button the LED should come on for three seconds.
162
+
1. 保存并运行程序。当你按按钮的时候LED持续亮3秒然后灭掉。
155
163
156
-
## Making a switch
164
+
## 制作一个开关
157
165
158
-
With a switch, a single press and release on the button would turn the LED on, and another press and release would turn it off again.
166
+
通过开关,按下和松开按钮一次就会点亮LED,再次按下和松开就会关掉LED。
159
167
160
-
1. Modify your code so that it looks like this:
168
+
1. 修改程序如下:
161
169
162
170
```python
163
171
from gpiozero importLED, Button
@@ -171,11 +179,12 @@ With a switch, a single press and release on the button would turn the LED on, a
171
179
led.toggle()
172
180
```
173
181
174
-
`led.toggle()`switches the state of the LEDfrom on to off, or off to on. Since this happens in a loop the LEDwith turn on and off each time the button is pressed.
1. It would be great if you could make the LED switch on only when the button is being held down. With GPIO Zero, that's easy. There are two methods of the `Button` class called `when_pressed` and `when_released`. These don't block the flow of the program, so if they are placed in a loop, the program will continue to cycle indefinitely.
184
+
1. These don't block the flow of the program, so if they are placed in a loop, the program will continue to cycle indefinitely.
1. Save and run the program. Now when the button is pressed, the LED will light up. It will turn off again when the button is released.保持并运行程序,现在按下按钮,LED亮;松开按钮,LED灭。
194
205
195
-
## What next?
206
+
## 接下来学习啥?
196
207
197
-
There are lots of other things you can control or monitor with your Raspberry Pi. Have a look at the worksheets below, to see how easily this can be done.
208
+
通过树莓派你可以控制和监测大量其他的元件。看看下面的列表,就会明白这很容易。
198
209
199
-
- [Using an active buzzer](buzzer.md)
200
-
- [Making traffic lights](trafficlights.md)
201
-
- [Using a light-dependent resistor](ldr.md)
202
-
- [Using a PIR Sensor](pir.md)
203
-
- [Using an ultrasonic distance sensor](distance.md)
0 commit comments