學習目標 : 認識faya LED模組的功能及使用方式
學習時間 : 35min
示範模組 : (1) fayaduino UNO
(2) faya 電源底板
(3) faya LED模組
工具 : (1) 樂高積木底板 (相容)
====================功能介紹====================
還記得學生時代的跑馬燈作業嗎? faya LED模組使用8顆的紅色LED燈,不但方便讓使用者作跑馬燈迴圈的程式練習,排成一直線的方式對於訊號強弱的顯示也能夠很直覺的顯示!
faya LED模組的面板上包含了8個輸入埠,D0~D7,分別連接到每個對應LED的陽極:
- 當輸入埠D0~D7收到High準位時,對應的LED點亮
- 當輸入埠D0~D7收到Low準位時,對應的LED點亮
====================原理知識====================
以下解釋提供給有需要知道背後原理的人:
faya LED模組的動作原理是很簡單的,由於紅色LED順向壓降約為2V,因此輸入5V電壓時,LED點亮,跨過LED的電流約等於 (5V-2V)/220 ohm = 13.5mA,足以提供給LED不錯的亮度。參考電路圖如下所示:
===================範例實作(1)===================
了解模組功能(原理)後,我們用以下範例來展示模組的功能:
目標:
(1) 設計LED跑馬燈,讓LED重複的由D0點依序亮到D7
接線:
(1) 電源線連接
如下圖所示,連接的說明請看這篇文章 或簡易版
(2) 訊號線連接
Arduino_13 ===> LED_D0
Arduino_12 ===> LED_D1
Arduino_11 ===> LED_D2
Arduino_10 ===> LED_D3
Arduino_9 ===> LED_D4
Arduino_8 ===> LED_D5
Arduino_7 ===> LED_D6
Arduino_6 ===> LED_D7
範例程式:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 2017/5/15 | |
// Faya-Nugget 範例程式 (RedLED8_1.ino) | |
// 單元: 模組介紹-fayaLED模組 | |
// 網址: http://fayalab.blogspot.com/2017/05/faya-led.html | |
// 目標 (1) 設計LED跑馬燈,讓LED重複的由D0點依序亮到D7 | |
// 接線: Arduino ==> faya模組 | |
// 13 ==> D0 (LED模組) | |
// 12 ==> D1 (LED模組) | |
// 11 ==> D2 (LED模組) | |
// 10 ==> D3 (LED模組) | |
// 9 ==> D4 (LED模組) | |
// 8 ==> D5 (LED模組) | |
// 7 ==> D6 (LED模組) | |
// 6 ==> D7 (LED模組) | |
void setup() { | |
// 設定LED腳位為輸出 | |
pinMode(13, OUTPUT); | |
pinMode(12, OUTPUT); | |
pinMode(11, OUTPUT); | |
pinMode(10, OUTPUT); | |
pinMode(9, OUTPUT); | |
pinMode(8, OUTPUT); | |
pinMode(7, OUTPUT); | |
pinMode(6, OUTPUT); | |
} | |
void loop() { | |
for(int i=13; i>=6; i-=1) // 從第13隻(D0)腳亮起,依序遞減 | |
{ | |
digitalWrite(i, HIGH); // 點亮 | |
delay(500); // 等 0.5秒 | |
digitalWrite(i, LOW); // 熄滅 | |
delay(100); // 等 0.1秒 | |
} | |
} |
備註:
- 在setup()中的L20~L27的腳位設定撰寫效率不佳,我們可以用以下迴圈取代:

- 由於LED的D0~D7剛好規律地順著Arduino的13~6腳位連接,因此在for迴圈裡可以直接把數字腳位根據需求拿來運算。
範例結果:
===================範例實作(2)===================
目標:(1) 設計LED跑馬燈,讓LED重複的由D0點依序亮到D7,再依序點亮回D0
接線:和上一範例相同,無須更改
範例程式:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 2017/5/16 | |
// Faya-Nugget 範例程式 (RedLED8_2.ino) | |
// 單元: 模組介紹-fayaLED模組 | |
// 網址: http://fayalab.blogspot.com/2017/05/faya-led.html | |
// 目標 (1) 設計LED跑馬燈,讓LED重複的由D0點依序亮到D7,再依序點亮回D0 | |
// 接線: Arduino ==> faya模組 | |
// 13 ==> D0 (LED模組) | |
// 12 ==> D1 (LED模組) | |
// 11 ==> D2 (LED模組) | |
// 10 ==> D3 (LED模組) | |
// 9 ==> D4 (LED模組) | |
// 8 ==> D5 (LED模組) | |
// 7 ==> D6 (LED模組) | |
// 6 ==> D7 (LED模組) | |
int D0_flag = 1; // D0 LED 旗標,初始值為1 (亮) | |
int D7_flag = 0; // D7 LED 旗標,初始值為0 (暗) | |
void setup() { | |
// 設定LED腳位為輸出 | |
for (int i=13; i>=6; i--) | |
{ | |
pinMode(i,OUTPUT); | |
} | |
} | |
void loop() { | |
if(D0_flag == 1) // D0亮時往D7跑 | |
{ | |
LED_D0_to_D7(); | |
} | |
if(D7_flag == 1) // D7亮時往D0跑 | |
{ | |
LED_D7_to_D0(); | |
} | |
} | |
// D0往D7跑副程式 | |
void LED_D0_to_D7() { | |
for(int i=13; i>=6; i-=1) | |
{ | |
digitalWrite(i, HIGH); | |
delay(500); | |
digitalWrite(i, LOW); | |
delay(100); | |
if (i==6) | |
{ | |
D0_flag = 0; //紀錄D0暗 | |
D7_flag = 1; //紀錄D7亮 | |
} | |
} | |
} | |
// D7往D0跑副程式 | |
void LED_D7_to_D0() { | |
for(int i=6; i<=13; i+=1) | |
{ | |
digitalWrite(i, HIGH); | |
delay(500); | |
digitalWrite(i, LOW); | |
delay(100); | |
if (i==13) | |
{ | |
D0_flag = 1; //紀錄D0亮 | |
D7_flag = 0; //紀錄D7暗 | |
} | |
} | |
} | |
備註:
- 要讓LED燈來回跑,我們設了2個旗標, D0_flag 和 D7_flag,分別記錄燈跑到最右邊(D0=1)和最左邊(D7=1)的狀態,有了這兩個旗標,我們就能判斷是否該回頭了!
範例結果:
討論:
各位有沒有發現這個程式會讓LED燈在最左右邊多閃了一次,如果要讓執行結果如下,該怎麼修改程式呢? 大家不妨試試看,需要解答的請到我們粉絲頁留言喔!
===================範例實作(3)===================
目標:
(1)利用鍵盤的數字0~7來開/關 LED燈D0~D7
接線:
和上一範例相同,無須更改
範例程式:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 2017/5/16 | |
// Faya-Nugget 範例程式 (RedLED8_3.ino) | |
// 單元: 模組介紹-fayaLED模組 | |
// 網址: http://fayalab.blogspot.com/2017/05/faya-led.html | |
// 目標 (1) 利用鍵盤的數字0~7來開/關 LED燈D0~D7 | |
// 接線: Arduino ==> faya模組 | |
// 13 ==> D0 (LED模組) | |
// 12 ==> D1 (LED模組) | |
// 11 ==> D2 (LED模組) | |
// 10 ==> D3 (LED模組) | |
// 9 ==> D4 (LED模組) | |
// 8 ==> D5 (LED模組) | |
// 7 ==> D6 (LED模組) | |
// 6 ==> D7 (LED模組) | |
int ledFlag[8] = {0,0,0,0,0,0,0,0}; //建立LED陣列,紀錄D0~D7狀態 | |
void setup() | |
{ | |
Serial.begin(9600); | |
for(int i=13; i>=6; i--) | |
{ | |
pinMode(i, OUTPUT); // 設定連接LED的腳位為輸出 | |
} | |
Serial.println("Enter numbrt 0~7 from keyboard to turn on/off LED D0~D7"); | |
} | |
void loop() | |
{ | |
if (Serial.available()) | |
{ | |
char ch = Serial.read(); | |
if (ch >= '0' && ch <= '7') // 判斷鍵盤數字是否介於0~7 | |
{ | |
int ledNumber = ch - '0'; // 把按的數字存到ledNumber | |
ledFlag[ledNumber] = !ledFlag[ledNumber]; // 切換目前LED狀態 (暗/亮) | |
digitalWrite(13-ledNumber, ledFlag[ledNumber]); //把結果輸出至LED | |
} | |
} | |
} |
備註:
- L36:利用NOT指令(!),讓目前的LED的結果轉態 (亮=>暗) or (暗=>亮)
- L33: ch原本設成字元,在比較時,自動轉換成HEX數值,因此所設的條件視為當讀到的HEX介於30到37之間時,附上ASCII表供參考如下

範例結果:
歡迎大家在底下留言或到我們的粉絲團留言喔!
====================================
fayalab 粉絲團
FB本篇留言版
沒有留言:
張貼留言