如下全部實驗均要求樹莓派刷入完整版Raspbian
操做系統,也就是官網上Raspbian Stretch with desktop and recommended software
的版本。
TrafficLight.py
文件,代碼以下:from gpiozero import TrafficLights from time import sleep lights = TrafficLights(2, 3, 4) lights.green.on() while True: sleep(3) lights.green.off() lights.amber.on() sleep(1) lights.amber.off() lights.red.on() sleep(3) lights.amber.on() sleep(1) lights.green.on() lights.amber.off() lights.red.off()
python3 TrafficLight.py
,便可看到具備交通訊號燈邏輯的紅黃綠亮燈順序。
對於TrafficLight.py
文件,還有一種寫法以下:python
from gpiozero import TrafficLights from time import sleep from signal import pause lights = TrafficLights(2, 3, 4) def traffic_light_sequence(): while True: yield (0, 0, 1) # green sleep(3) yield (0, 1, 0) # amber sleep(1) yield (1, 0, 0) # red sleep(3) yield (1, 1, 0) # red+amber sleep(1) lights.source = traffic_light_sequence() pause()
最後,說白了雖然上面使用的是TrafficLight
對象,固然使用LED
對象也是同樣能夠完成這個任務的。git
from gpiozero import LED from time import sleep red = LED(2) amber = LED(3) green = LED(4) green.on() amber.off() red.off() while True: sleep(3) green.off() amber.on() sleep(1) amber.off() red.on() sleep(3) amber.on() sleep(1) green.on() amber.off() red.off()
LED
燈條
LEDboard.py
文件,代碼以下:from gpiozero import LEDBoard from time import sleep from signal import pause leds = LEDBoard(5, 6, 13, 19, 26) leds.on() sleep(1) leds.off() sleep(1) leds.value = (1, 0, 1, 0, 1) sleep(1) leds.blink() pause()
python3 LEDboard.py
,便可看到全亮、全滅、半亮、閃爍等行爲。
LED
燈條亮度控制LEDboardPWN.py
文件,代碼以下:from gpiozero import LEDBoard from signal import pause from time import sleep leds = LEDBoard(5, 6, 13, 19, 26, pwm=True) leds.value = (0.2, 0.4, 0.6, 0.8, 1.0) sleep(2) leds.pulse() pause()
pwm=True
模式開啓了LEDBoard
對象的脈衝控制功能,這樣能夠控制電壓的輸出,在命令行界面運行python3 LEDBoard
以後,能夠觀察到亮度已經有了變化,分別只有原來的0.2,0.4,0.6,0.8,1.0
的倍數。LED
燈條精準控制LEDBoard
對象初始化的針腳爲一個數組,能夠使用Python
支持的全部數組操做。from gpiozero import LEDBoard from time import sleep leds = LEDBoard(5, 6, 13, 19, 26) for led in leds: led.on() sleep(1) led.off()
LED
燈板
LEDBarGraph.py
文件,代碼以下:from __future__ import division # required for python 2 from gpiozero import LEDBarGraph from time import sleep graph = LEDBarGraph(5, 6, 13, 19, 26, 20) while True: graph.value = 1 # (1, 1, 1, 1, 1, 1) sleep(1) graph.value = 1/2 # (1, 1, 1, 0, 0, 0) sleep(1) graph.value = -1/2 # (0, 0, 0, 1, 1, 1) sleep(1) graph.value = 1/4 # (1, 0, 0, 0, 0, 0) sleep(1) graph.value = -1 # (1, 1, 1, 1, 1, 1) sleep(1)
python LEDBarGraph.py
命令,能夠觀察到LED燈板按照以上代碼中的結構順序進行點亮和熄滅。
LED
燈板亮度控制LEDBarGraph
一樣能夠開啓脈衝模式:from __future__ import division # required for python 2 from gpiozero import LEDBarGraph from time import sleep graph = LEDBarGraph(5, 6, 13, 19, 26, pwm=True) while True: graph.value = 1/10 # (0.5, 0, 0, 0, 0) sleep(1) graph.value = 3/10 # (1, 0.5, 0, 0, 0) sleep(1) graph.value = -3/10 # (0, 0, 0, 0.5, 1) sleep(1) graph.value = 9/10 # (1, 1, 1, 1, 0.5) sleep(1) graph.value = 95/100 # (1, 1, 1, 1, 0.75) sleep(1)
好了,本次介紹就到這裏,這個系列的全文會收錄在個人github目錄,歡迎你們star和溝通:https://github.com/asukafighting/RaspberryPiHackergithub