Arduino

tags: 智慧聯網 Arduino UNO ABB Car

簡介

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是什麼?

Arduino是一家製作開源硬體和開源軟體的公司,同時兼有專案和用戶社群,該公司負責設計和製造單板微控制器和微控制器套件,用於構建數位裝置和互動式物件,以便在物理和數位世界中感知和控制物件。該專案的產品是按照GNU寬通用公共許可證(LGPL)或GNU通用公共許可證(GPL)許可的開源硬體和軟體分發的,Arduino允許任何人製造Arduino板和軟體分發。 Arduino板可以以預裝的形式商業銷售,也可以作為DIY套件購買。

特色

Arduino 就像一台沒接滑鼠、沒接螢幕跟喇叭的電腦主機,用小小的體積,把控制晶片和燒錄功能整合在一塊小小的板子上,並且讓Pin腳更容易接線、配合麵包板,可以輕鬆的接上各類感測器或週邊設備,初學者只要會插杜邦線,就能開始進行開發工作,就算沒有任何相關背景的人也可以快速學習。

  • 廉價
  • 跨平台
  • 簡單、清晰的編程方式
  • Open Source電路圖設計 + 程式開發介面

官網連結 : https://www.arduino.cc/
各式硬體種類 : https://www.arduino.cc/en/Main/Products
免費arduino模擬器 :

硬體介紹

Arduino UNO

Fusion 360 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 →

代號 介紹
A Reset 按鈕,重新啟動與重新載入程式。
B USB Type B 連接埠。
C ATMega16U2,USB 轉序列埠晶片。
D 直流電壓模組。
E 7V 到 12V 電壓輸入。
F ICSP,用來燒錄 USB 轉序列埠韌體。
G 左邊與 A5(SCL)相連,右邊與 A4(SDA) 相連。
H AREF,類比輸入埠參考電壓。
I GND。
J 數位輸入輸出腳位 D0 到 D13,其中 D3、D5、D6、D9、D10 與 D11 為 PWM,PWM 用於模擬類比訊號輸 出,而 D10、D11、D12 與 D13 是 SPI 通訊埠。
K ICSP,用來燒錄處理器 bootloader。
L 處理器 ATmega328P。
M 16MHz石英晶體。
N 此與 5V 輸出電壓相連,讓 Arduino 擴充版知道運作電壓。
O 輸入低電壓時,會重置 Arduino,相當於按下 Reset。鍵。
P 由左到右依序為 3.3V 與 5V 電壓輸出與兩個 GND。
Q 電壓輸入。
R 類比輸入腳位 A0 到 A5,其中 A4(SDA) 與 A5(SCL)是 I2C 通訊埠。

Arduino控制板燈號

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 控制板燈號說明如下 :

  • 內建 LED 燈: 可以使用 D13 控制,當 D13 輸出 1 時會點亮此內建 LED 燈,當 D13 輸出 0 時會熄滅此內建 LED 燈。

  • 電源指示燈: 當 Arduino 通電時,點亮此電源指示燈。

  • RX 與 TX 指示燈: 當 D0(RX)與 D1(TX)有資料傳輸時,這兩個指示燈會閃爍。

數位輸入與輸出腳位 Digital Output/Input Pin

數位輸入與輸出腳位只能輸入輸出 0 或 1,Arduino 提供 14 個數位輸入與輸出腳位(D0 到D13)的詳細說明如下表,數位腳位以 D 表示,後面所接數字為腳位編號,D0 表示數位輸出入腳位編號 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 →

腳位 說明
D0 與 D1 序列埠使用,D0 為序列埠接收腳位(RX),D1 為序列埠輸出腳位(TX)。
D3 、D5 、D6 、D9、D10 、 D11 PWM 為類比輸出埠,使用函式analogWrite 可以產生解析度為 8bit(8bit 的數值範圍為 0 到 255)的類比訊號,數值越低電壓越低,如此可以控制電壓的高低。
D10、D11、D12、D13 此為 SPI 通訊埠,只要周邊硬體使用 SPI 通訊埠就可以 Arduino 相連,需要D10(SS)、D11(MOSI)、D12(MISO)與 D13(SCK)這四個連接埠,SPI將於之後章節介紹。
D13 D13 可以控制 Arduino 內建 LED 是否點亮。

類比輸入腳位 Analog Input Pin

6 個類比輸入腳位(A0 到 A5)的詳細說明如下表,這 6 個類比輸入腳位只能輸入不能輸出,解析度為 10bit,輸入值範圍為 0 到 1023,類比腳位以 A 表示,後面所接數字為腳位編號,A0 表示類比輸出入腳位編號 0 的接腳。

腳位 說明
A4、A5 A4(SDA)與 A5(SCL)為 I2C 通訊埠,只要周邊硬體使用 I2C 通訊埠就可以 Arduino相連,A4(SDA)用於傳輸資料,A5(SCL)用於同步訊號。

ICSP(In-Circuit Serial Programming)

ICSP 用於燒錄處理器 ATmega328P 的 bootloader,有了 bootloader 就可以透過 USB 上傳程式到 Arduino,不需要透過專門的燒錄器上傳到 Arduino,市面上的 Arduino 都已經燒錄 bootloader,若要重新燒錄 bootloader,就需要另一塊 Arduino,由其中一塊 Arduino 透過 ICSP 串接另一塊 Arduino 來燒錄 bootloader,ICSP 由 6 個針腳組成功能如下。

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

下載及安裝

步驟一 : 下載

  1. https://www.arduino.cc/en/Main/Software 下載最新版 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 →

  1. 點選「JUST DOWNLOAD」,下載安裝程式。

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. 點選此檔案安裝 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 →

  2. 點選「I Agree」,同意版權宣告。
     

    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 →

  3. 點選要安裝的元件,可以依照預設值全部都安裝,點選「Next」。
     

    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. 選擇安裝的資料夾,點選「Browse…」可 以 重 新 選 擇 安 裝 資 料 夾, 點 選 「Install」安裝 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 →

  5. 完成

    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 →

步驟三 : 檢查是否正確連接上

  1. 使用USB傳輸線連結Arduino與電腦(僅需接上USB線不需要在額外接上電源)
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. 打開裝置管理員,檢查是否正確連接上
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.至Arduino IDE目錄,開啟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 →
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 →

Old_Arduino_Drivers.zip解壓縮手動安裝就行了,或直接執行 dpinst-amd64.exe也可以

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 →

  1. 執行 dpinst-amd64.exe 安裝驅動
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 →
  1. 打開裝置管理員,再次檢查是否正確連接上

程式開發環境介面說明

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的第一個程式 HelloRepeated

  1. 開啟IDE
  2. 撰寫程式碼
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Hello!");
  delay(1000);  //等待1秒
}

3.依照使用的控制器選擇相符合選項

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.選擇Arduino實際的連結位置

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.按下Verify驗證檢查程式能否運行

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 →

6.按下Upload上傳將程式燒錄進Arduino

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 →

7.上傳完成後,按下Serial Monitor序列埠監控視窗即可在螢幕上看到回傳值

基本程式設計介紹

Arduino的程式語言與C語言類似,但是更為簡單容易,以下我們將簡單介紹幾種常用到的語法

  • 程式架構
// 宣告個變數型態 : 於程式一開始宣告所要用到的變數與其資料型態 void setup() { // 當Arduino啟動時須設定的參數 } void loop() { // 當Arduino啟動後會一直執行的動作 }
  • 常用函數

    函數 功能
    pinMode() 設定輸入輸出pin腳
    digitalWrite() 設定數位輸出的high/low
    digitalRead() 讀取數位輸出的狀態
    analogRead() 讀取類比輸出的狀態
    analogWrite() 寫類比的PWM值
    delay() 延遲,以毫秒(ms)為單位
    Serial.begin() 設定鮑率(Baud),設定完成就可以開始使用串列傳輸
    Serial.print() 送資料到電腦
    Serial.read() 從電腦讀資料

LED控制

LED

LED(發光二極體),是一種很熱門的指示燈選擇,因為它的低電源供應及使用期限長。二極體是一個單向的篩選閥,電流只能從一個方向流過,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 →
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 →

正極 : 連接+端。一般來說會是較長的一端。

負極 : 連接-端。一般來說會是較短的一端,而且會較為扁平。

而每種顏色所需的電壓都不太一樣:

紅色LED:2.1 - 2.6V
綠色LED:3.3 - 3.9V
藍色LED:3.2 - 4.0V
白色LED:3.1 - 4.0V

Arduino Uno開發板上只提供5V或3.3V的電,所以基本上是過高了,所以為了避免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 →
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 →

  • 色碼電阻表
    1st條紋 : 1st數字
    2nd條紋 : 2nd數字
    3rd條紋 : 零的個位數
    4th條紋(如果有標示) : 誤差百分比
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 →

四環電阻色碼計算器 : https://www.digikey.tw/zh/resources/conversion-calculators/conversion-calculator-resistor-color-code

認識麵包版

同一個連通方向,表示每個孔間都是導通的,有的麵包板沒有上圖中藍與紅的部份,如果有的話,通常藍那列會拿來接 GND,紅的那列會拿來接電源正端。

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 →

範例一 : 一個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 →

int ledPin1 = 13; ​void setup() { ​ // initialize digital pin LED_BUILTIN as an output. ​ pinMode(ledPin1, OUTPUT); ​} ​ ​// the loop function runs over and over again forevervoid loop() { ​ digitalWrite(ledPin1, HIGH); // turn the LED on (HIGH is the voltage level) ​ delay(1000); // wait for a second ​ digitalWrite(ledPin1, LOW); // turn the LED off by making the voltage LOW ​ delay(1000); // wait for a second ​}
  • 電路圖

Tips

-LED 長腳接正極,短腳接地線(GND)。

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 →

-把開發板上的GND接到LED的負極,電阻不分正負極,所以只要其中一腳接到LED的正極,電阻另一腳接到開發板的5v。
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 →

注意

LED不能單獨接到電源上,當LED兩端的電壓超過2V時,就會有龐大的電流流過LED,可能會造成LED燒毀,為了讓LED發光且不會燒毀,串聯一個電阻就可以了,而這個保護LED的電阻叫做"限流電阻"。
(但是電阻不是隨便拿就可以,需要經過簡單的計算算出適合的電阻,一般Arduino用的LED限流電阻大約是220~470都可以)

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 →

範例二 : 兩個LED交互閃爍

  • 程式
int ledPin1 = 12; int ledPin2 = 13; void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(ledPin1, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(ledPin1, LOW); // turn the LED off by making the voltage LOW digitalWrite(ledPin2, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(ledPin2, LOW); // turn the LED off by making the voltage LOW delay(1000); }
  • 電路圖
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 →

呼吸燈

讓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 →

PWM

LED不只能有明滅兩種狀態,我們還可以讓它有最亮、普通亮、不太亮、稍暗、很暗、最暗、等等狀態,Arduino利用所謂的pulse width modulation (PWM)技術,可以做到這點。

簡單來說就是透過數位輸出的開和關之間的時間比來做控制,但這個一開一關的週期時間很短,從LED來說,我們眼睛看到的不會是閃爍,因為太快了,反而是變亮或變暗。

以arduino 的PWM輸出頻率500Hz來說,一個開關的週期為2毫秒

若100%Duty Cycle-analogWrite(255),也就是全開,沒有關,最後輸出為5V。

若50%Duty Cycle-analogWrite(127),會是1毫秒開,1毫秒關,最後輸出為2.5V。

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 →
  • 程式
    範例檔案路徑 : 檔案>範例>01.Basics>fade
int ledPin = 9; //設定led pin, PWM pin有~3,~5,~6,~9,~10,~11 int brightness = 0; //LED亮度變數 int fadeAmount = 5; //亮度變化變數 void setup() { //設定led pin為OUTPUT pinMode(ledPin, OUTPUT); } void loop() { //PWM輸出 analogWrite(ledPin, brightness); //在每次回圈brightness都會累加fadeAmount brightness = brightness + fadeAmount; //到最亮或最暗時反轉 if (brightness <= 0 || brightness >= 255) { fadeAmount = -fadeAmount; } //延遲30ms,使呼吸燈效果更好 delay(30); }
  • 電路圖

這裡要注意的是,不是每隻腳都有PWM輸出,電路板上有 ” ~ ” 符號的才有PWM輸出。

-從Arduino板子的腳位9接到220Ω電阻、再接到LED的長腳(陽極、正極)、LED的短腳接地。

-我們將利用數位腳位9,以PWM控制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 →

按鍵控制

範例一 : 按著才會亮

  • 程式碼

範例檔案路徑 : 檔案>範例>02.Digital > Button

/* Button Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 2. The circuit: - LED attached from pin 13 to ground - pushbutton attached to pin 2 from +5V - 10K resistor attached to pin 2 from ground - Note: on most Arduinos there is already an LED on the board attached to pin 13. created 2005 by DojoDave <http://www.0j0.org> modified 30 Aug 2011 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Button */ // constants won't change. They're used here to set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }
  • 電路圖

按鈕一般建議是10K~20K歐姆都可以,這裡我們用的是10K的。

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 →

範例二 : 按一下亮,按一下暗

  • 程式碼
    範例檔案路徑 : 檔案>範例>02.Digital > Debounce
/* Debounce Each time the input pin goes from LOW to HIGH (e.g. because of a push-button press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a minimum delay between toggles to debounce the circuit (i.e. to ignore noise). The circuit: - LED attached from pin 13 to ground - pushbutton attached from pin 2 to +5V - 10 kilohm resistor attached from pin 2 to ground - Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you don't need any extra components for this example. created 21 Nov 2006 by David A. Mellis modified 30 Aug 2011 by Limor Fried modified 28 Dec 2012 by Mike Walters modified 30 Aug 2016 by Arturo Guadalupi This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Debounce */ // constants won't change. They're used here to set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin // Variables will change: int ledState = HIGH; // the current state of the output pin int buttonState; // the current reading from the input pin int lastButtonState = LOW; // the previous reading from the input pin // the following variables are unsigned longs because the time, measured in // milliseconds, will quickly become a bigger number than can be stored in an int. unsigned long lastDebounceTime = 0; // the last time the output pin was toggled unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); // set initial LED state digitalWrite(ledPin, ledState); } void loop() { // read the state of the switch into a local variable: int reading = digitalRead(buttonPin); // check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited long enough // since the last press to ignore any noise: // If the switch changed, due to noise or pressing: if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer than the debounce // delay, so take it as the actual current state: // if the button state has changed: if (reading != buttonState) { buttonState = reading; // only toggle the LED if the new button state is HIGH if (buttonState == HIGH) { ledState = !ledState; } } } // set the LED: digitalWrite(ledPin, ledState); // save the reading. Next time through the loop, it'll be the lastButtonState: lastButtonState = reading; }
  • 電路圖
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 →

範例三 : 使用library-bounce master

  1. 下載Bounce-master.zip
    下載連結(請使用學校帳號登入)
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 →

2.開啟IDE至草稿碼(Sketch)>匯入程式庫(Import Library)>加入.ZIP程式庫(Add Lidrary)

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 →

3.將剛剛下載的zip檔匯入

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. 匯入成功
    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 →

  1. 即可使用
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 →

  • 程式碼
#include <Bounce.h> Bounce bouncer = Bounce(2, 50); static int ledStatus = LOW; void setup(){ Serial.begin(115200); pinMode(2, INPUT); pinMode(13, OUTPUT); } void loop(){ if(bouncer.update() == true && bouncer.read() == HIGH) { ledStatus = ledStatus == HIGH ? LOW : HIGH; digitalWrite(13, ledStatus); Serial.println(ledStatus); } }

內建物件 (Built-in Objects)

內建物件就使已經放在Arduino IDE中的那些函式。
使用時,在程式前面寫成the #include statement, 例如 #include <servo.h>

這個動作便是將servo.h 物件加到程式中。

  • 電路圖
    同上

可變電阻

認識可變電阻

可變電阻,也有人稱可調電阻,英文叫Potentiometer,簡稱Pot,也有人叫它Variable resistance,基本上,它就是電阻,但可以調整電阻值。雖然名叫電阻,但其實在許多電器的設計上都被用來當作使用介面的旋鈕,大家一定都用過的,像是音量、溫度等等,只是上面加了漂亮的蓋子,外觀上看不出來而已。

可變電阻上會標示歐姆數,可分為 A(對數型)、B(線性型)與 C(反對數型),分別代表旋轉角度與歐姆值的變化關係,以我手邊的這顆可變電阻為例,標示為 A10K,表示為 A 型可變電阻,最高電阻值為 10 KΩ。

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 →

可以看到可變電阻有三個接腳,外側兩個接腳兩邊都可以分別接 GND(負) 與電源(正),而中間的接腳與另兩個接腳之間的電阻值,會因為旋轉角度而不同。

類比數位轉換器(Analog to Digital Converter, ADC)

可使用腳位 A0 到 A5 來個 ADC 輸入,可讀取 0 到 5V 的電壓輸入,解析度為 10 個位元,也就是讀取而得到的值是 0 到 1023。

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 →

範例程式 : 類比輸入、類比輸出,利用可變電阻控制LED明暗變化

  • 程式碼

範例檔案路徑 : 檔案>範例>03.Analog>AnalogInOutSerial

/* Analog input, analog output, serial output Reads an analog input pin, maps the result to a range from 0 to 255 and uses the result to set the pulse width modulation (PWM) of an output pin. Also prints the results to the Serial Monitor. The circuit: - potentiometer connected to analog pin 0. Center pin of the potentiometer goes to the analog pin. side pins of the potentiometer go to +5V and ground - LED connected from digital pin 9 to ground created 29 Dec. 2008 modified 9 Apr 2012 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/AnalogInOutSerial */ // These constants won't change. They're used to give names to the pins used: const int analogInPin = A0; //類比輸入的PIN腳 const int analogOutPin = 9; // 類比輸出的PIN腳,接到LED的正極 int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out) void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); } void loop() { // read the analog in value: 讀取類比輸入的值會得到0~1023 sensorValue = analogRead(analogInPin); // map it to the range of the analog out: 將0~1023轉化成0~255 outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: 將結果用PWM的方式輸出給LED,改變亮度 analogWrite(analogOutPin, outputValue); // print the results to the Serial Monitor: Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue); // wait 2 milliseconds before the next loop for the analog-to-digital // converter to settle after the last reading: delay(2); }
  • 電路圖
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 →

  • 有源蜂鳴器 : 內建了一組固定的頻率,只要接通電源,就會發出固定的音調。

  • 無源蜂源器 : 必須透過程式,告訴它頻率,我們才能得到所需的音調,但我們就可以利用它來播放簡單的旋律了!

範例程式 : 有源蜂鳴器

  • 程式碼

簡單的 Da Da Da

void setup() { pinMode(8,OUTPUT); } void loop() { digitalWrite(8,HIGH); delay(1000); digitalWrite(8,LOW); delay(2000); }

範例檔案路徑 : 檔案>範例>02.Digital>toneMelody

/* Melody Plays a melody circuit: * 8-ohm speaker on digital pin 8 created 21 Jan 2010 modified 30 Aug 2011 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Tone */ #include "pitches.h" // notes in the melody: int melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 }; // note durations: 4 = quarter note, 8 = eighth note, etc.: int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 }; void setup() { // iterate over the notes of the melody: for (int thisNote = 0; thisNote < 8; thisNote++) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(8); } } void loop() { // no need to repeat the melody. for (int thisNote = 0; thisNote < 8; thisNote++) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(8); } }
  • 電路圖
    這個例子我們蜂鳴器叫1秒鐘後,停2秒,然後不斷重複;有源蜂鳴器的程式就這樣而已,非常容易。
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 →

光敏電阻(Photoresistor),會依光照的強度去改變電阻值,常被用在一些和明暗有關係的開關上,使用上基本和可變電阻、聲音模組差不多,只要線接對了,程式寫法是一樣的,也是透過類比輸入的方式讀取數值

  • 程式碼
void setup() { Serial.begin(9600); pinMode(3,OUTPUT); } void loop() { int sensorValue = analogRead(A0); Serial.println(sensorValue); if(sensorValue < 200){ //當光敏電阻給的值小於基準值。這個基準值依個別情況修改 digitalWrite(3,HIGH); //燈亮 }else{ digitalWrite(3,LOW); //燈滅 } delay(10); }
  • 電路圖

基準值以這個例子來說,正常讀取的值是900多,基準值我就設定900,這樣只要用手去擋住光敏電阻的上方,讓它所在的區域變暗了,數值必然低於900,那麼LED就亮;手一移開,數值又重回到900多,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 →

溫溼度感測器

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 →

市面上目前大家能找到的DHT有兩種:一種是單純DHT沒背板的,一種是有焊背板的。差別在於焊了背板就可以省掉加電阻這個麻煩,也少一個腳位,方便很多。

1.下載Adafruit_Sensor-master.zipDHT-sensor-library-master.zip(請用學校帳號登入)

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 →

2.開啟IDE至草稿碼(Sketch)>匯入程式庫(Import Library)>加入.ZIP程式庫…(Add Lidrary…)

3.將剛剛下載的zip檔匯入

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.匯入成功

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 →

  • 程式碼
#include <DHT.h> //加入DHT.h程式庫 #define ledPin 13 unsigned long duration=100; unsigned long frequency=3000; float hum; //濕度 float temp; //溫度 int light; //亮度 DHT dht(6,DHT11); //宣告dht物件使DHT類別並定義感測器針腳及類型 void setup() { // 建立初始化區塊 dht.begin(); //初始化dht11 pinMode( ledPin, OUTPUT); Serial.begin(9600); // 設定鮑率為 9600 bps } void loop() { // 主程式迴圈自動重複動作 hum=dht.readHumidity(); //將讀取到的溼度資料儲存在hum變數中 temp=dht.readTemperature(); //將讀取到的溫度資料儲存在temp變數中 light=analogRead(A0); //將讀取到的亮度資料純存在light變數中 if (temp > 20) { digitalWrite(ledPin, HIGH); tone(8,frequency,duration); } else { digitalWrite(ledPin, LOW); } Serial.print("Temperature: "); //將資料顯示在Serial Monitor上 Serial.print(temp); Serial.print("oC Humidity: "); Serial.print(hum); Serial.print("% Light level: "); Serial.println(light); delay(1000); //暫停1秒 }
  • 電路圖

大家可以試著對DHT哈氣,會發現濕度和溫度都會產生變化。

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 →

紅外線接收器與發射器

人類可見光約為 390 到 700 nm(奈米),其中可見的紅光波長約為 620 至 750 nm 左右,而波長超過 760 nm 的光,人類無法感知,稱為紅外線(infrared、IR),俗稱紅外光。

電視機、冷氣機等一般常使用 940 nm 波長的紅外光,以避免太陽光、室內光源等發出的紅外光干擾,其他還有 850 nm、860 nm 等其他波長,最好能讓接收器與發射器規格一致,以免造成誤判。

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 →

上圖左側是紅外線接收器,我手邊這隻三個接腳由左至右分別是訊號、GND 與電源,有的接收器 GND 與電源線會相反,使用時要確認一下。

由於生活周遭物品也會不定時地散發紅外光,紅外線接收器設計為只對特定頻率有反應,常見使用的是 38k Hz;紅外線發射器就是個 LED,可發出特定波長的紅外線,在 Arduino,可以使用 tone 函式(是之前談蜂嗚器時有提過)指定以特定頻率(像是 38k Hz)發射紅外線。

範例程式 : 紅外線接收器

1.下載Arduino-IRremote-master.zip(請用學校帳號登入)

2.開啟IDE至草稿碼(Sketch)>匯入程式庫(Import Library)>加入.ZIP程式庫…(Add Lidrary…)

3.將剛剛下載的zip檔匯入

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.匯入成功

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 →
,點選該函式 ,即可使用#include <IRremote.h>

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 →

  • 程式碼
#include <IRremote.h> int RECV_PIN = 11; //紅外線接收器角位 IRrecv irrecv(RECV_PIN); //decode_results results;//接收的資料結構 void setup() { Serial.begin(9600); pinMode(13,OUTPUT); irrecv.enableIRIn(); //啟用紅外線接收器 } void loop() { if (irrecv.decode()) { // 如果可以解譯 Serial.print("收到資料為"); //Serial.print(irrecv.results.value, HEX); Serial.println(irrecv.decodedIRData.decodedRawData, HEX); if (irrecv.decodedIRData.decodedRawData == 0x39D41DC6){ digitalWrite(13,HIGH); delay(1000); //digitalWrite(13,LOW); //delay(1000); } else { digitalWrite(13, LOW); delay(1000); } irrecv.resume(); //準備接收下一個訊息 } }
  • 電路圖
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 →

打開監控視窗,把遙控器拿來亂按,就會顯示你所按的按鍵代碼,用16進位輸出。這時可以記起來會用到的按鍵,分別是哪些代碼,這樣就能在程式中加上簡單的判斷了!

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 →

人體紅外線感測器

人體紅外線感測器 ( PIR Motion Sensor ) ,是用來做為警報器常見的元件之一,用途在於偵測會發出紅外線的物體的移動,而人體本來就會發射紅外線,面積又大,自然很容易就被偵測到,所以這個感測器拿來做為防盜等用途就非常適合。

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 →

SR501有兩個旋鈕可以調整 :

  • 敏感度(Sensitivity)
  • 延遲時間(Delay)

建議大家先調整一下,把延遲時間(Delay)先逆時針調到最底,也就是最低值,敏感度(Sensitivity)先順時針調到底,也就是最高值,這樣在測試時比較容易看到效果。等程式正常運作後,再自己試著調到適合的程度。

PIR Motion Sensor的接線很簡單,1正1負,再一個訊號線。程式方面就更簡單了,訊號線只會回傳0或1,代表有沒有偵測到物體在移動,就這樣,搞定!未來大家就可以結合蜂鳴器或燈號,做成完整的警報裝置囉!

  • 程式碼
int sensor = 9; //訊號線 void setup() { Serial.begin(9600); pinMode(sensor, INPUT); } void loop() { int moving = digitalRead(sensor); //讀取D9是否有偵測到物體移動 if(moving==1){ //如果有物體移動 Serial.println("Something is moving"); } }
  • 電路圖
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 →

MQ-4 氣體感測器所使用的氣敏材料是在清潔空氣中電導率較低的二氧化錫 (SnO2)。當感測器所處環境中存在可燃氣體時,感測器的電導率隨空氣中可燃氣體濃度的增加而增大。使用簡單的電路即可將電導率的變化轉換為與該氣體濃度相對應的輸出信號。

MQ-4氣體感測器對甲烷的靈敏度高,對丙烷、丁烷也有較好的靈敏度。這種感測器可檢測多種可燃性氣體,特別是天然氣,是一款適合多種應用的低成本感測器。

其他氣體類的傳感器,種類很多,包括:

MQ-2:煙霧氣體(液化氣 / 丙烷 / 氫氣)感測器模組
MQ-3:酒精感測器模組
MQ-4:天然氣、甲烷感測器模組
MQ-5:液化氣、天然氣、瓦斯氣體感測器
MQ-6:丙烷、液化氣氣體感測器模組模組
MQ-7:一氧化碳感測模組
MQ-8:氫氣氣體感測器模組空氣檢測模組
MQ-9:一氧化碳 / 甲烷 / 液化氣可燃氣體感測器模組
MQ-131:HO3 臭氧氣體感測模組臭氧傳感器模組
MQ-135:空汙檢測(氨 / 苯 / 酒精)有害氣體感測器模組
MQ-136:硫化氫液化氣煙霧感測器模組
MQ-137:氣體 Ammonia 阿摩尼亞 NH3 氨氣檢測模組
MQ-138:甲醛偵測感測器模組甲醛酮醇類氣體傳感器模組
MQ-139:氟利昂(氟氯烷)檢測感測器氣體檢測模組
MQ-214:甲烷感測器 可燃氣感測器模組