0% found this document useful (0 votes)
40 views78 pages

Basic Raspberry Pi

Basic Raspberry Pi

Uploaded by

Eric Chan C. W.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views78 pages

Basic Raspberry Pi

Basic Raspberry Pi

Uploaded by

Eric Chan C. W.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

學 ls

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

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

• Raspberry Pi: overview 樹莓派概探

• Setting up a Raspberry Pi 設置樹莓派

• Getting ready for Raspberry Pi: one-off tasks


準備迎接樹莓派:⼀一次性任務

• Let's program it! 編個程式吧!

• 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
Raspberry Pi: overview

樹莓派概探

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.
便宜的卡⽚片式電腦,可接駁顯⽰示屏、鍵盤及滑⿏鼠使⽤用。

• Capable of doing everything you’d expect a desktop computer to do, from


browsing the Internet and playing high-definition video, to making
spreadsheets, word-processing, and playing games.
可作桌⾯面式電腦使⽤用,能⽤用以瀏覽網⾴頁、播放影⽚片、製作試算表、⽂文字報
告或玩電腦遊戲等。

• Can be connected to other electronic components to make different


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

• 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
設置樹莓派

• What you need 你需要的:

• SD card.

• Monitor or TV. 顯⽰示器或電視。

• HDMI cable. HDMI 連接線。

• USB keyboard, mouse. USB 鍵盤與滑⿏鼠。

• Power supply. 電源供應器。

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.

• Size of SD card (Micro SD or Full SD) depends on Raspberry Pi model.


不同的樹莓派⽤用不同尺⼨寸的 SD card.
Model 類型 Model A Model B Model A+ Model B+
Size 尺⼨寸 Full SD Full SD Micro SD Micro SD

• 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。

Table data source:


• [Link] 12
SD card

SD Card Capacity
Full SD miniSD microSD
SD卡容量

SD up to 上限 2GB

SDHC High Capacity


⾼高容量 4GB — 32GB

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 連接線

• If your monitor or TV has HDMI input:


如你的顯⽰示器或電視有 HDMI 輸⼊入:

• You just need a HDMI (male–male) cable.


你只需要⼀一條HDMI 公對公連接線。

• Your monitor or TV with DVI input:


如你的顯⽰示器或電視有 DVI 輸⼊入:

• Use a HDMI to DVI (specifically, DVI-D) male–male cable.


你需要⽤用⼀一條HDMI ⾄至 DVI-D 公對公連接線。

• Sound goes to Monitor/TV directly in both cases.


在兩種情況下,聲⾳音會直接從顯⽰示器或電視輸出。
14
Monitor/TV and HDMI Cable
顯⽰示器或電視,與及 HDMI 連接線

HDMI male–male cable HDMI to DVI-D male–male cable


HDMI 公對公連接線 HDMI ⾄至 DVI-D 公對公連接線
Image source:
• [Link]
• [Link] 15
Monitor/TV and HDMI Cable
顯⽰示器或電視,與及 HDMI 連接線

• Your monitor or TV only supports VGA input.


如你的顯⽰示器或電視只有 VGA 輸⼊入:
• Use HDMI (male) to VGA (female) adaptor and a normal VGA
(male–female) cable to connect to computer.
你需要⽤用 HDMI (公)⾄至 VGA (⺟母)適配器及VGA 公對⺟母連接線去連
接電腦。
• Some models support sound output, some not.
只有部分型號⽀支援聲⾳音輪出。

Image source:
• [Link]
• [Link] 16
Keyboard, mouse
鍵盤,滑⿏鼠

• Any USB keyboard and mouse would do.


任何USB鍵盤和滑⿏鼠。

• Those with builtin USB hub (e.g., Apple keyboard) are


also supported.
亦⽀支援配有 USB 集線器(如:Apple keyboard)的USB
鍵盤和滑⿏鼠。

Image source:
• [Link] 17
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 may be needed.


可能需要配備Type-A USB ⾄至 micro USB 公對公連接線
Image sources:
• [Link]
• [Link] 18
• [Link]
Getting ready for Raspberry Pi: one-off tasks

準備迎接樹莓派:⼀一次性任務

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]

• Download SD Formatter 4.0 software from


[Link]
and install it.
從這網站下載然後安裝SD Formatter 4.0格式化軟體:
[Link]

Image sources:
• [Link] 20
Use SD Formatter to format the SD card
利⽤用 SD Formatter 4.0 將 SD card 格式化

• Insert your SD card into a card reader connected to your computer.


將你的 SD card 插⼊入連接到您的電腦的讀卡器。

• Run the software. 執⾏行軟件。

• Choose 選取 "Overwrite Format".

• Click the "Option" button, choose "Yes" in the menu that pops up, then click
"OK".
點擊 "Option", 從彈出的菜單中選取 "Yes",然後點擊"OK"。

• Specify the name of the SD card, e.g., "RPi".


給SD card ⼀一個名字,如 "RPi"。

• Click "Format" and wait for completion.


點擊 "Format" ,等格式化完成。
References:
• [Link]
• [Link] 21
Install NOOBS onto the SD card
在 SD card 上安裝 NOOBS

• Expand the zip file NOOBS_v1_4_2.zip


解壓 zip 檔 NOOBS_v1_4_2.zip

• Copy the expanded files and folders to the newly


formatted SD card.
將已解壓的⽂文件和⽂文件夾複製到剛格式化的 SD card.

• Eject the card from your computer.


將 SD card 從電腦中退出。

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.
接駁顯⽰示屏、鍵盤,和滑⿏鼠⾄至樹莓派。

• Power up the Raspberry Pi by connecting its micro USB connection to a


power source (USB power supply, a power bank, or a computer).
利⽤用 micro USB 向樹莓派供應電源。電源可從 USB 電源供應器、可携式電
源、或電腦獲取。

• Choose the keyboard type to be US keyboard if necessary.


如需要,選擇鍵盤類型為美式鍵盤。

• Choose to install Raspbian. Confirm and wait.


選擇安裝 Raspbian,確定,然後等待安裝完成。

23
Ready to go!

可⽤用了!

24
Usable now!
即可使⽤用!

• The Raspberry Pi can be used after Raspbian is installed


without configuration.
裝了Raspbian後,不必配置,樹莓派已可使⽤用。

• Login name 登錄名:pi


Password 密碼:raspberry

• A program can be used to do system-wide configuration


when required.
需要時,可利⽤用⼀一個程式做系統配置。

25
raspi-config

• Run the terminal program by clicking the icon.


按此圖標啟動終端機程式。

• Type in the terminal the command line, and press Enter.


在終端機程式內輸⼊入此命令,然後按Enter.
sudo 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

• Example: force use of audio jack for audio output.


例:強制使⽤用⾳音頻插孔輸出聲⾳音。

Main reference:
• [Link]
• [Link] 27
Let's program it!

編個程式吧!

28
Programming the Raspberry Pi
利⽤用樹莓派編寫程式
• Visual programming 可視化編程: Scratch

• Easy to learn, not all features included.


容易學習,不包所有功能。

• Writing code 寫程式碼: Python

• A powerful programming language used in many platforms. Used in


programs with a few lines to large scale systems.
多種平台可⽤用的強⼤大的編程語⾔言,由幾⾏行的程式⾄至⼤大型項⺫⽬目均會使⽤用。

• Making music 玩⾳音樂: Sonic Pi

• An open-source programming environment, designed for creating new


sounds with code in a live coding environment.
⼀一個開放源碼的編程環境,能實時⽤用代碼創建新的聲⾳音。
Main references:
• [Link] 29
Programming the Raspberry Pi

• You can also use C, C++, Java, HTML, etc.


你亦可使⽤用 C, C++, Java, HTML 等。

• Start at the submenu Menu > Programming.


環境在⼦子菜單 Menu > Programming 內。

Main references:
• [Link] 30
Programming with Raspberry Pi
利⽤用樹莓派編寫程式

Python

Sonic Pi

Scratch

31
Scratch

• A visual programming tool which allows the user to


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

• Included in Raspbian. Raspbian 內已有。

• An MIT project 由⿇麻省理⼯工開發:[Link]

• Raspberry Pi page 在樹莓派的網⾴頁:


[Link]
scratch/
32
Scratch

33
Python

• A powerful programming language that's easy to read


and write.
易讀易寫的強⼤大的編程語⾔言。

• Language is used in many platforms and projects of


various sizes. 能在不同平台上建⽴立不同⼤大⼩小的程式。

• Some success stories 成功例⼦子:


[Link]

• Web site 網站:[Link]

34
Python
people = [ "Peter", "Paul", "Mary" ]
for person in people:
print("Hello Python") print(len(person), person)

print(1+2*3/4) for char in "Hello":


print(char)
for i in range(4):
print('Hello Python') for person in people:
if person[0]=='P':
for i in range(10,20,5): print("The word",person, "starts with a P")
print('Number', i) else:
print("The word",person, "does not start with a P")
n = 0
for i in range(1, 101):
n += i
print("The sum of the numbers 1 to 100 is:",n)

for i in range(3):
print(i," ",end='')

age = 15
print(age)
age += 1
print(age)

35
Sonic Pi

• A free live coding synth for everyone originally designed to


support computing and music lessons within schools.
可即時編碼的合成器,設計為⽀支援在學校的電腦和⾳音樂課。

• Developed by Sam Aaron in the University of Cambridge


Computer Lab.
由劍橋⼤大學電腦中⼼心的 Sam Aaron 及其團隊開發。

• Based on the Ruby programming language.


建基於 Ruby 編程語⾔言。

• Available in Raspberry Pi, OS X, and Windows.


在樹莓派、OS X,和 Windows 均能使⽤用。
36
Music quiz! ⾳音樂測驗!

• How to names the notes on a piano?


鋼琴上的⾳音符怎樣叫?

37
Music quiz! ⾳音樂測驗!

• How to names the notes on a piano?


鋼琴上的⾳音符怎樣叫?

♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯
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 ⾳音符號碼。

• Middle C is named C4, with a MIDI number 60.


中央 C 叫 C4,MIDI ⾳音符號碼 60.

• The MIDI number increases by one by going up a semitone.


向上⼀一個半⾳音,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 ⾳音符號碼

• Range of piano marked in yellow. ⿈黃⾊色底為鋼琴⾳音域。


Octave C♯ D♯ F♯ G♯ A♯
C D E F G A B
number D♭ E♭ G♭ A♭ B♭
-1 0 1 2 3 4 5 6 7 8 9 10 11
0 12 13 14 15 16 17 18 19 20 21 22 23
1 24 25 26 27 28 29 30 31 32 33 34 35
2 36 37 38 39 40 41 42 43 44 45 46 47
3 48 49 50 51 52 53 54 55 56 57 58 59
4 60 61 62 63 64 65 66 67 68 69 70 71
5 72 73 74 75 76 77 78 79 80 81 82 83
6 84 85 86 87 88 89 90 91 92 93 94 95
7 96 97 98 99 100 101 102 103 104 105 106 107
8 108 109 110 111 112 113 114 115 116 117 118 119
9 120 121 122 123 124 125 126 127
41
Sonic Pi
• Web site 網站:[Link]

• Learning resource page 學習資源:


[Link]

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

• 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.
通常,發光⼆二極管要⽤用電阻以限流,以免被燒壞。
46
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.


新的發光⼆二極管正極腳會⻑⾧長些。
47
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?


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

48
LED quiz! LED問答!

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


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

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

• 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.


電阻的外觀如圖。

51
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.
很多電⼦子元件如發光⼆二極管不能承受太多電流,故會串連電阻限
流,以免燒壞。

52
Colour coding ⾊色碼

• Resistors and some other components are colour coded.


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

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%

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

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

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

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

⾊色環。 White ⽩白 9 x 109

Gold ⾦金 x 10-1 ±5%

Silver 銀 x 10-2 ±10%

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Ω

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. [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

• 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:
• [Link]
• [Link] 58
Resistor quiz!
電阻問答!

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


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

59
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+

?Ω

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 通⽤用輸⼊入輸出

• The General Purpose Input/Output (GPIO) pins on a


Raspberry Pi connects to the world.
通⽤用輸⼊入輸出(GPIO)引腳可將樹莓派與外界連接。

• You can use various hardware components and modules


for input/output.
可⽤用各種零件和模塊作輸⼊入/輸出。

• e.g., buttons, sensors, LEDs, motors,...


例:按鈕、傳感器、發光⼆二極管、摩打⋯⋯

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
麵包版

• 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.
通常,麵包版兩邊有兩⾏行洞⽤用來連接電源(紅正⿊黑
負),⽽而中間的洞就每⾏行相椄,但兩邊的洞並不相連。
66
Breadboard: internal connections
麵包版:內裏連接例

• The power connections are marked on the surface of this


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

67
Connecting components
連接零件

• Electronic components are connected using wires on a


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

Jumper cables Jumper wire roll Flat cable with connectors


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

68
Scratch GPIO
• To use GPIO of Raspberry Pi, the GPIO server needs to be started. Use one of the
following ways.
⽤用樹莓派的 GPIO,須啟動 GPIO 伺服器,可⽤用以下任⼀一種⽅方法。

• Choose Edit > Start GPIO server manually.


⼈人⼿手選 Edit > Start GPIO server.

• Broadcast gpioserveron within Scratch.


在 Scratch 程式內廣播 gpioserveron 訊息。

• Configure GPIO 4 to be an output pin by broadcasting the config4out message.


要將 GPIO 4 作輸出端⼦子,廣播 config4out 訊息。

• Broadcasting the gpio4on and gpio4off message turns the GPIO on and off
respectively.
廣播訊息 gpio4on 和 gpio4off 分別將 GPIO 4 開和關。

Reference:
• [Link] 69
A Scratch program
⼀一個 Scratch 程式

• Which GPIO pin does the program use?


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

• GPIO 2.

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

• Repeatedly turn on GPIO 2


for 0.2 seconds, then turn it off
for 0.2 seconds.
不斷將 GPIO 2 開 0.2 秒關 0.2 秒。

70
Scratch Blinking light
Scratch 閃燈

• What should the circuit be if the program


is to control an LED?
如要⽤用這程式控制⼀一顆發光⼆二極管,電路應是怎樣?

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


記得點亮發光⼆二極管的電路是怎樣的嗎?
GPIO 2
V+
• It's like this. 是這樣的。
820Ω
• How to use GPIO 2 to control the LED?
如何⽤用 GPIO 2 控制發光⼆二極管?

• This works. 這就⾏行了。


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

• Which part do you recognize? 你認得哪部分呢?

Start the GPIO server


啟動 GPIO 伺服器

Configure GPIO 2 as output


將 GPIO 2 ⽤用作輸出端⼦子

Turn on GPIO 2
開 GPIO 2

等 0.2 秒

Turn on GPIO 2
開 GPIO 2

72
What does this program do? 這程式做甚麼?

Configure GPIO 4 as input,


with pull down resistor.
i.e., no input is taken as low (0).
將 GPIO 4 ⽤用作輸⼊入端⼦子,
內置下拉電阻。
即是沒有電壓輸⼊入時當作低 (0) 輸⼊入。

Loop only when GPIO 4 is 1.


廻圈只在 GPIO 4 為 1 時進⾏行。

73
And the circuit is... 電路是⋯⋯

• The power can be connected to


GPIO 4 via a push button as
GPIO 4 is configured to have been pulled down.
因為 GPIO 4 有下拉電阻,它可以經按鈕直接電源。

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

• Use time package for generating a delay. 利⽤用 time 軟件包使程式等待。


import time
import [Link] as GPIO
import time • Specify that the GPIO numbers (as opposed to the physical pin numbers) are
used to name the GPIO pins:
[Link]([Link])
告訴系統你會⽤用 GPIO 號碼(⽽而不是引腳位置號碼)命名 GPIO 的引腳:
[Link](False) [Link]([Link])
led = 2
[Link](led, [Link]) • Suppress the warning that the GPIO pin is in use:
抑制 GPIO 引腳在使⽤用的警告:
while True: [Link](False)
[Link](led, 1)
[Link](0.2) • Configure GPIO 2 to be an output pin by using:
要將 GPIO 2 作輸出端⼦子,要⽤用:
[Link](led, 0)
[Link](2, [Link])
[Link](0.2)
• Set the output of GPIO 2 to high (1) or low (0):
將 GPIO 2 輸出變成⾼高 (1) 或低 (0):
[Link](2, 1)

• while True: makes the program loop forever.


while True: 使程式無限廻圈。
References:
• [Link]
• [Link] 75
• [Link]
Python GPIO: blinking light with push button
Python GPIO: 按鈕閃燈
import [Link] as GPIO
import time
[Link]([Link])
[Link](False)
led = 2
button = 4
[Link](led, [Link])
[Link](button, [Link], pull_up_down=GPIO.PUD_DOWN)
while True:
if [Link](button):
[Link](led, 1) • Configure GPIO 4 to be an input pin:
[Link](0.2) 將 GPIO 3 ⽤用作輸⼊入端⼦子,要⽤用:
[Link](led, 0) [Link](3, [Link])
[Link](0.2)
• Configure an internal pull down resistor for the pin:
[Link](3, [Link], pull_up_down=GPIO.PUD_DOWN)

• 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 萬⽤用表

• For testing components


⽤用來測試零件

• Cutter pilers 剪鉗

• For cutting component leads short


⽤用來剪短零件腳

77
Thank you 謝謝

Questions? 問題?

You might also like