Skip to content

Commit 5b0102f

Browse files
author
brentru
committed
add example for using this library with the pca9685, useful for Pi, etc!
1 parent 9174321 commit 5b0102f

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

examples/rgbled_pca9685.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
def wheel(pos):
24+
# Input a value 0 to 255 to get a color value.
25+
# The colours are a transition r - g - b - back to r.
26+
if pos < 0 or pos > 255:
27+
return 0, 0, 0
28+
if pos < 85:
29+
return int(255 - pos * 3), int(pos * 3), 0
30+
if pos < 170:
31+
pos -= 85
32+
return 0, int(255 - pos * 3), int(pos * 3)
33+
pos -= 170
34+
return int(pos * 3), 0, int(255 - (pos * 3))
35+
36+
def rainbow_cycle(wait):
37+
for i in range(255):
38+
i = (i + 1) % 256
39+
led.color = wheel(i)
40+
time.sleep(wait)
41+
42+
while True:
43+
# setting RGB LED color to RGB Tuples (R, G, B)
44+
print('setting color 1')
45+
led.color = (255, 0, 0)
46+
time.sleep(1)
47+
48+
print('setting color 2')
49+
led.color = (0, 255, 0)
50+
time.sleep(1)
51+
52+
print('setting color 3')
53+
led.color = (0, 0, 255)
54+
time.sleep(1)
55+
56+
# setting RGB LED color to 24-bit integer values
57+
led.color = 0xFF0000
58+
time.sleep(1)
59+
60+
led.color = 0x00FF00
61+
time.sleep(1)
62+
63+
led.color = 0x0000FF
64+
time.sleep(1)
65+
66+
# rainbow cycle the RGB LED
67+
rainbow_cycle(0.01)

0 commit comments

Comments
 (0)