树莓派 RGB led wata

某宝淘到RGB Led几只,查看售后说明,共阳,最长一根为高电平(3v3、5v),其他为GPIO(控制高低电平)输入,瞬间点亮,心情大好(之前测试双74HC595驱动芯片共阳4位段选数码管一直失败)。代码参考coolwriter大神文章:https://blog.csdn.net/coolwriter/article/details/77719346,感谢!!!

#!/usr/bin/env python
# encoding: utf-8
 
import RPi.GPIO
import time
 
R,G,B=18,15,14
 
RPi.GPIO.setmode(RPi.GPIO.BCM)
 
RPi.GPIO.setup(R, RPi.GPIO.OUT)
RPi.GPIO.setup(G, RPi.GPIO.OUT)
RPi.GPIO.setup(B, RPi.GPIO.OUT)
 
pwmR = RPi.GPIO.PWM(R, 50)
pwmG = RPi.GPIO.PWM(G, 50)
pwmB = RPi.GPIO.PWM(B, 50)
 
pwmR.start(0)
pwmG.start(0)
pwmB.start(0)
 
try:
 
    t = 1
    while True:
        # 红色灯全亮,蓝灯,绿灯全暗(红色)
        pwmR.ChangeDutyCycle(100)
        pwmG.ChangeDutyCycle(0)
        pwmB.ChangeDutyCycle(0)
        time.sleep(t)
         
        # 绿色灯全亮,红灯,蓝灯全暗(绿色)
        pwmR.ChangeDutyCycle(0)
        pwmG.ChangeDutyCycle(100)
        pwmB.ChangeDutyCycle(0)
        time.sleep(t)
         
        # 蓝色灯全亮,红灯,绿灯全暗(蓝色)
        pwmR.ChangeDutyCycle(0)
        pwmG.ChangeDutyCycle(0)
        pwmB.ChangeDutyCycle(100)
        time.sleep(t)
         
        # 红灯,绿灯全亮,蓝灯全暗(黄色)
        pwmR.ChangeDutyCycle(100)
        pwmG.ChangeDutyCycle(100)
        pwmB.ChangeDutyCycle(0)
        time.sleep(t)
         
        # 红灯,蓝灯全亮,绿灯全暗(洋红色)
        pwmR.ChangeDutyCycle(100)
        pwmG.ChangeDutyCycle(0)
        pwmB.ChangeDutyCycle(100)
        time.sleep(t)
         
        # 绿灯,蓝灯全亮,红灯全暗(青色)
        pwmR.ChangeDutyCycle(0)
        pwmG.ChangeDutyCycle(100)
        pwmB.ChangeDutyCycle(100)
        time.sleep(t)
         
        # 红灯,绿灯,蓝灯全亮(白色)
        pwmR.ChangeDutyCycle(100)
        pwmG.ChangeDutyCycle(100)
        pwmB.ChangeDutyCycle(100)
        time.sleep(t)
 
except KeyboardInterrupt:
    pass
 
pwmR.stop()
pwmG.stop()
pwmB.stop()
 
RPi.GPIO.cleanup()

上图:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值