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