树莓派 RGB led wata
某宝淘到RGB Led几只,查看售后说明,共阳,最长一根为高电平(3v3、5v),其他为GPIO(控制高低电平)输入,瞬间点亮,心情大好(之前测试双74HC595驱动芯片共阳4位段选数码管一直失败)。代码参考coolwriter大神文章:https://blog.****.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()
上图: