Professional Documents
Culture Documents
Fundamentals of Raspberry Pi 06
Fundamentals of Raspberry Pi 06
R a s p b e r r y Pi
TTU We e k # 6
1
R aspberry P i
TTU 課程大綱
第㇐週:認識樹莓派 第四週: Python 程式簡
樹莓派是什麼? 介 程式的編輯與執行
樹莓派能做甚麼 ? Python 基本指令
基本的 Linux 指令
第五週: GPIO 應用
第二週:樹莓派作業系統 GPIO 簡介
與 電路與元件
常用指令 實作 – LED 控制
QEMU 模 擬 器
Raspberry OS
常用 Linux 指令
第六週:樹莓派擴充板
常用的樹莓派擴充板
Sense HAT 擴充板
第三週:遠端控制樹莓派
實作
2
R aspberry P i
可以學習編碼、製造機器人、監控國際太空站的位
置等等。
Raspberry Pi 因為有 GPIO 和藍牙、 WiFi 連
3
R aspberry P i
4
R aspberry P i
5
R aspberry P i
陀螺儀
加速度計
地磁感測器
除了可以收集感測器的各項數值外,還可以用程式
控制 LED 做出不同的樣式,但有個缺點是由於
Sense
HAT 是直接放在 Raspberry pi 上面,因此溫
度的感測數值會受到 Raspberry pi 本身的影響,
相當不準確。但如果是作為㇐般練習玩耍使用,不
失為㇐個多功能的好選擇。 6
R aspberry P i
它鼓勵歐洲的小朋友利用它來設計實驗,並幫助太
空人進行各樣各樣的測試。作為擴充版它的入門門
檻相當低,它同時支援 Python 與 Scratch 2 的
API 。
隨後官方亦推出規格完全㇐樣的市場銷售版本,在
9
R aspberry P i
在網上搜尋你亦不難找到各種 Raspberry Pi
Sense
HAT 的應用例子。
10
R aspberry P i
(Temperature)
相對濕度感測器
(Humidity)
大氣壓力感測器
(Barometric Pressure)
加速計
(Accelerometer)
電子羅盤磁力儀
(Magnetometer)
陀螺儀 (Gyroscope)
五鍵可進行上、下、
左、右、及按下的搖桿
(Joystick) 11
R aspberry P i
顯示 Hello World
from sense_hat import SenseHat
sense = SenseHat()
red=(255,0,0)
sense.show_message("Hello World")
12
R aspberry P i
調整顯示速度
from sense_hat import SenseHat
sense = SenseHat()
# 跑馬燈預設速度為 0.1s ,參數是以秒為單位
sense.show_message("Hello World", 0.3)
13
R aspberry P i
調整顯示顏色
from sense_emu import SenseHat
sense = SenseHat()
# 跑馬燈的顏色,預設為白色,可藉由參數來調整色彩
# 顏色參數為 (R, G, B) 的形式,數值介於 0~255 之間
r = (255, 0, 0)
sense.show_message("HELLO WORLD", 0.2, r )
14
R aspberry P i
調整顯示背景
from sense_hat import SenseHat
sense = SenseHat()
# 跑馬燈的背景顏色,預設為黑色,可藉由參數來調整色
彩
# 顏色參數為 (R, G, B) 的形式,數值介於 0~255 之間
r=(255,0,0)
g=(0,255,0)
b=(0,0,255)
sense.show_message("HELLO WORLD", 0.2, r, b)
15
R aspberry P i
案例 1 :循環跑馬燈
from sense_hat import SenseHat
sense = SenseHat()
r=(255,0,0)
g=(0,255,0)
b=(0,0,255)
y=(255,255,0)
w=(255,255,255)
# 使用 while 迴圈來反覆執行
while(True):
sense.show_message("Hello World", 0.1, r, w )
16
R aspberry P i
案例 2 :彩燈跑馬燈
from sense_hat import SenseHat
import random
sense = SenseHat()
while(True):
cr = random.randrange(0, 255)
cg= random.randrange(0, 255)
cb= random.randrange(0, 255)
print(cr, cg, cb)
sense.show_message("HELLO WORLD", 0.1, (cr, cg,
cb))
17
R aspberry P i
圖示製作
from sense_hat import SenseHat
sense = SenseHat()
# 先清除資料
sense.clear()
#8x8 矩陣 RGB 顯示器可使用 XY 座標來設定 LED 顯示
sense.set_pixel( 0, 0, (255, 0, 0))
sense.set_pixel(0, 7, (0, 255, 0))
sense.set_pixel(7, 0, (0, 0, 255))
sense.set_pixel(7, 7, (255, 255, 0))
18
R aspberry P i
案例 3 :使用清單製圖
from sense_hat import SenseHat
sense = SenseHat()
r = (255, 0, 0)
g = (0, 255, 0)
b = (0, 0, 255)
icon = [r, r, r, r, r, r, r, r, g, g, g, g, g, g, g, g, b, b, b, b,
b, b, b, b, r, r, r, r, r, r, r, r, g, g, g, g, g, g, g, g, b, b, b,
b, b, b, b, b,
r, r, r, r, r, r, r, r, g, g, g, g, g, g, g, g]
sense.clear() sense.set_pixels(icon)
19
R aspberry P i
讀取感測數值–溫度
from sense_hat import SenseHat
# 增加引入時間功能
import time
sense = SenseHat()
while(True):
# 讀取溫度
t = sense.get_temperature()
print(" 溫度 = ", t)
time.sleep(1)
20
R aspberry P i
讀取感測數值–氣壓
from sense_hat import SenseHat
# 增加引入時間功能
import time
sense = SenseHat()
while(True):
# 讀取大氣壓力
p = sense.get_pressure()
print(" 大氣壓力 = ", p)
time.sleep(1)
21
R aspberry P i
讀取感測數值–濕度
from sense_hat import SenseHat
# 增加引入時間功能
import time
sense = SenseHat()
while(True):
# 讀取濕度
h = sense.get_humidity()
print(" 濕度 = ", h)
time.sleep(1)
22
R aspberry P i
讀取感測數值–溫度
from sense_hat import SenseHat
# 增加引入時間功能
import time
sense = SenseHat()
while(True):
# 讀取溫度
t = sense.get_temperature()
print(" 溫度 = ", t)
t_str = str(round(t,1))
sense.show_message(t_str)
time.sleep(1) 23
R aspberry P i
作業:
溫度感測
溫度偵測,當溫度低於 19 度時,顯示 Cold
圖示。溫度偵測,當溫度 20~26 度時,顯示 Fine
圖示。溫度偵測,當溫度高於 27 度時,顯示 HoT
圖示。
24
R aspberry P i