当前位置:网站首页>树莓派入门(4)LED闪烁&呼吸灯
树莓派入门(4)LED闪烁&呼吸灯
2022-08-10 05:25:00 【jualay】
一、PWM
PWM就是脉宽调制技术
1.频率
PWM的频率(f):1s内信号从低电平到高电平的总次数
2.周期
PWM周期(T):从一个高电平到下一个高电平的时间
f = 1 T {f=\frac{1}{T}} f=T1
3.脉宽
PWM脉宽(W):一个周期内的高电平时间
4.占空比
占空比(D):一个周期内脉宽占周期长度的百分比
D = W T {D=\frac{W}{T}} D=TW
二、使用PWM
1.创建一个PWM实例
PWM=GPIO.PWM(channel,freq)
2.启动PWM实例
PWM.start(dc)
dc:占空比,范围是:0.0~100.0
3.修改频率
PWM.ChangeFrequency(frequency)
4.停止PWM实例
PWM.stop()
三、LED闪烁
import RPi.GPIO as GPIO
import time #导入时间模块
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("import RPi.GPIO error")
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
while True:
GPIO.output(12,GPIO.HIGH)
time.sleep(0.5)
GPIO.output(12,GPIO.LOW)
time.sleep(0.5)
四、呼吸灯
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
led1=12
led2=18
freq=50
dc=0
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led1,GPIO.OUT)
GPIO.setup(led2,GPIO.OUT)
PWM1=GPIO.PWM(led1,freq)
PWM2=GPIO.PWM(led2,freq)
PWM1.start(dc)
PWM2.start(dc)
freq = int(input("Please input the frequency:"))
PWM1.ChangeFrequency(freq)
PWM2.ChangeFrequency(freq)
while True:
if(dc==0):
while 1:
dc=dc+1
sleep(0.01)
PWM1.ChangeDutyCycle(dc)
PWM2.ChangeDutyCycle(dc)
if (dc==100):
break
if(dc==100):
while 1:
dc=dc-1
sleep(0.01)
PWM1.ChangeDutyCycle(dc)
PWM2.ChangeDutyCycle(dc)
if (dc==0):
break
input()
PWM.stop()
GPIO.cleanup()
边栏推荐
- Get started with the OAuth protocol easily with a case
- 我用这一招让团队的开发效率提升了 100%!
- Abstract problem methodology
- Unity implements UI edge detection and drag-and-drop functions
- 线性模型中的高级特征选择技术——基于R
- 如何取得某月的最后一天
- ThreadPoolExecutor thread pool principle
- 基于BP神经网络的多因素房屋价格预测matlab仿真
- MySQL simple tutorial
- Interface debugging also can play this?
猜你喜欢

深度学习之-01

看了几十篇轻量化目标检测论文扫盲做的摘抄笔记

JavsSE => 多态

An article to master the entire JVM, JVM ultra-detailed analysis!!!

How to use Apifox's Smart Mock function?

Flutter开发:报错The following assertion was thrown resolving an image codec:Unable to…的解决方法

canvas 画布绘制时钟

leetcode每天5题-Day12

Practical skills 19: Several postures of List to Map List

暑期学前作业
随机推荐
EasyGBS connects to mysql database and prompts "can't connect to mysql server", how to solve it?
How does flinksql write that the value of redis has only the last field?
Abstract problem methodology
Get started with the OAuth protocol easily with a case
Linear Algebra (4)
Concurrency tool class - introduction and use of CountDownLatch, CyclicBarrier, Semaphore, Exchanger
栈与队列 | 有效的括号、删除字符串中的所有相邻元素、逆波兰表达式求值、滑动窗口的最大值、前K个高频元素 | leecode刷题笔记
canvas canvas drawing clock
【Pei Shu Theorem】CF1055C Lucky Days
Shield Alt hotkey in vscode
Joomla vulnerability reproduced
How to get the last day of a month
Rpc接口压测
SQL database field to append to main table
顺序表的删除,插入和查找操作
二进制中负数为何要用补码形式来表示——二进制加减法
应用在智能触摸遥控器中的触摸芯片
`id` bigint(20) unsigned NOT NULL COMMENT 'Database primary key',
2022 security officer C certificate test and simulation test in shandong province
curl命令介绍