2014.08.04~05
主辦:藝術無國界
贊助機構:澳門文化局
課程講師:張傑名(Jimmy)
ARDUINO 自造機工作坊
Arduino workshop in Macau
Arduino workshop in Macau
Arduino workshop in Macau
Arduino workshop in Macau
What is Arduino?
Arduino workshop in Macau
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
Arduino workshop in Macau
Arduino workshop in Macau
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
Arduino workshop in Macau
Arduino workshop in Macau
Arduino workshop in Macau
Where To Buy?
Handbook
Handbook
Circuit Sketch
Fritzing
123D Circuits
Electronics Toolkit
Arduino workshop in Macau
Specification
Arduino workshop in Macau
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…
Arduino workshop in Macau
http://arduino.cc/en/Main/Software
Arduino workshop in Macau
Arduino workshop in Macau
Arduino workshop in Macau
Arduino workshop in Macau
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

PDF
Arduino Basic
PDF
Arduino相關型錄
PDF
物聯網概論 - Arduino
PDF
nodeMCU IOT教學03 - NodeMCU導論
PDF
Arduino基礎IO控制
PDF
Arduino藍牙傳輸應用
PDF
Arduino 習作工坊 - Lesson 4 通訊之夜
PPT
Getting started with amarino
Arduino Basic
Arduino相關型錄
物聯網概論 - Arduino
nodeMCU IOT教學03 - NodeMCU導論
Arduino基礎IO控制
Arduino藍牙傳輸應用
Arduino 習作工坊 - Lesson 4 通訊之夜
Getting started with amarino

What's hot (20)

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

Similar to Arduino workshop in Macau (20)

PDF
Arduino 底層原始碼解析心得
PDF
Arduino L2
PDF
Arduino overview
PPTX
Week5 驅動程式
PPTX
LinkIt 7697 IoT tutorial
PDF
Arduino程式快速入門
PDF
Arduino應用系統設計 - Arduino程式快速入門
PDF
S4 a sensor board
PDF
學習歷程 期中專題-機器人動手做
PDF
AMA 中級術科實作III
PDF
開放硬體認知學習指引
PDF
藍色小鋪, 數字管時鐘, 作品進化分享 (by 黃偉峻)
PDF
[嵌入式系統] MCS-51 實驗 - 使用 IAR (1)
PDF
#1247 Sensor and Controller Student book Chinese version-Part 1
PPTX
A io t_ganalfhuang_day1_2022q1
PPTX
20160924 创客空间活动arduino教学
PPTX
LinkIt Smart 7688程式開發
PPT
Arduino 與 s4 a
PDF
電子學作業一 利用Arduino玩音樂
PPTX
Arduino導讀
Arduino 底層原始碼解析心得
Arduino L2
Arduino overview
Week5 驅動程式
LinkIt 7697 IoT tutorial
Arduino程式快速入門
Arduino應用系統設計 - Arduino程式快速入門
S4 a sensor board
學習歷程 期中專題-機器人動手做
AMA 中級術科實作III
開放硬體認知學習指引
藍色小鋪, 數字管時鐘, 作品進化分享 (by 黃偉峻)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (1)
#1247 Sensor and Controller Student book Chinese version-Part 1
A io t_ganalfhuang_day1_2022q1
20160924 创客空间活动arduino教学
LinkIt Smart 7688程式開發
Arduino 與 s4 a
電子學作業一 利用Arduino玩音樂
Arduino導讀
Ad

Arduino workshop in Macau