3624浏览
查看: 3624|回复: 1

[教程] zero玩树莓派2 有趣的blink和pwm

[复制链接]


一、教学目标
1.了解blink方法和PWMLED类
2.编程实现LED呼吸灯
二、实验设备
1.树莓派 *1https://www.dfrobot.com.cn/goods-1976.html
2.树莓派扩展板*1 https://www.dfrobot.com.cn/goods-2041.html
3.LED模块*1https://www.dfrobot.com.cn/goods-72.html
(2-3两个设备取代面包板,LED, 杜邦线 , 电阻,完美解决接线的烦恼)

知识回顾
LED模块点亮程序1,通过LED的 on 和off 方法来控制led灯的亮灭
zero玩树莓派2 有趣的blink和pwm图1

程序实操:
新知识,有趣的ZERO库中也可以用blink方法来实现,而且代码少:
blink(on_time=1, off_time=1, fade_in_time=0, fade_out_time=0, n=None, background=True)

Make the device turn on and off repeatedly.

Parameters:
on_time (float) – Number of seconds on. Defaults to 1 second.

off_time (float) – Number of seconds off. Defaults to 1 second.

fade_in_time (float) – Number of seconds to spend fading in. Defaults to 0.

fade_out_time (float) – Number of seconds to spend fading out. Defaults to 0.

n (int or None) – Number of times to blink; None (the default) means forever.

background (bool) – If True (the default), start a background thread to continue blinking and return immediately. If False, only return when the blink is finished (warning: the default value of n will result in this method never returning)


示例代码:
from gpiozero import LED
from signal import pause
led =LED(17)
led.blink()
pause() #注意pause函数  不能去掉,否则程序执行一遍就停止了。

zero玩树莓派2 有趣的blink和pwm图2


新知识:PWMLED实现呼吸灯的效果
PWM信号调节LED亮度,是学习LED时的入门课程。
PWM是脉冲宽度调制信号,即脉冲的高电平的时间。调用PWMLED的类, 配合pulse命令发射信号指令,控制LED呼吸灯效果。

classgpiozero.PWMLED(pin, *, active_high=True, initial_value=0, frequency=100, pin_factory=None)

Extends PWMOutputDevice and represents a light emitting diode (LED) with variable brightness.

A typical configuration of such a device is to connect a GPIO pin to the anode (long leg) of the LED, and the cathode (short leg) to ground, with an optional resistor to prevent the LED from burning out.

Parameters:
pin (int or str) – The GPIO pin which the LED is connected to. See Pin Numberingfor valid pin numbers. If this is None a GPIODeviceError will be raised.

active_high (bool) – If True (the default), the on() method will set the GPIO to HIGH. If False, the on() method will set the GPIO to LOW (the off()method always does the opposite).

initial_value (float) – If 0 (the default), the LED will be off initially. Other values between 0 and 1 can be specified as an initial brightness for the LED. Note that None cannot be specified (unlike the parent class) as there is no way to tell PWM not to alter the state of the pin.

frequency (int) – The frequency (in Hz) of pulses emitted to drive the LED. Defaults to 100Hz.

pin_factory (Factory or None) – See API - Pins for more information (this is an advanced feature which most users can ignore)


具体代码一from gpiozero import PWMLED  #导入 PWMLED类
from signal import pause
led =PWMLED(17) #设置GPIO17端口
led.pulse() #发送脉冲
pause()

zero玩树莓派2 有趣的blink和pwm图3



具体代码二
你也可以用下面的代码来写同样的程序,其中,value值为0-1之间
from gpiozero import PWMLED
from time import sleep
led =PWMLED(17)
while True:
        led.value =0
        sleep(1)
        led.value =0.5
        sleep(1)
        led.value =1
        sleep(1)
zero玩树莓派2 有趣的blink和pwm图4



具体代码三
同样可以用for 循环实现
from gpiozero import PWMLED
from time import sleep
led =PWMLED(17)
num =0
while True:
        for i in range(0,11):
                num=i*0.1
                led.value =num
                sleep(1) #此处一秒时间较长,后续调整成了0.1秒效果更好

zero玩树莓派2 有趣的blink和pwm图5




具体代码四
from gpiozero import PWMLED
from time import sleep
led =PWMLED(17)
num =0
while True:
        for i in range(0,11):
                num=i*0.1
                led.value =num
                sleep(0.1)
zero玩树莓派2 有趣的blink和pwm图6





pATAq  版主

发表于 2020-6-30 22:10:46

代码格式化失败了,哈哈,感谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail