Basic Raspberry Pi
Basic Raspberry Pi
⼩小 ry & hoo
中 ma Sc
i
Pr ary
ond
c
Se
校園發明⼤大賽
INVENTION FOR
SCHOOLS CONTEST
Basic Raspberry Pi Dr. YIP Chi Lap [Beta], Faculty of Engineering, HKU
1
Topics to cover...
內容...
卡⽚片式電腦
3
Why credit card-sized computers?
為什麼使⽤用卡⽚片式電腦?
• Low cost 便宜
• Small ⼩小巧
• Portable 易攜
• Programmable 可編寫程式
樹莓派概探
Image source:
• [Link]
5
What is Raspberry Pi?
什麼是樹莓派?
• Low cost, credit card-sized computer that plugs into a computer monitor or
TV, and uses a standard keyboard and mouse.
便宜的卡⽚片式電腦,可接駁顯⽰示屏、鍵盤及滑⿏鼠使⽤用。
• Can be used to learn how to program in languages like Scratch and Python.
可⽤用以學習程式編寫如 Scratch 及 Python.
Image source:
• [Link] 6
Raspberry Pi models
樹莓派類型
Raspberry Pi A+
Raspberry Pi 2 B
Raspberry Pi B+
Comparison ⽐比較 :
[Link]
7
接駁輸⼊入裝置
接駁 USB 鍵盤及滑⿏鼠
接駁網絡
插⼊入 SD card
接駁顯⽰示屏
接駁電源 以HDMI或模擬
輸⼊入接駁顯⽰示屏
Image source:
• [Link] 8
Connect to a display, keyboard, mouse and power
接駁顯⽰示屏、鍵盤、滑⿏鼠,和電源
9
Setting up a Raspberry Pi
設置樹莓派
10
Setting up a Raspberry Pi
設置樹莓派
• SD card.
Main reference:
• Setting up your Raspberry Pi. Quick Start Guide. [Link] 11
SD card: capacity, size, and class
SD card: 容量、尺⼨寸,和速度等級
• Capacity: at least 8GB recommended.
容量:建議最少8GB.
• The card class determines the sustained write speed for the card; a
class 4 card will be able to write at 4MB/s, whereas a class 10 should
be able to attain 10 MB/s.
速度等級決定了卡的持續寫⼊入速度。Class 4 的卡每秒可寫⼊入4MB的
資料,Class 10則每秒10MB。
SD Card Capacity
Full SD miniSD microSD
SD卡容量
SD up to 上限 2GB
Extended
SDXC Capacity
超容量
32GB — 2TB
• There are products with a Micro SD card and a Micro SD-to-Full SD adaptor in a
package.
有些產品具有 Micro SD 卡及 Micro SD-to-Full SD 的適配器。
Table and image sources:
• [Link]
• [Link] 13
Monitor/TV and HDMI Cable
顯⽰示器或電視,與及 HDMI 連接線
Image source:
• [Link]
• [Link] 16
Keyboard, mouse
鍵盤,滑⿏鼠
Image source:
• [Link] 17
Power supply
電源
準備迎接樹莓派:⼀一次性任務
19
Download files
下載檔案
• Download the NOOBS (New Out Of the Box) zip file from this web site:
從這網站下載 NOOBS (New Out Of the Box) 的 zip 檔:
[Link]
Image sources:
• [Link] 20
Use SD Formatter to format the SD card
利⽤用 SD Formatter 4.0 將 SD card 格式化
• Click the "Option" button, choose "Yes" in the menu that pops up, then click
"OK".
點擊 "Option", 從彈出的菜單中選取 "Yes",然後點擊"OK"。
22
Installing Raspbian on your Raspberry Pi
安裝 Raspbian ⾄至樹莓派
• Put the SD card with NOOBS into the card slot of your Raspberry Pi.
將有 NOOBS 的 SD card 放⼊入樹莓派的咭槽內。
• Connect the video cable, keyboard and mouse to your Raspberry Pi.
接駁顯⽰示屏、鍵盤,和滑⿏鼠⾄至樹莓派。
23
Ready to go!
可⽤用了!
24
Usable now!
即可使⽤用!
25
raspi-config
• By using up, down, left and right arrow keys and the
Enter key, you can configure various aspects of the
system there.
利⽤用上下左右和Enter鍵,你可以利⽤用該程式作各樣配
置。
Main reference:
• [Link] 26
raspi-config
Main reference:
• [Link]
• [Link] 27
Let's program it!
編個程式吧!
28
Programming the Raspberry Pi
利⽤用樹莓派編寫程式
• Visual programming 可視化編程: Scratch
Main references:
• [Link] 30
Programming with Raspberry Pi
利⽤用樹莓派編寫程式
Python
Sonic Pi
Scratch
31
Scratch
33
Python
34
Python
people = [ "Peter", "Paul", "Mary" ]
for person in people:
print("Hello Python") print(len(person), person)
for i in range(3):
print(i," ",end='')
age = 15
print(age)
age += 1
print(age)
35
Sonic Pi
37
Music quiz! ⾳音樂測驗!
♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯
C D F G A C D F G A
D E G A B D E G A B
C D E F G A B C D E F G A B
38
MIDI 樂器數位介⾯面
• To process music, computers can use Musical Instrument Digital
Interface (MIDI).
電腦處理⾳音樂,可以⽤用樂器數位介⾯面 (MIDI)。
• Every note has a number after its name to designate the octave it is
in, and every note has an unique MIDI note number.
每個⾳音符的⾳音名後,都有個號碼表明它在哪個⼋八度。⽽而每個⾳音符都
有個獨特的 MIDI ⾳音符號碼。
39
MIDI note names and numbers
MIDI ⾳音名及⾳音符號碼
C ♯3 D ♯3 F ♯3 G ♯3 A ♯3 C ♯4 D ♯4 F ♯4 G ♯4 A ♯4
D3 E3 G3 A3 B3 D4 E4 G4 A4 B4
49 51 54 56 58 61 63 66 68 70
48 50 52 53 55 57 59 60 62 64 65 67 69 71
C3 D3 E3 F3 G3 A3 B3 C4 D4 E4 F4 G4 A4 B4
Middle C 中央C
40
MIDI note numbers
MIDI ⾳音符號碼
42
Interfacing with the real world
與世界接軌
You need some electronic components...
你要些電⼦子零件⋯⋯
43
44
Know the components:
Light Emitting Diodes (LEDs)
認識元件:發光⼆二極管
45
Light Emitting Diode
發光⼆二極管
• An LED is a semiconductor component.
發光⼆二極管是半導體元件。
48
LED quiz! LED問答!
49
Know the components: Resistors
認識元件:電阻
50
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,利⽤用希臘字⺟母Ω代表。
51
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.
很多電⼦子元件如發光⼆二極管不能承受太多電流,故會串連電阻限
流,以免燒壞。
52
Colour coding ⾊色碼
1234567890123456789012345678901234567890
53
Colour code table ⾊色碼表
• Four colour 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 colour bands are used for Blue 藍 6 x 106
resistors with ±1% tolerance. Violet 紫 7 x 107
54
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.
它通常會距離其他⾊色環較遠,或該環較近尾部。 誤差⾊色環旁的,
就是倍數⾊色環。 其他的,就是代表數值的⾊色環。
55
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.
要讀電阻值,先由不是誤差⾊色環的那⾯面開始,讀數值⾊色環的數值。 這會得
出⼀一個兩位或三位數。 將此數乘以倍數⾊色環所代表的值,便會得出電阻
值,單位為歐姆。
56
What are their resistance values?
它們的阻值是甚麼?
4 7 1 5% => 47x101 = 470Ω
Image source:
• Yet another picture of resistors. By Omegatron. [Link] 57
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
59
Ohm's law: example
歐姆定律:例⼦子
?Ω
60
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Ω。
61
Connecting to the real world
連接現實世界
62
GPIO 通⽤用輸⼊入輸出
63
Raspberry Pi Model A and B GPIO pinout
Raspberry Pi Model A and B GPIO 腳位圖
64
Raspberry Pi Model A+, B+ and 2 GPIO pinout
Raspberry Pi Model A+, B+ and 2 GPIO 腳位圖
• The leftmost 26 pins have pinout the same as that for Model A
and B.
最左⼆二⼗十六腳腳位與 Model A 和 B 的腳位⼀一樣。
65
Breadboard
麵包版
67
Connecting components
連接零件
68
Scratch GPIO
• To use GPIO of Raspberry Pi, the GPIO server needs to be started. Use one of the
following ways.
⽤用樹莓派的 GPIO,須啟動 GPIO 伺服器,可⽤用以下任⼀一種⽅方法。
• Broadcasting the gpio4on and gpio4off message turns the GPIO on and off
respectively.
廣播訊息 gpio4on 和 gpio4off 分別將 GPIO 4 開和關。
Reference:
• [Link] 69
A Scratch program
⼀一個 Scratch 程式
• GPIO 2.
70
Scratch Blinking light
Scratch 閃燈
Turn on GPIO 2
開 GPIO 2
等 0.2 秒
Turn on GPIO 2
開 GPIO 2
72
What does this program do? 這程式做甚麼?
73
And the circuit is... 電路是⋯⋯
V+ GPIO 2
V+
GPIO 4
820Ω
74
Python GPIO: blinking light 閃燈
• The [Link] package should be used to access GPIO pins in Python.
在 Python ⽤用 GPIO,要⽤用 [Link] 軟件包。
import [Link] as GPIO
• Python programs finish when all the statements finish, so the input
GPIO condition is checked within the loop to determine if the LED
should be made to blink.
Python 程式會在所有指令完成後終結,故需在無限廻環內測試 GPIO
輸⼊入端⼦子的狀態,以決定是否閃動發光⼆二極管。
References:
• [Link]
• [Link] 76
• [Link]
Tools ⼯工具
• Multimeter 萬⽤用表
• Cutter pilers 剪鉗
77
Thank you 謝謝
Questions? 問題?