STEM Education Enriching Knowledge Series:
Workshop on Air Quality Monitoring Station:
application of laser cutter, 3D printer and
commonly used microcontrollers (New)
STEM教育知識增益系列:應用鐳射切割機及立體打印
機製作空氣質素監察站工作坊 (新辦)
Date日期 : 24/6/2020 & 8/7/2020 (Wednesday/星期三)
Time 時間 : 2:00p.m. – 5:00p.m.
Venue地點 : Maker Space, Room 109, Arts & Technology Education Centre
課程發展處科技教育組借調老師
莊慶榮老師
學與教資源
► 簡介由教育局開發的「以專題研習方式於STEM教育中教授機械人」學與教資源
► 科技科目 - 學與教資源
[Link]
-subjects/[Link]
[Link]
常用微控制器的應用和編程
Arduino 第 0 課:
認識Arduino
Part 1 Arduino是甚麼
Arduino是……
► open source
[Link]
► 硬體(Arduino I/O board)
► 軟體(Arduino IDE)
► 用感應器(sensor)能感應周遭環境
變化(如:紅外線感應器、溫濕度感
應器
► 能控制周遭裝置(如:燈、馬達)
► 電路可獨自運作
► 也可以搭配電腦運作,跟電腦溝通
Arduino家族
► Arduino UNO
► Clone
► Arduino Mega
► Arduino Nano
► Arduino Mini
► Arduino Pro Mini
► Arduino LilyPad
不同的
INPUT/SENSORS
不同的Output
Arduino的結構
數碼輸入/輸出
Digital
Input/output
模擬輸入
Analog Input
模擬輸入
數碼輸入/輸出 Analog Input
Digital
Input/output
Nano擴展板(Shield)
I/O Shield
G: GND
V: VCC
S: Signal
Get Ready ?
► 下載Arduino IDE (編寫Arduino程式的軟件)
► [Link]
► 下載mblock (支援圖像化編寫Arduino程式)
► [Link]
► 下載CH340驅動程式 (Arduino Clone使用的CH340晶片)
► [Link]
Get Ready ?
► 將Arduino Uno插入I/O Shield
► 用USB線將Arduino Uno和電腦連接
► 打開Arduino IDE軟件
► 在tools/Board選 ”Arduino Uno”
► 在tools/Port選Arduino Uno連接的com
Arduino 第一課:Blink
Part 1 基本閃燈
► 打開Code: 01_Blink
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
// 這是注釋,不影響程式運作
void setup() {
//這是注釋,不影響程式運作
pinMode(LED_BUILTIN, OUTPUT);
}
//這是注釋,不影響程式運作
void loop() {
digitalWrite(LED_BUILTIN, HIGH); //這是注釋,不影響程式運作
delay(1000); //這是注釋,不影響程式運作
digitalWrite(LED_BUILTIN, LOW); //這是注釋,不影響程式運作
delay(1000); //這是注釋,不影響程式運作
}
void setup() {
void loop() {
}
void setup() { 程式設定,只會執行一次
void loop() { loop內的程式會無限循環
}
// the setup function runs once when you press reset or power the board
void setup() {
// 設定digital pin LED_BUILTIN 作為輸出
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); //亮著LED (HIGH)
delay(1000); //等候1秒(1000毫秒)
digitalWrite(LED_BUILTIN, LOW); //熄LED (LOW)
delay(1000); //等候1秒(1000毫秒)
}
上載程式
//
void setup() {
//
pinMode(LED_BUILTIN, OUTPUT);
}
//
void loop() {
digitalWrite(LED_BUILTIN, HIGH); //
delay(1000); //改變這個數字有甚麼變化?
digitalWrite(LED_BUILTIN, LOW); //
delay(1000); //改變這個數字有甚麼變化?
}
Part 2 進階閃燈
先刪除所有注釋
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
將LED由內置改為D12輸出
void setup() {
pinMode(12, OUTPUT);
void loop() {
digitalWrite(12, HIGH);
G S
delay(1000);
digitalWrite(12, LOW);
delay(1000);
}
上載程式
再加一LED,輸出為D11
void setup() {
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
G S
delay(1000);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
delay(1000);
}
改變輸出值
void setup() {
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
delay(1000);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
delay(1000);
}
上載程式
試試看……
► 寫一個交通燈程式:紅綠 燈
► 寫一個交通燈程式:紅黃綠燈
Arduino 第二課:
Push Button and Serial Print
Part 1 Push Button 按鈕
► 打開Code: 02_Button
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 7; // 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() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}
Button 🡪 Arduino
GND 🡪 GND
OUT 🡪 D7
VCC 🡪 5V
void loop() {
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // turn LED on:
} else {
digitalWrite(ledPin, LOW); // turn LED off:
}
}
上載程式
► 將按鈕改為感應器
► 將LED改為警報器
Part 2 Serial Print 序列埠輸出
► 打開03_DigitalReadSerial
// digital pin 7 has a pushbutton attached to it. Give it a name:
int pushButton = 7; //改成PIN 7
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
[Link](9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
[Link](buttonState);
delay(1); // delay in between reads for stability
}
上載程式
// digital pin 7 has a pushbutton attached to it. Give it a name:
int pushButton = 7;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
[Link](9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
[Link](buttonState);
delay(1000); // delay in between reads for stability //改慢一點
}
上載程式
// digital pin 7 has a pushbutton attached to it. Give it a name:
int pushButton = 7;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
[Link](9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
[Link](“Hi! The button is “); //用” ”加入文字, 不用ln
[Link](buttonState);
delay(1000); // delay in between reads for stability
}
上載程式
Arduino 第三課:
Analog Input 類比輸入
使用Potentiometer可變電阻
► 打開04_Serial_Input
int sensor = A0; //設定變數sensor,由A0腳輸入 (注意不是D0)
int sensorRead = 0; //設定變數sensor的初始值為0
void setup() {
[Link](9600); //開啟序列埠
}
void loop() {
sensorRead = analogRead(sensor); //讀取A0的訊號
[Link](sensorRead); //將讀到訊號傳到序列埠
delay(200); //停滯時間
}
上載程式
► [Link]
Blink
Light dependent resistor光敏電阻