0% found this document useful (0 votes)
10 views

Basic Arduino

Basic Arduino

Uploaded by

Eric Chan C. W.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Basic Arduino

Basic Arduino

Uploaded by

Eric Chan C. W.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

學 ls

⼩小 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...
內容...

• Credit-card sized computers 卡⽚片式電腦

• Arduino: overview Arduino 概探

• Setting up Arduino 設置 Arduino

• Software development environment for Arduino


為 Arduino ⽽而設的軟件開發環境

• Interfacing with the real world 與世界椄軌

2
Credit-card sized
computers

卡⽚片式電腦

3
Why credit card-sized computers?
為什麼使⽤用卡⽚片式電腦?

• Low cost 便宜

• Small ⼩小巧

• Portable 易攜

• Programmable 可編寫程式

• Can be used as a desktop computer


可作桌⾯面式電腦使⽤用

• Can be connected to other electronic components to make


different devices 可連接到其他電⼦子零件去製作出不同的裝置
4
Arduino: overview

Arduino 概探

Image source:
• https://www.arduino.cc
5
What is Arduino?
什麼是 Arduino?

• An open-source prototype platform based on easy-to-


use hardware and software.
⼀一套易⽤用且建基於開放源碼平台的硬件和軟件。

• Can be connected to other electronics components to


make different devices.
可連接到其他電⼦子零件去製作出不同的裝置。

6
Some of the Arduino models
部分的 Arduino 類型

Arduino UNO / Genuino UNO Arduino Due Arduino Yun

Arduino Zero Arduino Leonardo


Comparison ⽐比較 :
https://www.arduino.cc/en/Products/Compare 7
Sockets for connecting the Arduino to other electronics
⽤用以接駁電⼦子零件

USB connector used


to load programs on
to Arduino from
computer USB
插頭,⽤用以上載程式
到 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 可以⽤用來做什麼?

• Able to read inputs, such as light on sensor, a finger on a


button, or a Twitter message.
能夠讀取各種輸⼊入如:感應器偵測到光線、⼿手指按動按
鈕、或⼀一個Twitter訊息。

• Generate an output such as activating a motor, turning


on an LED, publishing something online.
變成輸出如:啟動摩打、點亮LED燈或在網上發佈信息
等。

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

• What you need 你需要的:

• Power supply. 電源供應器。

• A male type A USB to male micro USB cable.


⼀一條 Type-A USB ⾄至 micro USB 公對公連接線。

• A software development environment for Arduino.


⼀一個為 Arduino ⽽而設的軟件開發環境。

Main reference:
• Setting up your Raspberry Pi. Quick Start Guide. https://www.raspberrypi.org/help/quick-start-guide/ 12
Power supply
電源供應器

• Use a 5V micro USB power supply.


使⽤用5V micro USB 電源供電。

• Power bank for mobile phone.


可携式電源。

• Computer USB port.


電腦的 USB 端⼝口。

• USB power supply.


USB 電源供應器。

• A male type A USB to male micro USB cable.


⼀一條 Type-A USB ⾄至 micro USB 公對公連接線。
Image sources:
• http://hken.rs-online.com/web/p/plug-in-power-supply/8226373/
• http://hken.rs-online.com/web/p/power-banks/7757508/ 13
• https://www.facebook.com/orico.hk/photos/a.644756615659662.1073742025.147921822009813/644756678992989/
Software development environment for Arduino
為 Arduino ⽽而設的軟件開發環境

14
Programming the Arduino
Arduino 編程

• Visual programming 可視化編程: Scratch for Arduino

• Easy to learn. 容易學習。

• Writing code on the Arduino IDE.


⽤用 Arduino IDE 寫程式碼。

• Nearly identical environment for Processing


programmers.
和 Processing 的開發環境幾乎完全⼀一樣。

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 的整合開發環境

• Download from 在此下載:


https://www.arduino.cc/en/Main/Software

• Install the software 安裝軟件。

• The Arduino Integrated Development


Environment (IDE) looks like this after
successful installation.
成功安裝後 Arduino 的整合開發環境
(IDE) 看來像這樣。

References:
• https://www.arduino.cc/en/Guide/HomePage
• https://www.arduino.cc/en/Main/Software 18
Scratch for Arduino

• A visual programming tool which allows the user to create


animations and games with a drag-and-drop interface.
⽤用拖放界⾯面的可視化編程⼯工具,可寫動畫和遊戲。

• Scratch is an MIT project:


Scratch 由⿇麻省理⼯工開發:https://scratch.mit.edu/

• Scratch for Arduino (S4A) is a modified version of Scratch to


be used with Arduino.
Scratch for Arduino (S4A) 是 Scratch 的修改版,使之可⽤用於
Arduino。

• Web site 網⾴頁:http://s4a.cat/


19
Installing Scratch for Arduino
• Install Arduino IDE first. 先裝 Arduino 整合開發環境。

• Download Scratch for Arduino from here 在這裏下載:


http://s4a.cat/#downloads
Install the software. 安裝軟件。

• Download the firmware. 下載韌體。

• Save the file linked by "Download our firmware from here" at


http://s4a.cat/#downloads (direct link at http://vps34736.ovh.net/
S4A/S4AFirmware16.ino)
下載在 http://s4a.cat/#downloads 中 "Download our firmware
from here" 所連結的檔案。 (或直接取 http://vps34736.ovh.net/
S4A/S4AFirmware16.ino)

20
Installing Scratch for Arduino

• Make sure Scratch for Arduino is not open.


確定 Scratch for Arduino 不在執⾏行。

• Connect Arduino to the computer using a USB cable.


將 Arduino ⽤用 USB 線連到電腦。

• Open Arduino IDE.


執⾏行 Arduino 整合開發環境。

• Open the firmware file S4AFirmware16.ino in the IDE.


在 Arduino 整合開發環境內開韌體檔
S4AFirmware16.ino。
21
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 上載韌體。

• Close Arduino IDE. 關閉 Arduino 整合開發環境。

• Start Scratch for Arduino, it should be able to find your


Arduino board.
執⾏行 Scratch for Arduino,它應可找到你的 Arduino 板.
22
Installing Scratch for Arduino

• Microsoft Windows users may need to install the Arduino


drivers, available at http://s4a.cat/#drivers. (Direct link
http://vps34736.ovh.net/S4A/drivers.zip)
微軟視窗⽤用⼾戶可能需要安裝 Arduino 驅動程式,可在
http://s4a.cat/#drivers下載。(於此直接下載:http://
vps34736.ovh.net/S4A/drivers.zip)

• To avoid confusion, do not run Scratch for Arduino and


Arduino IDE at the same time.
為避免混亂,Scratch for Arduino 和 Arduino 勿同時開
啟。

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.
發光⼆二極管是半導體元件。

• LED is a special kind of diode, which is a component that allows


one-way flow of current only.
發光⼆二極管是⼀一種特別的⼆二極管。 在⼆二極管內,電流只能單向
流動。

• What makes LED different from a normal diode is that when


there is current flow, it lights up.
發光⼆二極管特別的是,電流通過時,它會發光。

• Usually, a resistor is needed to limit the current flow through an


LED so it would not be burnt.
通常,發光⼆二極管要⽤用電阻以限流,以免被燒壞。
27
Light Emitting Diode 發光⼆二極管
• Since an LED allows unidirectional current flow only, it is
polarized.
正因發光⼆二極管只能讓電流單向流動,它是有極性的元件。

• The direction of current flow is along the triangle in the circuit


diagram.
電流由符號中三⾓角形那邊的正極流往粗線那邊的負極。

• The side current enters is marked as positive using the +


symbol, and the other side negative using the – symbol.
正極可⽤用正號 + 表⽰示,負極則⽤用負號 –。

• The lead on the positive side of a new LED is often longer.


新的發光⼆二極管正極腳會⻑⾧長些。
28
Light Emitting Diode 發光⼆二極管

• The polarity of an LED can also be determined by looking


at the transparent or translucent LED itself.
其實,我們可以從透明或半透明的發光⼆二極管的結構,
得知其引線的極性。

• Inside an LED, one side is larger in size than the other.


The larger side connects to the negative (–) terminal.
發光⼆二極管中,⾦金屬⼤大些的那邊為負極。

• Which is the positive terminal in this LED?


這隻發光⼆二極管哪邊是正極?

29
LED quiz! LED問答!

• What are the polarities of the LEDs Cony is holding?


兔兔拿着兩個發光⼆二極管,極性是甚麼?

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,利⽤用希臘字⺟母Ω代表。

• Resistors are non-polar. That is, unlike batteries, installing a


resistor one way of another does not matter.
電阻是無極性元件。 即是說,不像電池,正反來安裝也沒所謂。

• Resistors look like those in the picture.


電阻的外觀如圖。

32
Ohm's law 歐姆定律
• The larger the value the resistor, the smaller will be the current flow
given the same voltage.
電阻值越⼤大,施加同樣電壓時電流越⼩小。

• With a resistor value of R and a voltage of V, the current


I=V/R. This is called the Ohm's law and can be remembered as V=IR.
若電壓為 V,電阻值為 R, 電流 I 就等於 V/R。 此公式為歐姆定律,可
以 V=IR 記之。

• 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 ⾊色碼

• Resistors and some other components are colour coded.


電阻和⼀一些電⼦子零件都會⽤用⾊色碼記錄數值。

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%

誤差值為 ±5% 和 ±10% 的電阻 Red 紅 2 x 102

通常有四個⾊色環。 Orange 橙 3 x 103

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

誤差值為 ±1% 的電阻通常有五個 Grey 灰 8 x 108

⾊色環。 White ⽩白 9 x 109

Gold ⾦金 x 10-1 ±5%

Silver 銀 x 10-2 ±10%

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Ω

3 0 1 0 1% => 301x100 = 301Ω

4701 1% => 470x101 = 4.7kΩ

104 5% => 10x104 = 100kΩ

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

• E-24 series, often available in resistors with 5% and 1% tolerance:


E-24 系列,誤差值 5% 和 1% 的電阻常⽤用:
10, 11, 12, 13, 15, 16, 18, 20, 22, 24, 27, 30, 33, 36, 39, 43, 47, 51, 56,
62, 68, 75, 82, 91
References:
• https://www.youtube.com/watch?v=jSPNvJ0XYCQ
• http://www.resistorguide.com/resistor-values/ 39
Resistor quiz! 電阻問答!

• What's the resistance of the resistors Cony is holding?


兔兔拿着兩⽀支電阻,阻值是多少?

40
Ohm's law: example 歐姆定律:例⼦子

• An LED operates at 1.5V and can take up at most 5mA


of current. What should the series resistor value be when
the power supply is 5V?
⼀一⼯工作電壓為1.5V 的發光⼆二極管能承受不多於 5mA 的
電流。當電源電壓為 5V,串連的限流電阻阻值應是多
少?
V+

?Ω

41
Ohm's law: example 歐姆定律:例⼦子

• An LED operates at 1.5V and can take up at most 5mA


of current. What should the series resistor value be when
the power supply is 5V?
⼀一⼯工作電壓為1.5V 的發光⼆二極管能承受不多於 5mA 的電
流。當電源電壓為 5V,串連的限流電阻阻值應是多少?
V+
• Answer 答案: (5V-1.5V)/5mA = 3.5V/0.005A = 700Ω
700Ω
• Higher resistance limits the current more, so a rule of
thumb is to use a value slightly more than 700Ω,
e.g., 820Ω.
電阻阻值⼤大,限流更多,故可⽤用經驗法則,⽤用稍⼤大於
700Ω 的阻值,例如 820Ω。
42
Connecting to the real world
連接現實世界

43
Breadboard 麵包版

• A breadboard is a board with internal connections and


holes for placing components.
麵包版上有孔洞以安插電⼦子電件,⽽而內部有⾦金屬連接各
洞。

• Usually, there are connected holes on the two sides for


power supply connections (black for ground, red for
positive voltage), and those in the middle are connected
every row, though rows on the two sides are not
connected.
通常,麵包版兩邊有兩⾏行洞⽤用來連接電源(紅正⿊黑
負),⽽而中間的洞就每⾏行相椄,但兩邊的洞並不相連。
44
Breadboard: internal connections
麵包版:內裏連接例

• The power connections are marked on the surface of this


board. 這麵包版上有電源⾏行的連接標記。

45
Connecting components 連接零件

• Electronic components are connected using wires on a


breadboard.
電⼦子零件⽤用電線在麵包版上連接。

Jumper cables Jumper wire roll Flat cable with connectors


跳線 單枝電線卷 杜邦彩排線

46
A S4A program
⼀一個 S4A 程式
• Components have to be connected to the Arduino in a particular way.
電⼦子零件要⽤用特定⽅方法連接 Arduino。

• S4A allows for:


S4A 可⽀支援:

• 6 analog inputs (analog pins) 六個模擬輸⼊入(模擬引腳),

• 2 digital inputs (digital pins 2 and 3) 兩個數碼輸⼊入(數碼引腳 2 和 3),

• 3 analog outputs (digital pins 5, 6 and 9) 三個模擬輸出(數碼引腳 5, 6 和 9),

• 3 digital outputs (pins 10, 11 and 13) and 三個數碼輸出(數碼引腳 10, 11 和 13)

• 4 special outputs to connect Parallax continuous rotation servomotors (digital pins 4,


7, 8 and 12).
還有四個特別的輸出,⽤用作連接 Parallax 連續旋轉伺服摩打(數碼引腳 4, 7, 8 和
12)
Reference:
• http://s4a.cat/ 47
A Scratch program
⼀一個 Scratch 程式

• To switch on or off a digital output on an Arduino board,


use the digital on or off block.
利⽤用 digital on 或 off 模塊控制數碼輸出的電位。

• To output an analog value,


use the analog value block.
利⽤用 analog value 模塊控制
模擬輸出的電位。

48
A S4A program
⼀一個 S4A 程式

• Which output pin does the program use?


這程式⽤用哪個輸出端⼦子?

• Digital pin 13. 數碼輸出,引腳 13.

• What does it do? 這程式做甚麼?

• Repeatedly turn on digital output 13


for 0.2 seconds, then turn it off
for 0.2 seconds.
不斷將數碼輸出引腳 13 開 0.2 秒關 0.2 秒。

49
S4A Blinking light
S4A 閃燈
• What should the circuit be if the program
is to control an LED?
如要⽤用這程式控制⼀一顆發光⼆二極管,電路應是怎樣?

• Remember how the circuit to light up an LED looks like?


記得點亮發光⼆二極管的電路是怎樣的嗎?
digital pin 13
• It's like this. 是這樣的。 V+

• How to use digital output pin 13 to control 820Ω


the LED?
如何⽤用數碼輸出引腳 13 控制發光⼆二極管?

• This works. 這就⾏行了。


50
A new program......
⼀一個新的程式⋯⋯

• Which part is different? 有哪部分不同?

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 上經按鈕接電源,亦要下拉電阻,因沒按按鈕時引腳處於⾼高阻
抗狀態,容易被噪⾳音影響。

• Pull down resistor value is typically one to a few kΩ. 下拉電阻阻值通常為⼀一⾄至幾千歐姆。


V+
digital pin 13
V+ V+

V+ digital pin 2 820Ω

1kΩ

53
Programming in Arduino IDE
Arduino 整合開發環境下編程

• Code inside setup() is run once at start of program.


setup() 內的程式碼會於程式開始時執⾏行⼀一次。

• Code inside setup() is run periodically, many times a


second.
setup() 內的程式碼會
不斷執⾏行,每秒多次。

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.
⽤用來控制引腳是輸⼊入、輸出,還是有拉上電阻的輸⼊入。

• digitalWrite(pin, value) outputs a HIGH or LOW value to a


digital pin. 輸出⾼高或低電位予數碼輸出引腳。

• digitalRead(pin) reads the value from a specified digital pin,


either HIGH or LOW. 讀數碼輸⼊入引腳的電位,是⾼高還是低。

• delay(ms) pauses the program for ms milliseconds.


暫停 ms 毫秒。
Reference:
• https://www.arduino.cc/en/Reference/PinMode
• https://www.arduino.cc/en/Reference/DigitalWrite
• https://www.arduino.cc/en/Reference/DigitalRead
• https://www.arduino.cc/en/Reference/delay 55
The two programs in Arduino IDE
Arduino 整合開發環境下的兩個程式

Reference:
• https://www.arduino.cc/en/Reference/HomePage 56
Tools ⼯工具

• Multimeter 萬⽤用表

• For testing components ⽤用來測試零件

• Cutter pilers 剪鉗

• For cutting component leads short ⽤用來剪短零件腳

57
Thank you 謝謝
Questions? 問題?

You might also like