常见的 rgbLED 有四根引脚,一个 GND,另外三个分别是红绿蓝三色控制脚。通常由输入的电压不同来显示不同的颜色。但是树莓派的 GPIO 输出的是数字信号,这里可以用编程的方式来模拟电压的变化。 树莓派上用 PWM(脉宽调制)方法来实现,这是一种对模拟信号电平进行数字编码的方法。PWM 可以简单理解为,通过可控频率的高低电平切换来实现模拟电压变化的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 import RPi.GPIO as GPIO import time R, G, B = 20, 16, 21 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(R, GPIO.OUT) GPIO.setup(G, GPIO.OUT) GPIO.setup(B, GPIO.OUT) pwmR = GPIO.PWM(R, 70) pwmG = GPIO.PWM(G, 70) pwmB = GPIO.PWM(B, 70) pwmR.start(0) pwmG.start(0) pwmB.start(0) try: t = 0.5 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) for r in range(0, 101, 20): pwmR.ChangeDutyCycle(r) for g in range(0, 101, 20): pwmG.ChangeDutyCycle(g) for b in range(0, 101, 20): pwmB.ChangeDutyCycle(b) time.sleep(0.05) except KeyboardInterrupt: pass pwmR.stop() pwmG.stop() pwmB.stop() GPIO.cleanup()