[2] Arduino講義: 環境與跑馬燈+按鍵+多工七段顯示器+鍵盤
Install the Arduino
-
至Arduino官方網站下載開發環境: (https://www.arduino.cc/en/Main/Software)
-
開起「裝置管理員」,將 Arduino 接上,看一下是否有新增 USB 序列裝置(COM6),不一定為 COM6。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
若「USB序列裝置」出現無法辨識的裝置
方法一 : 按滑鼠右鍵,選擇「更新驅動程式」,並點選「自動搜尋更新的驅動程式軟體」,可以排除問題。
方法二 : 按滑鼠右鍵,選擇「更新驅動程式」,並點選「瀏覽電腦上的驅動程式軟體」,瀏覽下載Arduino的地方(arduino-1.8.8),找到drivers檔,點選並安裝。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
方法三 : 搜尋COM port安裝檔,下載後執行 https://sparks.gogo.co.nz/ch340.html
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
開啟Arduino IDE
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
按下工具→序列埠,同時將arduino接上,選擇Uno,即可以使用。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 在「開發板」的部分記得確認是Arduino/Genuino Uno。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Arduino Common Function
1.setup()
設定程式參數,如:指定接腳的輸入輸出狀態,setup()裡的程式碼只會執行一次。
2.loop()
在loop()函式中的程式碼將會不停地重複執行,直到電源關閉為止。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
通用輸入/輸出,每個 GPIO 端口可透過軟件分別配置成 INPUT 或者 OUTPUT ,其接腳可以供使用者由程式控制自由使用。
- Input mode: MCU 透過 GPIO 腳從外面收資料/信號,可以進行外接感測器(溫度、紅外線感測器等)狀態、高低電位等資訊的獲取。
- Output mode: MCU 透過 GPIO 腳將資料/信號送出,可用於控制LED、蜂鳴器及伺服馬達…等。
- GPIO 端口可以當 I2C(LCD 16x2)、SPI (10~13)、UART(Bluetooth)…等多種用途,也能作為 IRQ (中斷要求)(2、3)的信號。
3.pinMode(pin,mode)
用於設定接腳模式,該函數無返回值,pin代表所要配置的接腳,mode代表設置的模式-INPUT或OUTPUT。
Ex:
4.digitalWrite(pin,value)
函數有兩個參數 digitalWrite(pin 接腳,電壓值), 電壓值為HIGH(高電位)與LOW(低電位)。
Ex:
5.digitalRead(pin)
在接腳為輸入的情況下,可以獲取接腳的電壓情況—HIGH或LOW。
6.delay(ms)
設定程式運行停止時間長度,單位為ms,故delay(1000)為延遲1秒。
7.millis()
millis()函數會回傳Arduino從執行程式碼開始至目前為止的milliseconds數值,其資料型態為unsigned long,該數值大約在50天之後會溢位(overflow),屆時會從0開始重新計數。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
以上方程式碼為例,每一秒印出millis()的回傳值,利用delay(1000)產生1000毫秒的延遲。
然而,相鄰的兩個數值的差值可能並未完全為1000,則是Serial.print()印出資料需要的時間差。
Serial Library
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
1. Serial.begin()
啟動Serial Port並設定鮑率(baud rate)
Ex:
2. Serial.print()
將字串透過TX腳位傳遞出去,並且顯示在Serial Monitor中
3. Serial.println()
傳送資料到外部電腦並切換至下一行
4. Serial.write()
以二進位碼(byte)輸出
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
5. Serial.read()
讀取進來的第一個位元組 (first incoming byte)。
6. Serial.available()
取得 Serial Port 可讀取的資料位元組數目 (number of bytes),如果 Serial port 有資料進來,Serial.available() 會回傳大於 0 的數值。
通常我們會使用Serial.available()來檢查是否有資料進來,在使用Serial.read()把資料讀取出來放到變數後供後續使用。例如:
<<Note>>
Arduino其他相關語法請參考:Arduino語言手冊
Arduino講義:跑馬燈 + 按鍵 + 多工七段顯示器 + 4 x 4鍵盤
1.跑馬燈(Electronic Marquee)
模組說明
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
<<Note>>
需要限流電阻(大概 220Ω 或 330Ω),使 LED 流進電流不超過20mA。
Arduino LED Blink
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
程式說明
LAB 1-1
- 實驗目的:學習對Arduino的GPIO及LED基本控制方式。
- 實驗目標:設定5個GPIO來實現跑馬燈,模式如範例影片所示
- 參考程式碼(可任意新增改寫)
- 範例影片
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
LAB 1-2 (Bonus)
- 實驗目的:學習對Arduino的GPIO及LED基本控制方式。
- 實驗目標:設定5個GPIO來實現跑馬燈,模式如範例影片所示
- 參考程式碼(可任意新增改寫)
- 範例影片
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →

開關接法

若未按下開關,Arduino的接腳沒接地,但也沒有接到高電位。輸入的訊號會在0到1之間飄移,造成浮動訊號,Arduino就無法正確判斷輸入值。

若開關沒有被按下,Pin2將會透過10kΩ接地,讀取到低電位(LOW);按下開關時,5V電源將流入Pin2,產生高電位(HIGH)。

若將電阻接到電源,則此電阻稱為「上拉(pull-up)電阻」(在開關接通時,輸入為「低電位」)。
使用Arduino內部的上拉電阻
Arduino內部的pin腳考量到拿來做數位輸入,有內建上拉電阻(除了D13腳位),可取代外接上拉電阻。
- 實驗目的:知道上拉電阻與下拉電阻的接法差別,並用Button控制LED
- 實驗目標:按上拉電阻的Button時,LED向左一顆亮;按下拉電阻的Button時,LED向右邊一顆亮。
- 參考程式碼(可任意新增改寫)
- 範例影片
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
彈跳問題
機械式開關在切換的過程中,訊號並非立即從1變成0或從0變成1,而是會經過如下圖一般有忽高忽低的情況。
雖然彈跳的時間很短暫,但arduino仍會讀取到其連續變化的開關訊號,而導致誤差。

消除彈跳(de-bouncing)方法
在發現訊號開始變化時先讀取,在經過一段時間後,若訊號仍為HIGH,則視為按下第二次按鈕。
LAB 2-2 de-bouncing
- 實驗目的:解決Button彈跳問題
- 實驗目標:無論長按或短按,按一下按鈕改變LED亮暗,且沒有不規則閃爍的問題;另外一顆LED燈,保持以每秒亮按的頻率持續閃爍,不受按壓按紐影響 Hint:不使用delay()函式
- 參考程式碼(可任意新增改寫)
- 範例影片:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Arduino講義:多工七段顯示器與掃描鍵盤
3. 多工七段顯示器(Multiplexed Seven-Segment display)
七段顯示器


<<Note>>
注意!!需額外增加限流電阻,否則內部可能會燒毀!!

依據電源連接方式的不同,七段顯示器可以分成「共陽極」與「共陰極」兩種:
共陰極代表所有的LED接地端都相連,故LED的另一端接「高電位」就會發光;相反地,共陽極則是輸入「低電位」來發光。
十進位數 |
a |
b |
c |
d |
e |
f |
g |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
2 |
1 |
1 |
0 |
1 |
1 |
0 |
1 |
3 |
1 |
1 |
1 |
1 |
0 |
0 |
1 |
4 |
0 |
1 |
1 |
0 |
0 |
1 |
1 |
5 |
1 |
0 |
1 |
1 |
0 |
1 |
1 |
6 |
1 |
0 |
1 |
1 |
1 |
1 |
1 |
7 |
1 |
1 |
1 |
0 |
0 |
0 |
0 |
8 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
9 |
1 |
1 |
1 |
1 |
0 |
1 |
1 |
多工七段顯示器



多工七段顯示器 |
Arduino |
0 |
Pin10 |
1 |
Pin11 |
2 |
Pin12 |
3 |
Pin13 |
a |
Pin2 |
b |
Pin3 |
c |
Pin4 |
d |
Pin5 |
e |
Pin6 |
f |
Pin7 |
g |
Pin8 |
dp |
Pin9 |
LAB 3-1 多工七段顯示器
- 實驗目的:學習對Arduino的GPIO與多工七段顯示器掃描基本控制方式
- 實驗目標:控制多工七段顯示器顯示"2021"
- 參考程式碼(可任意新增改寫)
const int SEG_COM[4] = {10, 11, 12, 13};
const int SEG_data[10][8] = {{1, 1, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 0, 1, 0},
{1, 1, 1, 1, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{1, 0, 1, 1, 0, 1, 1, 0},
{1, 0, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 0, 0, 1, 1, 0}};
int display[4] = {2, 0, 2, 1};
void setup() {
for(int i=2; i<=13; i++){
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
}
}
void loop() {
for(int i=0; i<=3; i++){
digitalWrite(SEG_COM[i], LOW);
SEG_Drive(disp[i]);
delay(5);
digitalWrite(SEG_COM[i], HIGH);
}
}
void SEG_Drive(char number){
}
- 範例影片:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
LAB 3-2 多工七段顯示器數數
- 實驗目的:學習對Arduino的GPIO與多工七段顯示器掃描基本控制方式
- 實驗目標:控制多工七段顯示器 每隔一秒計數加一
- 參考程式碼(可任意新增改寫)
const int SEG_COM[4] = {10, 11, 12, 13};
const int SEG_data[10][8] = {{1, 1, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 0, 1, 0},
{1, 1, 1, 1, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{1, 0, 1, 1, 0, 1, 1, 0},
{1, 0, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 0, 0, 1, 1, 0}};
int disp[4];
int number = 0;
int timer = 0;
void setup() {
for(int i=2; i<=13; i++){
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
}
}
void loop() {
number_transfer(number);
}
void number_transfer(int Num){
}
void SEG_Drive(char number){
}
- 範例影片:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
4. 4x4鍵盤(Keypad)

按鈕偵測與掃描原理(1x3簡化版)

假設開關的「行1」~「行3」輸入端全部都輸出高電位,則無論開關是否被按下,Arduino都將會接收到高電位。因此,為了偵測到使用者按下的開關鍵,撰寫程式時必須依序將「行1」~「行3」設置成低電位來檢測。
- 輪到「行1」腳被掃描時,「行1」腳輸入低電位,但A開關沒有被按下,所以Arduino的微控腳接收到高電位(1)

- 在掃描到「行2」時,「行2」腳輸入低電位,且Arduino的微控腳也接收到低電位(0),因此可以判斷出「行2」的B開關被按下。

- 輪到「行3」腳被掃描時,「行3」腳輸入低電位,但C開關沒有被按下,所以Arduino的微控腳接收到高電位(1)

實際上的使用需要運用到雙重迴圈(4x4),才能夠分批掃描到每一列:

實際腳位配置
4x4 Keypad |
Arduino |
1(ROW1) |
Pin A0 |
2(ROW2) |
Pin A1 |
3(ROW3) |
Pin A2 |
4(ROW4) |
Pin A3 |
5(COL1) |
Pin 2 |
6(COL2) |
Pin 3 |
7(COL3) |
Pin 4 |
8(COL4) |
Pin 5 |
LAB 4-1 掃描式鍵盤
- 實作目的:學習如何使用掃描式鍵盤
- 實作目標:在不使用<Keypad.h>的情況,以Keypad鍵入資料,將資料透過UART傳輸至內建的Serial Monitor
- 參考程式碼(可自行改寫)
- 範例影片:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
匯入Keypad函式庫
Keypad程式庫的運作方式與LAB 4-1 大致相同,且多了除彈跳(de-bouncing)的部分。LAB 4-1有助於了解掃瞄鍵盤的原理,實際使用上仍是Keypad程式庫為主。
Keypad Library下載


使用Keypad函式庫,程式碼需定義按鍵模組的行(col)、列(row)、連接Arduino的腳位以及按鍵所代表的字元。

LAB 4-2 Keypad 輸出字串
- 實驗目的:學習如何使用掃描式鍵盤及其應用
- 實驗目標:利用Keypad輸入按鍵,並且將值暫存在buffer中,按下 '#' 後會將之前輸入的字元依序Print到Serial Monitor上。
- 範例影片:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
LAB 4-3 碼錶 (Bonus)
-
實驗目的:學習如何使用掃描式鍵盤與七段顯示器之結合應用
-
實驗目標:利用Keypad輸入按鍵,按 'A' 會進入計時模式,按 'B' 則會進入倒數計時模式,按 'C' 為開始,按 'D' 為暫停,按 '*' 為重新開始,按 '#' 為離開並進入選擇模式。(註:進入倒數計時模式時須先輸入欲倒數秒數)
-
參考pin腳
多工七段顯示器 |
Arduino |
4x4 Keypad |
Arduino |
0 |
Pin10 |
Row1 |
A0 |
1 |
Pin11 |
Row2 |
A1 |
2 |
Pin12 |
Row3 |
A2 |
a |
Pin2 |
Row4 |
A3 |
b |
Pin3 |
Col1 |
Pin13 |
c |
Pin4 |
Col2 |
Pin9 |
d |
Pin5 |
Col3 |
A4 |
e |
Pin6 |
Col4 |
A5 |
f |
Pin7 |
|
|
g |
Pin8 |
|
|
註 : 要使用 Pin 0 & 1 也可以,但不能使用Serial.print()等函式
-
實驗結果:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
課後習題
-
Arduino微處理器不僅包含MCU,還內建記憶體、類比/數位訊號轉換器以及周邊控制介面,相當於將完整的電腦功能塞入一個矽晶片中,所採用的微處理器是ATmega328P。根據下圖,標示在Arduino UNO板上的digital 與 analog I/O的位置、數量以及相關用途(如:通訊、中斷…)。

-
(1)描述機械式開關的彈跳現象
(2)在ATmeg328p微控器的數位接腳有內建上拉電阻,預設沒有啟用,該上拉電阻值介於20kΩ至50kΩ之間;而一般外接10kΩ的上拉電阻,甚至在要求高反應速率的電子訊號切換時會使用4.7kΩ或是1kΩ的上拉電阻。試問:上拉電阻值的高低對於處理雜訊干擾的能力之間的關係?
-
描述多工七段顯示器及鍵盤掃描的工作原理
-
心得
作業繳交格式
W2結報_第X組.zip
壓縮檔裡包含:
1.W2結報.pdf
2.資料夾:W2
Lab1-1.ino
Lab1-2.ino
…