Simple test
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2018 Tony DiCola for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Simple demo of controlling the TLC5947 12-bit 24-channel PWM controller.
5# Will update channel values to different PWM duty cycles.
6# Author: Tony DiCola
7
8import board
9import busio
10import digitalio
11
12import adafruit_tlc5947
13
14# Define pins connected to the TLC5947
15SCK = board.SCK
16MOSI = board.MOSI
17LATCH = digitalio.DigitalInOut(board.D5)
18
19# Initialize SPI bus.
20spi = busio.SPI(clock=SCK, MOSI=MOSI)
21
22# Initialize TLC5947
23tlc5947 = adafruit_tlc5947.TLC5947(spi, LATCH)
24# You can optionally disable auto_write which allows you to control when
25# channel state is written to the chip. Normally auto_write is true and
26# will automatically write out changes as soon as they happen to a channel, but
27# if you need more control or atomic updates of multiple channels then disable
28# and manually call write as shown below.
29# tlc5947 = adafruit_tlc5947.TLC5947(spi, LATCH, auto_write=False)
30
31# There are two ways to channel channel PWM values. The first is by getting
32# a PWMOut object that acts like the built-in PWMOut and can be used anywhere
33# it is used in your code. Change the duty_cycle property to a 16-bit value
34# (note this is NOT the 12-bit value supported by the chip natively) and the
35# PWM channel will be updated.
36
37# With an RGB LED hooked up to pins 0, 1, and 2, cycle the red, green, and
38# blue pins up and down:
39
40red = tlc5947.create_pwm_out(0)
41green = tlc5947.create_pwm_out(1)
42blue = tlc5947.create_pwm_out(2)
43
44step = 10
45start_pwm = 0
46end_pwm = 32767 # 50% (32767, or half of the maximum 65535):
47
48while True:
49 for pin in (red, green, blue):
50 # Brighten:
51 print("Brightening LED")
52 for pwm in range(start_pwm, end_pwm, step):
53 pin.duty_cycle = pwm
54
55 # Dim:
56 print("Dimming LED")
57 for pwm in range(end_pwm, start_pwm, 0 - step):
58 pin.duty_cycle = pwm
59
60# Note if auto_write was disabled you need to call write on the parent to
61# make sure the value is written (this is not common, if disabling auto_write
62# you probably want to use the direct 12-bit raw access instead shown below).
63# tlc5947.write()
64
65# The other way to read and write channels is directly with each channel 12-bit
66# value and an item accessor syntax. Index into the TLC5947 with the channel
67# number (0-23) and get or set its 12-bit value (0-4095).
68# For example set channel 1 to 50% duty cycle.
69# tlc5947[1] = 2048
70# Or set channel 23 (first channel from the end) to 2/3 duty cycle.
71# tlc5947[-1] = 2730
72# Again be sure to call write if you disabled auto_write.
73# tlc5947.write()