树莓派+RGB彩灯实现跑马灯
RGB彩灯跟普通的LED不同,它可以根据RGB的色值自定义颜色,从而轻松的实现跑马灯的效果,我们选用HW-478 3色全彩LED smd模块来实现。
一、硬件接线
RGB彩灯硬件接线比较简单,负极接GND,R/G/B分别接树莓派定义的针脚即可。
二、软件实现:
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) t = 1 #no light on pwmR.ChangeDutyCycle(0) pwmG.ChangeDutyCycle(0) pwmB.ChangeDutyCycle(0) time.sleep(t) try: 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) pwmR.ChangeDutyCycle(0) pwmG.ChangeDutyCycle(0) pwmB.ChangeDutyCycle(0) time.sleep(t) except keyboardInterrupt: pass pwmR.stop() pwmG.stop() pwmB.stop() RPi.GPIO.cleanup()
参考资料:
https://shumeipai.nxez.com/2014/11/13/rpi-gpio-module-pwm-basic-function.html