# A04. RGB LED 顏色變化

### 專案說明 <a href="#a04.rgbled-yan-se-bian-hua-zhuan-an-shui-ming" id="a04.rgbled-yan-se-bian-hua-zhuan-an-shui-ming"></a>

使用三根數位腳位來控制 RGB LED 的顏色變化

### 電路圖 <a href="#a04.rgbled-yan-se-bian-hua-dian-lu-tu" id="a04.rgbled-yan-se-bian-hua-dian-lu-tu"></a>

* [LinkIt 7697](https://robotkingdom.com.tw/product/linkit-7697/)
* 麵包板
* RGB LED (共陽極) (RGB = Red， Green， Blue， 為光的三原色)
* 杜邦線

此範例使用的 RGB  LED 為共陽極 LED， 由下圖 腳位1 的接到 3.3V 的電源， 腳位 2 / 3 / 4 接到數位輸出腳位

* 當 2 / 3 / 4 的腳位拉高到 3.3V, 電流就不會流過, LED 就不會點亮
* 當 2 / 3 / 4 的腳位拉低， 電流就會流過， LED 就會點亮

![](/files/Ck1D1oMAxWRafkOoIVC2)

![](/files/T6cjzG0DArnGTaRFdIoa)

(圖片由 [Fritzing](http://fritzing.org/home/) 產生, LinkIt 7697 fritzing [下載](https://www.dropbox.com/s/44rgxihq1nri2qn/linkit_7697_fritzing_and_diagrams.zip))\
&#x20;

### 積木畫布 <a href="#a04.rgbled-yan-se-bian-hua-ji-mu-hua-bu" id="a04.rgbled-yan-se-bian-hua-ji-mu-hua-bu"></a>

控制數位寫入腳位的高低電位來控制 RBG 三色的亮度. 此範例使用的是共陽極三色LED， 所以該顏色對應的腳位是低電位狀態， 電流才會流過並發亮

![](/files/h7Ad3CXrttQP8sHibvfl)

```
void setup()
{
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
}

void loop()
{
  digitalWrite(15, LOW);
  digitalWrite(16, HIGH);
  digitalWrite(17, HIGH);
  delay(1000);

  digitalWrite(15, LOW);
  digitalWrite(16, LOW);
  digitalWrite(17, HIGH);
  delay(1000);

  digitalWrite(15, LOW);
  digitalWrite(16, LOW);
  digitalWrite(17, LOW);
  delay(1000);
}

```

### 衍伸學習 : 三原色混色 <a href="#a04.rgbled-yan-se-bian-hua-yan-shen-xue-xi-san-yuan-se-hun-se" id="a04.rgbled-yan-se-bian-hua-yan-shen-xue-xi-san-yuan-se-hun-se"></a>

透過 RBG 三原色的混色， 可以混出不同的顏色， 可嘗試調整範例中數位腳位的高低電位， 來觀察顏色的變畫， 可以產生以下**七種**顏色組合.

![](/files/97cNMzpMLnZGyXzUzUk2)

### 衍伸學習 : 千變萬化的顏色 <a href="#a04.rgbled-yan-se-bian-hua-yan-shen-xue-xi-qian-bian-wan-hua-de-yan-se" id="a04.rgbled-yan-se-bian-hua-yan-shen-xue-xi-qian-bian-wan-hua-de-yan-se"></a>

可以將**數位寫入**改成**類比寫入，** 讓 RGB 三原色各有 **256** 種電壓 (0\~255) 輸出組合，可以呈現 256x256x256 = **16,777,216** (一千六百多萬) 種的顏色組合變化.

此範例讓 RGB 三色每秒會隨機取得 0\~255 的數值

![](/files/kfKjezVeo8qMLmjvD6qc)

{% hint style="info" %}
**隨機積木**產生出來的值， 會介於 "最小" \~ "最大-1" 之間.

下面這範例 會產生的數值是 0, 1, 2, 3, 4, 5, 6, 7, 8, 9， 並不包含 10
{% endhint %}

![](/files/8lHgK288jXsAkcI7fz4l)

### 衍伸學習: 共陰極 RGB LED <a href="#a04.rgbled-yan-se-bian-hua-yan-shen-xue-xi-gong-yin-ji-rgbled" id="a04.rgbled-yan-se-bian-hua-yan-shen-xue-xi-gong-yin-ji-rgbled"></a>

另外還有一種 RGB LED 是共陰極 LED， 電流會從下圖的 P15，16，17 輸出， 透過共用的陰極腳位回到 GND

![](/files/GsZj7EORD1wVvwWpKP4n)


---

# 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://cavedu.gitbook.io/linkit-7697/linkit-7697-blocklyduino-shi-yong-zhi-nan/shi-zuo-fan-li-jiao-xue/a04.-rgb-led-yan-se-bian-hua.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.
