Simple test¶
Print out the current state¶
Ensure your device works by reading the state of the internal registers.
examples/tca9555_simpletest.py¶
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2021 James Carr
3#
4# SPDX-License-Identifier: Unlicense
5
6
7# An example to read the configuration, polarity inversion and state of all
8# 16 inputs from the TCA9555.
9
10
11import board
12import busio
13
14from community_tca9555 import TCA9555
15
16
17# Get or create an I2C object
18if hasattr(board, "I2C"):
19 i2c = board.I2C()
20elif hasattr(board, "SCL") and hasattr(board, "SDA"):
21 i2c = busio.I2C(scl=board.SCL, sda=board.SDA)
22else:
23 # These pins are for Raspberry Pi Pico
24 # If using another board, these may need to be changed.
25 i2c = busio.I2C(scl=board.GP5, sda=board.GP4)
26
27# Create the TCA9555 expander
28expander = TCA9555(i2c)
29
30
31# Read the configuration of all 16 pins
32# 0 = output, 1 = input
33in_or_out = expander.configuration_ports
34print("Configuration\n{:016b}".format(in_or_out))
35
36# Read the polarity inversion state of all 16 pins
37polarity_inversion = expander.polarity_inversions
38print("Polarity inversion\n{:016b}".format(polarity_inversion))
39
40# Read the input state of all 16 pins
41input_state = expander.input_ports
42print("Input state\n{:016b}".format(input_state))
43
44# Read the output state of all 16 pins
45# At power up, this defaults to 1111111111111111
46output_state = expander.output_ports
47print("Output state\n{:016b}".format(output_state))
Read the inputs¶
Monitor the inputs with the Debouncer library and print out when they rise or fall.
examples/tca9555_debounce_inputs.py¶
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2021 James Carr
3#
4# SPDX-License-Identifier: Unlicense
5
6
7# A simple test to read the 16 inputs from the TCA9555.
8# It uses the Debouncer library to print out when an input has changed.
9
10
11import time
12import sys
13import board
14import busio
15
16from community_tca9555 import TCA9555
17
18try:
19 from adafruit_debouncer import Debouncer
20except ImportError:
21 # Make sure the Debounce library is available
22 # It is a requirement of this example but not the library.
23 print("Please install the Debounce library:")
24 print(" To install to your Python environment")
25 print(" pip3 install adafruit-circuitpython-debouncer")
26 print(" To install direct to a connected CircuitPython device")
27 print(" circup install adafruit-circuitpython-debouncer")
28 sys.exit()
29
30
31# Get or create an I2C object
32if hasattr(board, "I2C"):
33 i2c = board.I2C()
34elif hasattr(board, "SCL") and hasattr(board, "SDA"):
35 i2c = busio.I2C(scl=board.SCL, sda=board.SDA)
36else:
37 # These pins are for Raspberry Pi Pico
38 # If using another board, these may need to be changed.
39 i2c = busio.I2C(scl=board.GP5, sda=board.GP4)
40
41# Create the TCA9555 expander
42expander = TCA9555(i2c)
43
44
45# Prepare to read the 16 inputs
46# Create a tuple of buttons which are debounced so they can be monitored for changes.
47# This reads the bits from the expander individually, instead of as bytes or a word.
48# This is to make the debouncing easier.
49buttons = (
50 Debouncer(lambda: expander.input_port_0_pin_0),
51 Debouncer(lambda: expander.input_port_0_pin_1),
52 Debouncer(lambda: expander.input_port_0_pin_2),
53 Debouncer(lambda: expander.input_port_0_pin_3),
54 Debouncer(lambda: expander.input_port_0_pin_4),
55 Debouncer(lambda: expander.input_port_0_pin_5),
56 Debouncer(lambda: expander.input_port_0_pin_6),
57 Debouncer(lambda: expander.input_port_0_pin_7),
58 Debouncer(lambda: expander.input_port_1_pin_0),
59 Debouncer(lambda: expander.input_port_1_pin_1),
60 Debouncer(lambda: expander.input_port_1_pin_2),
61 Debouncer(lambda: expander.input_port_1_pin_3),
62 Debouncer(lambda: expander.input_port_1_pin_4),
63 Debouncer(lambda: expander.input_port_1_pin_5),
64 Debouncer(lambda: expander.input_port_1_pin_6),
65 Debouncer(lambda: expander.input_port_1_pin_7),
66)
67
68
69# Loop forever
70while True:
71 time.sleep(0.001)
72 for index, button in enumerate(buttons):
73 button.update() # Update the debounce information
74 if button.rose:
75 print("Button", index, "rose")
76 if button.fell:
77 print("Button", index, "fell")
Pimoroni RGB Base¶
Read the buttons on the Pimoroni RGB Base
examples/tca9555_pimoroni_rgb_base.py¶
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2021 James Carr
3#
4# SPDX-License-Identifier: Unlicense
5
6
7# A simple test for the Pimoroni RGB Base.
8# https://shop.pimoroni.com/products/pico-rgb-keypad-base
9# It reads the button presses and changes the color of the button when pressed.
10
11
12import time
13import sys
14import board
15import busio
16import digitalio
17
18from community_tca9555 import TCA9555
19
20try:
21 from adafruit_debouncer import Debouncer
22except ImportError:
23 # Make sure the Debounce library is available
24 # It is a requirement of this example but not the library.
25 print("Please install the Debounce library:")
26 print(" To install to your Python environment")
27 print(" pip3 install adafruit-circuitpython-debouncer")
28 print(" To install direct to a connected CircuitPython device")
29 print(" circup install adafruit-circuitpython-debouncer")
30 sys.exit()
31
32try:
33 from adafruit_dotstar import DotStar
34except ImportError:
35 # Make sure the DotStar library is available
36 # It is a requirement of this example but not the library.
37 print("Please install the DotStar library:")
38 print(" To install to your Python environment")
39 print(" pip3 install adafruit-circuitpython-dotstar")
40 print(" To install direct to a connected CircuitPython device")
41 print(" circup install adafruit-circuitpython-dotstar")
42 sys.exit()
43
44
45# Get or create an I2C object
46if hasattr(board, "I2C"):
47 i2c = board.I2C()
48elif hasattr(board, "SCL") and hasattr(board, "SDA"):
49 i2c = busio.I2C(scl=board.SCL, sda=board.SDA)
50else:
51 # These pins are for Raspberry Pi Pico
52 # If using another board, these may need to be changed.
53 i2c = busio.I2C(scl=board.GP5, sda=board.GP4)
54
55# Create the TCA9555 expander
56expander = TCA9555(i2c)
57
58
59leds = DotStar(board.GP18, board.GP19, 16, brightness=0.2)
60chip_select = digitalio.DigitalInOut(board.GP17)
61chip_select.direction = digitalio.Direction.OUTPUT
62# There should be nothing else on this SPI bus, so grab it for the duration
63chip_select.value = False
64
65# Prepare to read the 16 inputs
66# Create a tuple of buttons which are debounced so they can be monitored for changes.
67# This reads the bits from the expander individually, instead of as bytes or a word.
68# This is to make the debouncing easier.
69buttons = (
70 Debouncer(lambda: expander.input_port_0_pin_0),
71 Debouncer(lambda: expander.input_port_0_pin_1),
72 Debouncer(lambda: expander.input_port_0_pin_2),
73 Debouncer(lambda: expander.input_port_0_pin_3),
74 Debouncer(lambda: expander.input_port_0_pin_4),
75 Debouncer(lambda: expander.input_port_0_pin_5),
76 Debouncer(lambda: expander.input_port_0_pin_6),
77 Debouncer(lambda: expander.input_port_0_pin_7),
78 Debouncer(lambda: expander.input_port_1_pin_0),
79 Debouncer(lambda: expander.input_port_1_pin_1),
80 Debouncer(lambda: expander.input_port_1_pin_2),
81 Debouncer(lambda: expander.input_port_1_pin_3),
82 Debouncer(lambda: expander.input_port_1_pin_4),
83 Debouncer(lambda: expander.input_port_1_pin_5),
84 Debouncer(lambda: expander.input_port_1_pin_6),
85 Debouncer(lambda: expander.input_port_1_pin_7),
86)
87
88
89# Loop forever - change the color of a button when it is pressed
90with leds:
91 while True:
92 time.sleep(0.001)
93 for index, button in enumerate(buttons):
94 button.update() # Update the debounce information
95 if button.value:
96 # Not pressed
97 leds[index] = (0, 128, 0, 0.05) # Dim green
98 else:
99 # Pressed
100 leds[index] = (255, 0, 128, 0.8) # Bright pink