Basic Arduino
Basic Arduino
⼩小 ry & hoo
中 ma Sc
ri y
P ar
nd
co
Se
校園發明⼤大賽
INVENTION FOR
SCHOOLS CONTEST
Basic Arduino Dr. YIP Chi Lap [Beta], Faculty of Engineering, HKU
Arduino ⼊入⾨門
⾹香港⼤大學⼯工程學院 葉志⽴立博⼠士
1
Topics to cover...
內容...
2
Credit-card sized
computers
卡⽚片式電腦
3
Why credit card-sized computers?
為什麼使⽤用卡⽚片式電腦?
• Low cost 便宜
• Small ⼩小巧
• Portable 易攜
• Programmable 可編寫程式
Arduino 概探
Image source:
• https://www.arduino.cc
5
What is Arduino?
什麼是 Arduino?
6
Some of the Arduino models
部分的 Arduino 類型
Power connector
接駁電源
Sockets for connecting the Arduino to other electronics
⽤用以接駁電⼦子零件
Image source:
• https://www.arduino.cc 8
What can Arduino be used for?
Arduino 可以⽤用來做什麼?
9
What can Arduino be used for?
Arduino 可以⽤用來做什麼?
光敏電阻
麵包板
Arduino 内有⼀一微控制器,是⼀一可讓你編寫程式的⼩小電腦,你可以接駁感應器去量度不同的環境
狀況(如室內環境的光暗度),再由Arduino 變成輸出(如房間太暗則點亮LED燈)
Image source:
• http://playground.arduino.cc/Main/ArduinoComic 10
Setting up Arduino
設置 Arduino
11
Setting up Arduino
設置 Arduino
Main reference:
• Setting up your Raspberry Pi. Quick Start Guide. https://www.raspberrypi.org/help/quick-start-guide/ 12
Power supply
電源供應器
14
Programming the Arduino
Arduino 編程
Main references:
• https://www.raspberrypi.org/documentation/usage/ 15
Programming the Arduino: Scratch for Arduino
編寫程式以控制 Arduino: Scratch for Arduino
Image source:
• http://s4a.cat 16
Programming the Arduino: Arduino IDE
編寫程式以控制 Arduino: Arduino IDE
Image source:
• https://www.arduino.cc 17
Installing the Arduino IDE
安裝 Arduino 的整合開發環境
References:
• https://www.arduino.cc/en/Guide/HomePage
• https://www.arduino.cc/en/Main/Software 18
Scratch for Arduino
20
Installing Scratch for Arduino
• In the Tools menu, select the board version and the serial
port where the board is connected.
利⽤用 Tools 菜單選擇 Arduino 板種和連接的串流埠。
• Load the firmware into your board through File > Upload
⽤用 File > Upload 上載韌體。
23
Interfacing with the real world
與世界接軌
You need some electronic components...
你要些電⼦子零件⋯⋯
24
25
Know the components:
Light Emitting Diodes (LEDs)
認識元件:發光⼆二極管
26
Light Emitting Diode 發光⼆二極管
• An LED is a semiconductor component.
發光⼆二極管是半導體元件。
29
LED quiz! LED問答!
30
Know the components: Resistors
認識元件:電阻
31
Resistors 電阻
• In a circuit diagram, the zig-zag symbol correspond to resistors.
電路圖上鋸⿒齒形的符號代表電阻。
• Resistors limit current flow and its values are measures in ohms,
with the greek letter Ω (Omega) as symbol.
電阻減低電流,單位為「歐姆」Ohm,利⽤用希臘字⺟母Ω代表。
32
Ohm's law 歐姆定律
• The larger the value the resistor, the smaller will be the current flow
given the same voltage.
電阻值越⼤大,施加同樣電壓時電流越⼩小。
• Many components like light emitting diodes (LEDs) cannot bear too
much current, and resistors are used in series with them to limit the
current so as not to damage them.
很多電⼦子元件如發光⼆二極管不能承受太多電流,故會串連電阻限
流,以免燒壞。
33
Colour coding ⾊色碼
1234567890123456789012345678901234567890
34
Colour code table ⾊色碼表
• Four color bands are used for Colour Value Multiplier Tolerance
resistors with ±5% or ±10% 顏⾊色 數值 倍數 誤差
Black ⿊黑 0 x 100
tolerance. Brown 啡 1 x 101 ±1%
Yellow ⿈黃 4 x 104
Green 綠 5 x 105
• Five color bands are used for Blue 藍 6 x 106
resistors with ±1% tolerance. Violet 紫 7 x 107
35
To read resistor values 讀電阻數值
• First, identify the tolerance band. If there is a gold or silver band at
the end of the resistor, it must be the tolerance band. Otherwise,
probably your resistor has five colour bands, and a brown band at
one end, which could be a bit separated from other bands or very
much near the end of the resistor, is the tolerance band.
⾸首先要確定哪個是誤差⾊色環。 如其⼀一尾端之⾊色環是⾦金⾊色或銀⾊色,
那肯定是誤差⾊色環。 如沒有,那電阻很可能是有五個⾊色環,⽽而其
中⼀一尾環會是啡⾊色的誤差⾊色環。
• The colour band besides the tolerance band is the multiplier band.
Other colour bands are value bands.
它通常會距離其他⾊色環較遠,或該環較近尾部。 誤差⾊色環旁的,
就是倍數⾊色環。 其他的,就是代表數值的⾊色環。
36
Reading resistor colour code 讀電阻⾊色碼
• From the opposite side of the tolerance band, start reading the value bands.
This would form a two- or three-digit number. The value of the resistor is the
number, multiplied by the multiplier indicated by the multiplier band, in ohms.
要讀電阻值,先由不是誤差⾊色環的那⾯面開始,讀數值⾊色環的數值。 這會得
出⼀一個兩位或三位數。 將此數乘以倍數⾊色環所代表的值,便會得出電阻
值,單位為歐姆。
37
What are their resistance values?
它們的阻值是甚麼?
4 7 1 5% => 47x101 = 470Ω
Image source:
• Yet another picture of resistors. By Omegatron. https://commons.wikimedia.org/wiki/File:Resistors.jpg 38
Preferred values 標準阻值
• Resistor values may have errors, and tolerance values tell their range.
電阻值會有誤差,誤差值告訴⽤用者阻值範圍。
• Preferred values, derived from tolerance values, are used to make mass
production of resistors easier.
為使更易⼤大量⽣生產電阻,有利⽤用誤差值計出來的標準阻值。
• E-12 series, most commonly used, designed for resistors with 10%
tolerance:
最常⽤用的 E-12 系列,設計給誤差值 10% 的電阻:
10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82
40
Ohm's law: example 歐姆定律:例⼦子
?Ω
41
Ohm's law: example 歐姆定律:例⼦子
43
Breadboard 麵包版
45
Connecting components 連接零件
46
A S4A program
⼀一個 S4A 程式
• Components have to be connected to the Arduino in a particular way.
電⼦子零件要⽤用特定⽅方法連接 Arduino。
• 3 digital outputs (pins 10, 11 and 13) and 三個數碼輸出(數碼引腳 10, 11 和 13)
48
A S4A program
⼀一個 S4A 程式
49
S4A Blinking light
S4A 閃燈
• What should the circuit be if the program
is to control an LED?
如要⽤用這程式控制⼀一顆發光⼆二極管,電路應是怎樣?
51
What does this program do? 這程式做甚麼?
• The loop runs, and thus the LED blinks, only when digital
input 2 is high.
廻環只在數碼輸⼊入 2 是⾼高電位時才有效,發光⼆二極管才
會閃。
52
And the circuit is... 電路是⋯⋯
• We need to connect digital pin 2 to power through a push button. A pull down resistor is
also needed because when the button is not pressed, the input voltage is in high
impedance state and is prone to noise.
我們要給數碼輸⼊入引腳 2 上經按鈕接電源,亦要下拉電阻,因沒按按鈕時引腳處於⾼高阻
抗狀態,容易被噪⾳音影響。
1kΩ
53
Programming in Arduino IDE
Arduino 整合開發環境下編程
Reference:
• https://www.arduino.cc/en/Reference/HomePage 54
A few useful functions
幾個有⽤用的函數
• pinMode(pin, mode) sets the mode of a pin.
mode can be INPUT, OUTPUT, or INPUT_PULLUP.
⽤用來控制引腳是輸⼊入、輸出,還是有拉上電阻的輸⼊入。
Reference:
• https://www.arduino.cc/en/Reference/HomePage 56
Tools ⼯工具
• Multimeter 萬⽤用表
• Cutter pilers 剪鉗
57
Thank you 謝謝
Questions? 問題?