GPIO18
號口。from gpiozero import LightSensor
sensor = LightSensor(18)
while True:
sensor.wait_for_light()
print("It's dark! :)")
sensor.wait_for_dark()
print("It's light! :(")
複製代碼
wait_for_light()
和wait_for_dark()
這兩個函數,手遮擋住光線的時候,顯示"It's dark! :)"
,手拿開以後,顯示"It's light! :("
。from gpiozero import LightSensor, LED
from signal import pause
sensor = LightSensor(18)
led = LED(17)
sensor.when_dark = led.off
sensor.when_light = led.on
pause()
複製代碼
sensor
的狀態直接提供給led
,這樣光線傳感器檢測到亮光的時候led
熄滅,檢測不到光線的時候,led
點亮。好了,本次介紹就到這裏,本文收錄在個人《手把手教你玩樹莓派》系列教程,立足於普及樹莓派搭配人工智能、物聯網和機器人的玩法,想要跟我一塊兒學習的童鞋能夠加我微信/微博ID:asukafighting,也能夠直接看教程:github.com/asukafighti…python