Skip to content

Commit 05c6200

Browse files
authored
Usage examples for chaining drivers.
1 parent 6fdb5a2 commit 05c6200

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

examples/tlc5947_chain.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Simple demo of controlling a chain of several TLC5947 12-bit 24-channel PWM controllers.
2+
# Will update channel values to different PWM duty cycles.
3+
# Authors: Tony DiCola, Walter Haschka
4+
5+
import board
6+
import busio
7+
import digitalio
8+
9+
import adafruit_tlc5947
10+
11+
# Define pins connected to the TLC5947
12+
SCK = board.SCK
13+
MOSI = board.MOSI
14+
LATCH = digitalio.DigitalInOut(board.D5)
15+
16+
# Initialize SPI bus.
17+
spi = busio.SPI(clock=SCK, MOSI=MOSI)
18+
19+
# Initialize TLC5947
20+
DRIVER_COUNT = 2 # change this to the number of drivers you have chained
21+
tlc5947 = adafruit_tlc5947.TLC5947(spi, LATCH, num_drivers=DRIVER_COUNT)
22+
# You can optionally disable auto_write which allows you to control when
23+
# channel state is written to the chip. Normally auto_write is true and
24+
# will automatically write out changes as soon as they happen to a channel, but
25+
# if you need more control or atomic updates of multiple channels then disable
26+
# and manually call write as shown below.
27+
#tlc5947 = adafruit_tlc5947.TLC5947(spi, LATCH, num_drivers=DRIVER_COUNT, auto_write=False)
28+
29+
# There are two ways to set channel PWM values. The first is by getting
30+
# a PWMOut object that acts like the built-in PWMOut and can be used anywhere
31+
# it is used in your code. Change the duty_cycle property to a 16-bit value
32+
# (note this is NOT the 12-bit value supported by the chip natively) and the
33+
# PWM channel will be updated.
34+
35+
def first_last():
36+
"""Cycles the red pin of LED one up, then the other LED; now dims the LEDs
37+
both down. Repeats with green and blue pins. Then starts all over again.
38+
39+
Hook up one RGB LED to pins 0 (red), 1 (green), and 2 (blue), AND connect
40+
another RGB LED to pins 21, 22 and 23 of the last chained driver, respectively.
41+
"""
42+
redA = tlc5947.create_pwm_out(0)
43+
greenA = tlc5947.create_pwm_out(1)
44+
blueA = tlc5947.create_pwm_out(2)
45+
redZ = tlc5947.create_pwm_out(DRIVER_COUNT*24-3)
46+
greenZ = tlc5947.create_pwm_out(DRIVER_COUNT*24-2)
47+
blueZ = tlc5947.create_pwm_out(DRIVER_COUNT*24-1)
48+
49+
step = 10
50+
start_pwm = 0
51+
end_pwm = 32767 # 50% (32767, or half of the maximum 65535):
52+
53+
while True:
54+
for (pinA, pinZ) in ((redA, redZ), (greenA, greenZ), (blueA, blueZ)):
55+
# Brighten:
56+
print("LED A up")
57+
for pwm in range(start_pwm, end_pwm, step):
58+
pinA.duty_cycle = pwm
59+
# tlc5947.write() # see NOTE below
60+
61+
print("LED Z up")
62+
for pwm in range(start_pwm, end_pwm, step):
63+
pinZ.duty_cycle = pwm
64+
# tlc5947.write() # see NOTE below
65+
66+
# Dim:
67+
print("LED A and LED Z down")
68+
for pwm in range(end_pwm, start_pwm, 0 - step):
69+
pinA.duty_cycle = pwm
70+
pinZ.duty_cycle = pwm
71+
# tlc5947.write() # see NOTE below
72+
73+
# NOTE: if auto_write was disabled you need to call write on the parent to
74+
# make sure the value is written in each loop (this is not common, if disabling
75+
# auto_write you probably want to use the direct 12-bit raw access instead,
76+
# shown next).
77+
78+
#----------
79+
# The other way to read and write channels is directly with each channel 12-bit
80+
# value and an item accessor syntax. Index into the TLC5947 with the channel
81+
# number (0-max) and get or set its 12-bit value (0-4095).
82+
def test_all_channels(step):
83+
"""Loops over all available channels of all connected driver boards,
84+
brightening and dimming all LEDs one after the other. With RGB LEDs,
85+
all each component is cycled. Repeats forever.
86+
87+
:param step: the PWM increment in each cycle. Higher values makes cycling quicker.
88+
"""
89+
90+
start_pwm = 0
91+
end_pwm = 3072 # 75% of the maximum 4095
92+
93+
while True:
94+
for pin in range(DRIVER_COUNT*24):
95+
# Brighten:
96+
for pwm in range(start_pwm, end_pwm, step):
97+
tlc5947[pin] = pwm
98+
# Again be sure to call write if you disabled auto_write.
99+
#tlc5947.write()
100+
101+
# Dim:
102+
for pwm in range(end_pwm, start_pwm, 0 -step):
103+
tlc5947[pin] = pwm
104+
# Again be sure to call write if you disabled auto_write.
105+
#tlc5947.write()
106+
107+
#----------
108+
# Choose here which function to try:
109+
#first_last()
110+
test_all_channels(16)

0 commit comments

Comments
 (0)