SlideShare a Scribd company logo
1 of 113
Download to read offline
2014.08.04~05
主辦:藝術無國界
贊助機構:澳門文化局
課程講師:張傑名(Jimmy)
ARDUINO 自造機工作坊
What is Arduino?
Arduino robotics
VertiBOT
8x8x8 LED
EyeWriter
Secret Knock Door Lock
Turn Signal Biking Jacket
Botanicalls
Sigh Collector
Tweet a watt
Bubblesteen
Air Drums
Cloud
Flappy Bird in a Box
Mario in a Box
Super Angry Birds
Moody Useless Machines
Knock Knock
Moving Mario
Pinokio
Laser Harp
Football Hero
CirCAT Board
MaKey MaKey
Mimicry
Reference
What is Arduino?
Arduino is an open-source
electronics platform based on
easy-to-use hardware and
software. It's intended for anyone
making interactive projects.
Massimo Banzi
David Cuartielles
Board
http://arduino.cc/en/Main/ArduinoBoardUno
Schematic
http://arduino.cc/en/Main/ArduinoBoardUno
Types of Arduino
Arduino UNO
Arduino MEGA
Arduino NANO
Arduino MINI
LilyPad
Arduino Shield
http://shieldlist.org
Shield List
http://shieldlist.org
Ethernet
Motor Shield
Wave Shield
RFID Shield
XBee
Extreme Sensor Kit
Where To Buy?
Handbook
Handbook
Circuit Sketch
Fritzing
123D Circuits
Electronics Toolkit
Specification
ATmega328 ( Single Chip Microcomputer )
Flash Memory : 32KB

儲存⾃自定程式碼及開機程式
!
SRAM : 2KB
暫存程式執⾏行中所需要的資料


EEPROM : 1KB
儲存程式永久資料
Clock Speed : 16 MHz
Analog Input x 6 pins
Digital I/O Pins : 14
One Chip Arduino
PWM x 14 pins
Digital I/O x 14 pins
Analog Input x 6 pins
ATMEGA328P
Type-B USB
DC Jack
Input / Output
Development Tools
Lets Get Rocked…
http://arduino.cc/en/Main/Software
ctrl + u
Done compiling
Hello World
communication
Analog
I/O
Digital
I/O
重
點
精
華
Digital I/O x 14 pins
ATMEGA328P
A.標準LED
B.雙⾊色或三⾊色(RGB)LED
C.閃光LED
D.紅外線(IR)LED
E.⻝⾷食⼈人⿂魚LED(⾼高功率LED)
F.表⾯面黏著型LED(SMD)
G.紫外線LED(⿊黑光LED)
LED
+
_
A B D E
F
+
_
電路符號
A.碳膜電阻
B.⽔水泥電阻
C.可變電阻(電位計)
D.滑桿電阻
Resistor
電路符號
A
B
C
D
A.按鈕/鍵開關
B.⽔水銀開關
C.微動開關
D.震動開關
E.磁簧開關
F.搖頭開關
Switch/Button
電路符號
A
B
C
D
E F
數位輸出
像電池⼀一樣可提供⾼高電位(正極)

每個腳位最⼤大可輸出40mA

加⼊入負載元件連接⾄至地極

便可形成⼀一個可控制的迴路
1 0 1 0 1 0 1 0
0V
5V
Digital Output
int led = 13;
!
void setup() {
pinMode(led, OUTPUT);
}
!
void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
Digital Output
int LEDs[] = {9,11,13};
int total = sizeof(LEDs);
int index = 0;
!
void setup() {
for(int i=0; i<total; i++) {
pinMode(LEDs[i], OUTPUT);
}
}
!
void loop() {
for(int i=0; i<total; i++) {
digitalWrite(LEDs[i], LOW);
}
digitalWrite(LEDs[index], HIGH);
index++;
if(index == total) index=0;
delay(200);
}
7-Segment Display
共負或共正
共負或共正
共負:負極都接在⼀一起
共正:正極都接在⼀一起
Digital Output
int LEDs[] = {2,3};
int total = sizeof(LEDs);
int index = 0;
!
void setup() {
for(int i=0; i<total; i++) {
pinMode(LEDs[i], OUTPUT);
}
}
!
void loop() {
for(int i=0; i<total; i++) {
digitalWrite(LEDs[i], LOW);
}
}
共正極
Shift Register
輸⼊入間隔
最後同時⼀一次輸出
74HC595
10110111
1
0
1
1
0
1
1
1
Digital Output
int dataPin = 9;
int gatePin = 11;
int clockPin = 12;
!
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(gatePin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
!
void loop() {
digitalWrite(gatePin, LOW);
shiftOut(dataPin, clockPin,
LSBFIRST, B00000011);
digitalWrite(gatePin, HIGH);
}
12
8
3456 7B00000011
LSBFIRST
18
數位輸⼊入
可提供低電位(接地)

每個腳位最⼤大可輸⼊入40mA

通常與開關連接形成迴路
來控制輸出或通訊
1 0 1 0 1 0 1 0
0V
5V
13
pin D11
9
按住亮,放開暗
Digital Input
int led = 13;
int send = 11;
int receive = 9;
!
void setup() {
pinMode(led, OUTPUT);
pinMode(send, OUTPUT);
pinMode(receive, INPUT);
}
!
void loop() {
digitalWrite(send, HIGH);
boolean val = digitalRead(receive);
if(val) digitalWrite(led, HIGH);
else digitalWrite(led, LOW);
}
彈跳 (Bouncing)
這是機械開關最常⾒見的問題
我們以為只有按⼀一次
但實際上會產⽣生多次按壓的現象
雖然發⽣生在幾微秒的時間內
卻⾜足以上微電腦記錄下來造成誤判
期望 00000001111111111111 實際 00000000100101111111
按下 放開
按下
放開
消除彈跳 (De-Bouncing)
硬體解法:RC電路
軟體解法:當程式在讀取輸⼊入腳位的狀態並

且偵測到變化時,在⼀一定時間的延遲後再進

⾏行⼀一次讀取⼯工作。如果程式確認狀態有變化
則代表開關/按鈕已經改變了位置。
Digital Input
int led = 13;
int send = 11;
int receive = 9;
boolean oldState = LOW;
boolean nowState = LOW;
!
void setup() {
pinMode(led, OUTPUT);
pinMode(send, OUTPUT);
pinMode(receive, INPUT);
oldState = digitalRead(receive);
}
!
void loop() {
digitalWrite(send, HIGH);
boolean r1 = digitalRead(receive);
Digital Input
if(r1 != oldState) {
delay(20);
boolean r2 =
digitalRead(receive);
if(r1 == r2) {
oldState = r1;
nowState = !nowState;
digitalWrite(led, nowState);
}
}
}
按下,放開,亮
按下,放開,暗
按下,放開,亮
Practice 1
Practice 2
按住三秒,閃爍三秒
像機器Reset情形
Digital Input
#include <IRremote.h>
int RECV_PIN = 13;
IRrecv irrecv(RECV_PIN);
decode_results results;
!
void setup() {
irrecv.enableIRIn();
pinMode(9, OUTPUT);
}
!
void loop() {
if (irrecv.decode(&results)) {
if(results.value == 16593103)
digitalWrite(9, HIGH);
else digitalWrite(9, LOW);
}
}
對準IR接收器
按下遙控器電源鍵
Digital Input
int irRece = 10;
int led = 13;
!
void setup() {
pinMode(led, OUTPUT);
pinMode(irRece, INPUT);
digitalWrite(led, LOW);
}
!
void loop() {
boolean val = digitalRead(irRece);
if (val) digitalWrite(led, HIGH);
else digitalWrite(led, LOW);
}
IR接收
IR發射
ATMEGA328P
Analog Input x 6 pins
類⽐比輸⼊入
不同於數位訊號只有兩種狀態
類⽐比訊號為⼀一串連續的數值
在Arduino代表0V~5V之間可能的數值
透過內建A/D轉換器進⾏行取樣
Potentiometer
c
電流從A流⼊入,CW與CB流出,當W為量測腳位時
測量到的電阻值為AC
+ _
int pot = 0;
int led = 13;
!
void setup() {
pinMode(led, OUTPUT);
pinMode(pot, INPUT);
Serial.begin(9600);
}
!
void loop() {
boolean val = analogRead(pot);
Serial.println(val);
digitalWrite(led, HIGH);
delay(val);
digitalWrite(led, LOW);
delay(val);
}
Analog Input
按下IDE右上⽅方 圖⽰示
將數據顯⽰示在Serial Monitor
Analog Input
int pot = 0;
int led = 13;
!
void setup() {
pinMode(led, OUTPUT);
pinMode(pot, INPUT);
Serial.begin(9600);
}
!
void loop() {
boolean val = analogRead(pot);
Serial.println(val);
digitalWrite(led, HIGH);
delay(val);
digitalWrite(led, LOW);
delay(val);
}
將可以換成蜂鳴⽚片
觀察在Monitor的數值變化
(中間接正,外圈接負)
PWM
橫軸為時間,analogWrite(64)代表在⼀一個⼯工作週期的
時間中,⾼高電位(5V)的時間佔整個週期時間的25%,
所以平均來看像是提供了1.25V的電⼒力,因此利⽤用此
概念,我們可以⽤用來控制LED的亮度或⾺馬達的轉速
t
t
t
t
t
Analog Output
int analogOutPin = 11;
!
void setup() {
pinMode(analogOutPin, OUTPUT);
}
!
void loop() {
analogWrite(analogOutPin, 0);
delay(500);
analogWrite(analogOutPin, 64);
delay(500);
analogWrite(analogOutPin, 128);
delay(500);
analogWrite(analogOutPin, 255);
delay(500);
analogWrite(analogOutPin, 128);
delay(500);
analogWrite(analogOutPin, 64);
delay(500);
}
最亮
Analog Output
#include <Servo.h>
Servo myservo;
int pot = 0;
int val;
!
void setup() {
myservo.attach(9);
}
!
void loop() {
val = analogRead(pot);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(15);
}
讀⼊入類⽐比訊號數值介於0~1023,透過map⽅方法
將數值依照⽐比例轉換為0~179數值
ATMEGA328P
serial
communication
Serial Communication
int led = 11;
!
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
digitalWrite(led, LOW);
}
!
!
void loop() {
if(Serial.available()>0) {
int val = Serial.read();
Serial.println(val);
if(val==97) {
digitalWrite(led, HIGH);
}
}
}
代表有收到資料
傳送過來的數值會轉換為
ascii碼,因此97代表⼩小寫‘a’
Serial Communication
利⽤用Aeduino上的Serial Library我們可以很容易的利
⽤用任何⼀一種⼯工具語⾔言來跟Aeduino進⾏行資料傳輸與溝
通,以下我們將透過Apache來架設網⾴頁伺服器,並
利⽤用PHP的Serial⽅方法與Aeduino進⾏行溝通
!
⾸首先安裝XAMPP這套整合軟體,其內建Apache +
MySQL + PHP,可以幫助我們輕鬆地建⽴立網⾴頁開發
環境,接著只需要三⾏行PHP程式碼我們就透過網⾴頁
跟Arduino溝通囉!
XAMPP https://www.apachefriends.org/zh_tw/index.html
=
Serial Communication
XAMPP安裝完成後,打開manager-osx管理介⾯面
中的Manage Servers並按下右⽅方Start鍵,接著在
瀏覽器網址列地⽅方輸⼊入127.0.0.1或localhost,若
有看到XAMPP歡迎畫⾯面代表你安裝成功囉!
!
!
!
!
!
!
若Apache Web Server無法啟動,請打開MAC終端
機輸⼊入sudo apachectl stop
Serial Communication
接著我們在應⽤用程式找到XAMPP資料夾,其中
htdocs中我們可以看到⼀一個名為index.php的檔
案,⽤用⽂文字編輯器打開index.php,刪除所有內容
後輸⼊入以下程式碼並存檔:
!
<?php
$fp =fopen("連接埠", "w");
fwrite($fp, 'a');
fclose($fp);
?>
!
window作業系統在連接埠填⼊入如”COM?”
mac作業系統則填⼊入如”/dev/tty.usbmodem?”
最後打開瀏覽器並在網址列中輸⼊入127.0.0.1,此
時網⾴頁會透過Serial傳送⼀一個⼩小寫的’a’字元給
Arduino,Arduino在接收到資料後便會將LED點亮

More Related Content

What's hot

LinkIt 7697 開發平台簡介 (Traditional Chinese)
LinkIt 7697 開發平台簡介 (Traditional Chinese)LinkIt 7697 開發平台簡介 (Traditional Chinese)
LinkIt 7697 開發平台簡介 (Traditional Chinese)Bear Wang
 
Arduino 習作工坊 - Lesson 3 電音之夜
Arduino 習作工坊 -  Lesson 3 電音之夜Arduino 習作工坊 -  Lesson 3 電音之夜
Arduino 習作工坊 - Lesson 3 電音之夜CAVEDU Education
 
Topc open-platform-public
Topc open-platform-publicTopc open-platform-public
Topc open-platform-publicKenson Chou
 
使用 Arduino 控制 ESP8266 的各種方式
使用 Arduino 控制 ESP8266 的各種方式使用 Arduino 控制 ESP8266 的各種方式
使用 Arduino 控制 ESP8266 的各種方式Kenson Chiang
 
Arduino 與 raspberry pi 硬體差異與應用
Arduino 與  raspberry pi 硬體差異與應用Arduino 與  raspberry pi 硬體差異與應用
Arduino 與 raspberry pi 硬體差異與應用Marcus Pek
 
MicroPython簡介
MicroPython簡介 MicroPython簡介
MicroPython簡介 Max Lai
 
瞻營全電子_六足機器人(二)
瞻營全電子_六足機器人(二)瞻營全電子_六足機器人(二)
瞻營全電子_六足機器人(二)CAVEDU Education
 
期末專題報告書
期末專題報告書期末專題報告書
期末專題報告書HsuChi Chen
 
LinkIt ONE tutorial #1- Basics
LinkIt ONE tutorial #1- BasicsLinkIt ONE tutorial #1- Basics
LinkIt ONE tutorial #1- BasicsCAVEDU Education
 
單晶片期末專題-報告二
單晶片期末專題-報告二單晶片期末專題-報告二
單晶片期末專題-報告二HsuChi Chen
 
HC 05藍芽模組連線
HC 05藍芽模組連線HC 05藍芽模組連線
HC 05藍芽模組連線Chen-Hung Hu
 
程式人雜誌 -- 2013 年 2 月號
程式人雜誌 -- 2013 年 2 月號程式人雜誌 -- 2013 年 2 月號
程式人雜誌 -- 2013 年 2 月號鍾誠 陳鍾誠
 
Arduino 習作工坊#2 - 動力之夜150114
Arduino 習作工坊#2 - 動力之夜150114Arduino 習作工坊#2 - 動力之夜150114
Arduino 習作工坊#2 - 動力之夜150114CAVEDU Education
 

What's hot (20)

LinkIt 7697 開發平台簡介 (Traditional Chinese)
LinkIt 7697 開發平台簡介 (Traditional Chinese)LinkIt 7697 開發平台簡介 (Traditional Chinese)
LinkIt 7697 開發平台簡介 (Traditional Chinese)
 
Arduino AMA中級認證術科實作 all
Arduino AMA中級認證術科實作 allArduino AMA中級認證術科實作 all
Arduino AMA中級認證術科實作 all
 
Arduino導論
Arduino導論Arduino導論
Arduino導論
 
Arduino感測應用
Arduino感測應用Arduino感測應用
Arduino感測應用
 
Arduino 習作工坊 - Lesson 3 電音之夜
Arduino 習作工坊 -  Lesson 3 電音之夜Arduino 習作工坊 -  Lesson 3 電音之夜
Arduino 習作工坊 - Lesson 3 電音之夜
 
Arduino簡介
Arduino簡介Arduino簡介
Arduino簡介
 
Topc open-platform-public
Topc open-platform-publicTopc open-platform-public
Topc open-platform-public
 
使用 Arduino 控制 ESP8266 的各種方式
使用 Arduino 控制 ESP8266 的各種方式使用 Arduino 控制 ESP8266 的各種方式
使用 Arduino 控制 ESP8266 的各種方式
 
Arduino 與 raspberry pi 硬體差異與應用
Arduino 與  raspberry pi 硬體差異與應用Arduino 與  raspberry pi 硬體差異與應用
Arduino 與 raspberry pi 硬體差異與應用
 
Arduino overview
Arduino overviewArduino overview
Arduino overview
 
MicroPython簡介
MicroPython簡介 MicroPython簡介
MicroPython簡介
 
瞻營全電子_六足機器人(二)
瞻營全電子_六足機器人(二)瞻營全電子_六足機器人(二)
瞻營全電子_六足機器人(二)
 
Python與Ardinio整合應用
Python與Ardinio整合應用Python與Ardinio整合應用
Python與Ardinio整合應用
 
期末專題報告書
期末專題報告書期末專題報告書
期末專題報告書
 
LinkIt ONE tutorial #1- Basics
LinkIt ONE tutorial #1- BasicsLinkIt ONE tutorial #1- Basics
LinkIt ONE tutorial #1- Basics
 
單晶片期末專題-報告二
單晶片期末專題-報告二單晶片期末專題-報告二
單晶片期末專題-報告二
 
HC 05藍芽模組連線
HC 05藍芽模組連線HC 05藍芽模組連線
HC 05藍芽模組連線
 
程式人雜誌 -- 2013 年 2 月號
程式人雜誌 -- 2013 年 2 月號程式人雜誌 -- 2013 年 2 月號
程式人雜誌 -- 2013 年 2 月號
 
Arduino 習作工坊#2 - 動力之夜150114
Arduino 習作工坊#2 - 動力之夜150114Arduino 習作工坊#2 - 動力之夜150114
Arduino 習作工坊#2 - 動力之夜150114
 
AMA 中級術科實作II
AMA 中級術科實作IIAMA 中級術科實作II
AMA 中級術科實作II
 

Similar to Arduino workshop in Macau

Otto97完全製作手冊 v0.9
Otto97完全製作手冊 v0.9Otto97完全製作手冊 v0.9
Otto97完全製作手冊 v0.9Bear Wang
 
Web + Arduino 實在有夠潮 ( 課程簡報 )
Web + Arduino 實在有夠潮 ( 課程簡報 ) Web + Arduino 實在有夠潮 ( 課程簡報 )
Web + Arduino 實在有夠潮 ( 課程簡報 ) Web Arduino
 
Iot 自動販賣機
Iot 自動販賣機Iot 自動販賣機
Iot 自動販賣機艾鍗科技
 
開放硬體認知學習指引
開放硬體認知學習指引開放硬體認知學習指引
開放硬體認知學習指引MAKERPRO.cc
 
物聯網技術分享 使用ESP8266
物聯網技術分享 使用ESP8266物聯網技術分享 使用ESP8266
物聯網技術分享 使用ESP8266Power Wu
 
2015 JSDC Build Anything with JavaScript
2015 JSDC Build Anything with JavaScript2015 JSDC Build Anything with JavaScript
2015 JSDC Build Anything with JavaScriptWeb Arduino
 
Arduino在農業、氣象與工業上的應用
Arduino在農業、氣象與工業上的應用Arduino在農業、氣象與工業上的應用
Arduino在農業、氣象與工業上的應用Victor Sue
 
IoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbed
IoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbedIoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbed
IoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbedBang Min Shiue
 
物聯網興起淺談Arduino.pdf
物聯網興起淺談Arduino.pdf物聯網興起淺談Arduino.pdf
物聯網興起淺談Arduino.pdf永忠 曹
 
Arduino 底層原始碼解析心得
Arduino 底層原始碼解析心得Arduino 底層原始碼解析心得
Arduino 底層原始碼解析心得roboard
 
#1247 Sensor and Controller Student book Chinese version-Part 1
#1247 Sensor and Controller Student book Chinese version-Part 1#1247 Sensor and Controller Student book Chinese version-Part 1
#1247 Sensor and Controller Student book Chinese version-Part 1Sandy Lu
 
Arduino Yun Mini簡介
Arduino Yun Mini簡介Arduino Yun Mini簡介
Arduino Yun Mini簡介Wei-Tsung Su
 

Similar to Arduino workshop in Macau (20)

Otto97完全製作手冊 v0.9
Otto97完全製作手冊 v0.9Otto97完全製作手冊 v0.9
Otto97完全製作手冊 v0.9
 
S4 a sensor board
S4 a sensor boardS4 a sensor board
S4 a sensor board
 
Web + Arduino 實在有夠潮 ( 課程簡報 )
Web + Arduino 實在有夠潮 ( 課程簡報 ) Web + Arduino 實在有夠潮 ( 課程簡報 )
Web + Arduino 實在有夠潮 ( 課程簡報 )
 
Iot 自動販賣機
Iot 自動販賣機Iot 自動販賣機
Iot 自動販賣機
 
開放硬體認知學習指引
開放硬體認知學習指引開放硬體認知學習指引
開放硬體認知學習指引
 
物聯網技術分享 使用ESP8266
物聯網技術分享 使用ESP8266物聯網技術分享 使用ESP8266
物聯網技術分享 使用ESP8266
 
Arduino overview
Arduino overviewArduino overview
Arduino overview
 
2015 JSDC Build Anything with JavaScript
2015 JSDC Build Anything with JavaScript2015 JSDC Build Anything with JavaScript
2015 JSDC Build Anything with JavaScript
 
mBot組裝與測試
mBot組裝與測試mBot組裝與測試
mBot組裝與測試
 
Arduino在農業、氣象與工業上的應用
Arduino在農業、氣象與工業上的應用Arduino在農業、氣象與工業上的應用
Arduino在農業、氣象與工業上的應用
 
IoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbed
IoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbedIoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbed
IoT 與 WoT 物聯網裝置實作:使用 Arch Pro 與 mbed
 
AMA 中級術科實作III
AMA 中級術科實作IIIAMA 中級術科實作III
AMA 中級術科實作III
 
物聯網興起淺談Arduino.pdf
物聯網興起淺談Arduino.pdf物聯網興起淺談Arduino.pdf
物聯網興起淺談Arduino.pdf
 
Arduino 底層原始碼解析心得
Arduino 底層原始碼解析心得Arduino 底層原始碼解析心得
Arduino 底層原始碼解析心得
 
設定Arduino Yún Ethernet連線
設定Arduino Yún Ethernet連線設定Arduino Yún Ethernet連線
設定Arduino Yún Ethernet連線
 
#1247 Sensor and Controller Student book Chinese version-Part 1
#1247 Sensor and Controller Student book Chinese version-Part 1#1247 Sensor and Controller Student book Chinese version-Part 1
#1247 Sensor and Controller Student book Chinese version-Part 1
 
Arduino Yun Mini簡介
Arduino Yun Mini簡介Arduino Yun Mini簡介
Arduino Yun Mini簡介
 
使用console訊息操作Arduino Yún IO
使用console訊息操作Arduino Yún IO使用console訊息操作Arduino Yún IO
使用console訊息操作Arduino Yún IO
 
LinkIt 7697 IoT tutorial
LinkIt 7697 IoT tutorialLinkIt 7697 IoT tutorial
LinkIt 7697 IoT tutorial
 
Arduino yún簡介
Arduino yún簡介Arduino yún簡介
Arduino yún簡介
 

Arduino workshop in Macau