---
# System prepended metadata

title: '**蜂鳴器**'

---

# **蜂鳴器**
### **下載安裝Jetson.GPIO庫**
```shell
sudo pip install Jetson.GPIO
sudo pip3 install Jetson.GPIO
```

### **連接**
**正極:pin12
負極:pin14**

### **程式碼**
```python=
import RPi.GPIO as GPIO
import time

pin_buzzer=12
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin_buzzer,GPIO.OUT, initial=GPIO.HIGH)

try:
    while True:
        GPIO.output(pin_buzzer, GPIO.LOW)
        time.sleep(1)
        GPIO.output(pin_buzzer, GPIO.HIGH)
        time.sleep(0.1)
        if <偵測到person>:
            GPIO.output(pin_buzzer, GPIO.HIGH)
            break
except:
    print("Wrong")
    GPIO.output(pin_buzzer, GPIO.HIGH)
finally:
    print("Program finished")
    GPIO.output(pin_buzzer, GPIO.HIGH)
    GPIO.cleanup()
```

```python=
#coding:utf-8

# 匯入GPIO控制薄塊
import RPi.GPIO as GPIO
# 匯入time模組
import time

# 定義引腳
fm = 13
# 設定使用的引腳編碼模式
GPIO.setmode(GPIO.BOARD)
# 進行引腳的初始化，因為是低電平觸發，初始時設定為高電平
GPIO.setup(fm,GPIO.OUT, initial=GPIO.HIGH)

# 進行一長兩短的聲音播放
# 播放1秒聲音
GPIO.output(fm, GPIO.LOW)
time.sleep(1)
GPIO.output(fm, GPIO.HIGH)
time.sleep(0.1)
# 播放0.5秒聲音
GPIO.output(fm, GPIO.LOW)
time.sleep(0.5)
GPIO.output(fm, GPIO.HIGH)
time.sleep(0.1)
# 播放0.5秒聲音
GPIO.output(fm, GPIO.LOW)
time.sleep(0.5)
# 停止蜂鳴器播放
if cv2.waitKey(1) & 0xFF == ord('q'):
    GPIO.output(fm, GPIO.HIGH)
    GPIO.cleanup()
```

```python=
import RPi.GPIO as GPIO
import time

fm=19
GPIO.setmode(GPIO.BOARD)
GPIO.setup(fm,GPIO.OUT, initial=GPIO.HIGH)

i=5
while i>0:
    GPIO.output(fm, GPIO.LOW)
    time.sleep(1)
    GPIO.output(fm, GPIO.HIGH)
    time.sleep(0.1)
    i=i-1
    if cv2.waitKey(1) & 0xff == ord('q'):
        GPIO.output(fm, GPIO.HIGH)
        GPIO.cleanup()
```

```python=
import RPi.GPIO as GPIO
import time
import cv2

fm=19
GPIO.setmode(GPIO.BOARD)
GPIO.setup(fm,GPIO.OUT, initial=GPIO.HIGH)

i=5
while i>0:
    GPIO.output(fm, GPIO.LOW)
    time.sleep(1)
    GPIO.output(fm, GPIO.HIGH)
    time.sleep(0.1)
    i=i-1

    key = cv2.waitKey(1)
    if key == ord('q'):
        GPIO.output(fm, GPIO.HIGH)
        GPIO.cleanup()
        print("程序正常退出...Bye 不要想我哦")
        break
```

```python=
import RPi.GPIO as GPIO
import time
import cv2

fm=22 #GND隨便
GPIO.setmode(GPIO.BOARD)
GPIO.setup(fm,GPIO.OUT, initial=GPIO.HIGH)

i=5
while i>0:
    key = cv2.waitKey(1)
    if key == ord('q'):
        GPIO.output(fm, GPIO.HIGH)
        GPIO.cleanup()
    GPIO.output(fm, GPIO.LOW)
    time.sleep(1)
    GPIO.output(fm, GPIO.HIGH)
    time.sleep(0.1)
    i=i-1

GPIO.output(fm, GPIO.HIGH)
GPIO.cleanup()
```