樹莓派+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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM