任何常规LED都可以使用PWM(脉冲宽度调制)设置其亮度值。在GPIO Zero中,可以PWMLED
使用0和1之间的值来实现 :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#LEDPWM1.PY from gpiozero import PWMLED from time import sleep led = PWMLED(17) while True: led.value = 0 # off sleep(1) led.value = 0.5 # half brightness sleep(1) led.value = 1 # full brightness sleep(1) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[cc lang="python"] #LEDPWM2.PY from gpiozero import PWMLED from time import sleep led = PWMLED(17) while True: for num in range(0,100): led.value=num/100 sleep(0.1) for num in range(0,10): led.value=(10-num)/10 sleep(0.1) [/cc] |
1 2 3 4 5 6 7 8 9 10 |
[cc lang="python"] #LEDPWM3.PY from gpiozero import PWMLED from signal import pause led = PWMLED(17) led.pulse(fade_in_time=3, fade_out_time=1) pause() |
[/cc]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[cc lang="python"] #LEDPWM4.PY from gpiozero import PWMLED,Button from signal import pause led = PWMLED(17) button = Button(2) while True: button.wait_for_press() led.pulse() button.wait_for_release() led.off() pause() [/cc] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[cc lang="python"] #LEDPWM5.PY from gpiozero import PWMLED,Button from signal import pause led = PWMLED(17) button = Button(2) button.when_pressed = led.pulse button.when_released = led.off pause() [/cc] |
转载请注明:徐自远的乱七八糟小站 » 20170904 (第3课)树莓派PYPI- 呼吸灯