rgbled_pca9685.py
1 import time 2 import board 3 import busio 4 import adafruit_pca9685 5 import adafruit_rgbled 6 7 # PCA9685 Initialization 8 i2c = busio.I2C(board.SCL, board.SDA) 9 pca = adafruit_pca9685.PCA9685(i2c) 10 pca.frequency = 60 11 12 # PCA9685 LED Channels 13 RED_LED = pca.channels[0] 14 GREEN_LED = pca.channels[1] 15 BLUE_LED = pca.channels[2] 16 17 # Create the RGB LED object 18 led = adafruit_rgbled.RGBLED(RED_LED, GREEN_LED, BLUE_LED, invert_pwm=True) 19 20 # Optionally, you can also create the RGB LED object with inverted PWM 21 # led = adafruit_rgbled.RGBLED(RED_LED, GREEN_LED, BLUE_LED, invert_pwm=True) 22 23 24 def wheel(pos): 25 # Input a value 0 to 255 to get a color value. 26 # The colours are a transition r - g - b - back to r. 27 if pos < 0 or pos > 255: 28 return 0, 0, 0 29 if pos < 85: 30 return int(255 - pos * 3), int(pos * 3), 0 31 if pos < 170: 32 pos -= 85 33 return 0, int(255 - pos * 3), int(pos * 3) 34 pos -= 170 35 return int(pos * 3), 0, int(255 - (pos * 3)) 36 37 38 def rainbow_cycle(wait): 39 for i in range(255): 40 i = (i + 1) % 256 41 led.color = wheel(i) 42 time.sleep(wait) 43 44 45 while True: 46 # setting RGB LED color to RGB Tuples (R, G, B) 47 print("setting color 1") 48 led.color = (255, 0, 0) 49 time.sleep(1) 50 51 print("setting color 2") 52 led.color = (0, 255, 0) 53 time.sleep(1) 54 55 print("setting color 3") 56 led.color = (0, 0, 255) 57 time.sleep(1) 58 59 # setting RGB LED color to 24-bit integer values 60 led.color = 0xFF0000 61 time.sleep(1) 62 63 led.color = 0x00FF00 64 time.sleep(1) 65 66 led.color = 0x0000FF 67 time.sleep(1) 68 69 # rainbow cycle the RGB LED 70 rainbow_cycle(0.01)