# 把玩三色LED燈與PWM呼吸燈

## 一、把玩三色燈

直接使用Arduino的交通三色燈模組小板，學習gpio一次整體設定的方法。

{% embed url="<https://youtu.be/0Sv1ThslDqM>" %}

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

gpio.setmode(gpio.BCM)

GREEN = 17
YELLOW = 27
RED = 22

gyrleds = list([GREEN, YELLOW, RED])

gpio.setup(gyrleds, gpio.OUT)

try:
    while True:
        for led in gyrleds:
            gpio.output(led, gpio.HIGH)
            time.sleep(1)
            gpio.output(led, gpio.LOW)
            time.sleep(1)
except KeyboardInterrupt:
    pass

gpio.cleanup()
```

## 二、使用gpiozero套件模組來把玩三色燈

gpiozero這個套件模組提供了關於控制LED的Class類別，讓我們可以輕易透過它來建立相對應的物件，並取用簡易的控制LED明暗的方法，程式撰寫變得更為簡易

{% embed url="<https://youtu.be/GLIFbcmpfJA>" %}

```python
from gpiozero import LEDBarGraph, LEDBoard, TrafficLights
from time import sleep
from signal import pause

leds = LEDBoard(17, 27, 22, pwm=True)

print('LED ON OFF')
for i in range(5):
    leds.on()
    sleep(1)
    leds.off()
    sleep(1)
time.sleep(3)

print('LED Value')
for i in range(5):
    leds.value = (1, 0, 0)
    sleep(1)
    leds.value = (0, 1, 0)
    sleep(1)
    leds.value = (0, 0, 1)
    sleep(1)
time.sleep(3)

print('LED Blink')
leds.blink(on_time=0.5, off_time=0.5)
pause()
```

## 三、製作PWM呼吸燈

利用非常易懂的例子學習什麼是PWM脈寬調變的概念，再利用這個概念實作出RPi GPIO的PWM呼吸燈，以及gpiozero的PWM呼吸燈的實例，PWM對於日後學習控制輸出功率是不可或缺的觀念與技巧。

{% embed url="<https://youtu.be/d5OW-NzB27E>" %}

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

leds = [17, 27, 22]
# led_pwm = list([])

gpio.setmode(gpio.BCM)
gpio.setup(leds, gpio.OUT)

led_pwm = [gpio.PWM(led, 60) for led in leds]
[led.start(0) for led in led_pwm]
try:
    while True:
        for i in range(0, 80, 5):
            [led.ChangeDutyCycle(i) for led in led_pwm]
            time.sleep(0.1)
        for i in range(80, 0, -5):
            [led.ChangeDutyCycle(i) for led in led_pwm]
            time.sleep(0.1)
except KeyboardInterrupt:
    pass

[l.stop() for l in led_pwm]
gpio.cleanup()
```

採用gpiozero會使程式碼變得更簡潔，如下列程式碼，要製作PWN效果，只要改變其value屬性值即可，非常方便。

```python
from gpiozero import LEDBoard
from signal import pause
import time

pwmled = LEDBoard(17, 27, 22, pwm=True)

v = 0.1
while True:
    for led in pwmled:
        led.value = v
        time.sleep(1)
        v += 0.1
        if v >= 1:
            v = 0.1
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://s761111.gitbook.io/raspi-sensor/ba-wan-san-se-ledpwm-hu-xi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
