It's easy to use the TCA8418 with Python or CircuitPython, and the Adafruit_CircuitPython_TCA8418 module. This module allows you to easily write Python code that reads the TCA8418's GPIO pins as a GPIO expander or keypad matrix. You can use this breakout with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.
CircuitPython Microcontroller Wiring
First, wire up a TCA8418 and keypad to your board exactly as shown below. Here's an example of wiring a Feather M4 to the breakout with I2C using one of the handy STEMMA QT connectors:
TCA8418
- Board 3V to breakout VIN (red wire)
- Board GND to breakout GND (black wire)
- Board SCL to breakout SCL (yellow wire)
- Board SDA to breakout SDA (blue wire)
Keypad
- Pin 1 to breakout C3
- Pin 2 to breakout C2
- Pin 3 to breakout C1
- Pin 4 to breakout C0
- Pin 5 to breakout R2
- Pin 6 to breakout R1
- Pin 7 to breakout R0
You can also use standard 0.100" pitch headers to wire it up on a breadboard:
TCA8418
- Board 3V to breakout VIN (red wire)
- Board GND to breakout GND (black wire)
- Board SCL to breakout SCL (yellow wire)
- Board SDA to breakout SDA (blue wire)
Keypad
- Pin 1 to breakout C3
- Pin 2 to breakout C2
- Pin 3 to breakout C1
- Pin 4 to breakout C0
- Pin 5 to breakout R2
- Pin 6 to breakout R1
- Pin 7 to breakout R0
Python Computer Wiring
Since there's dozens of Linux computers/boards you can use, below shows wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
Here's the Raspberry Pi wired to the breakout using I2C and a STEMMA QT connector:
TCA8418
- Pi 3V to breakout VIN (red wire)
- Pi GND to breakout GND (black wire)
- Pi SCL to breakout SCL (yellow wire)
- Pi SDA to breakout SDA (blue wire)
Keypad
- Pin 1 to breakout C3
- Pin 2 to breakout C2
- Pin 3 to breakout C1
- Pin 4 to breakout C0
- Pin 5 to breakout R2
- Pin 6 to breakout R1
- Pin 7 to breakout R0
Finally here is an example of how to wire up a Raspberry Pi to the sensor using a solderless breadboard:
TCA8418
- Pi 3V to breakout VIN (red wire)
- Pi GND to breakout GND (black wire)
- Pi SCL to breakout SCL (yellow wire)
- Pi SDA to breakout SDA (blue wire)
Keypad
- Pin 1 to breakout C3
- Pin 2 to breakout C2
- Pin 3 to breakout C1
- Pin 4 to breakout C0
- Pin 5 to breakout R2
- Pin 6 to breakout R1
- Pin 7 to breakout R0
Python Installation of TCA8418 Library
You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!
Once that's done, from your command line run the following command:
pip3 install adafruit-circuitpython-tca8418
If your default Python is version 3, you may need to run pip
instead. Make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!
CircuitPython Usage
To use with CircuitPython, you need to first install the TCA8418 library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folders and file:
- adafruit_bus_device/
- adafruit_register/
- adafruit_tca8418.mpy
Python Usage
Once you have the library pip3
installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:
python3 code.py
# SPDX-FileCopyrightText: Copyright (c) 2022 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: Unlicense import time import board from adafruit_tca8418 import TCA8418 i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller tca = TCA8418(i2c) keymap = (("*", "0", "#"), ("7", "8", "9"), ("4", "5", "6"), ("1", "2", "3")) # set up all R0-R2 pins and C0-C3 pins as keypads KEYPADPINS = ( TCA8418.R0, TCA8418.R1, TCA8418.R2, TCA8418.C0, TCA8418.C1, TCA8418.C2, TCA8418.C3, ) # make them inputs with pullups for pin in KEYPADPINS: tca.keypad_mode[pin] = True # make sure the key pins generate FIFO events tca.enable_int[pin] = True # we will stick events into the FIFO queue tca.event_mode_fifo[pin] = True # turn on INT output pin tca.key_intenable = True while True: if tca.key_int: # first figure out how big the queue is events = tca.events_count # now print keyevent, row, column & key name for _ in range(events): keyevent = tca.next_event # strip keyevent event = keyevent & 0x7F event -= 1 # figure out row row = event // 10 # figure out column col = event % 10 # print event type first if keyevent & 0x80: print("Key down") else: print("Key up") # use row & column coordinates to print key name print("Row %d, Column %d, Key %s" % (row, col, keymap[col][row])) tca.key_int = True # clear the IRQ by writing 1 to it time.sleep(0.01)
If running CircuitPython: Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
If running Python: The console output will appear wherever you are running Python.
In the example, the TCA8418 is instantiated on I2C. Then, the keypad matrix is setup. The key names are setup in a matrix as well.
In the loop, every time you press or release a key the key event is printed to the REPL, followed by the row, column and key name that was used in the event.
TCA8418
- Board 3V to breakout VIN (red wire)
- Board GND to breakout GND (black wire)
- Board SCL to breakout SCL (yellow wire)
- Board SDA to breakout SDA (blue wire)
LED
- Anode to breakout R0 (green wire)
- Cathode to board GND (black wire)
Button
- Input to breakout R1 (pink wire)
- GND to board GND (black wire)
# SPDX-FileCopyrightText: Copyright (c) 2022 ladyada for Adafruit Industries # # SPDX-License-Identifier: Unlicense import time import board from adafruit_tca8418 import TCA8418 i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller tca = TCA8418(i2c) # setup 2 gpio: LED on R0 and button in R1 OUTPIN = TCA8418.R0 INPIN = TCA8418.R1 tca.gpio_mode[OUTPIN] = True tca.gpio_mode[INPIN] = True # one is output, other is input with pullup tca.gpio_direction[OUTPIN] = True tca.gpio_direction[INPIN] = False tca.pullup[INPIN] = True # have LED mirror button while True: tca.output_value[OUTPIN] = tca.input_value[INPIN] time.sleep(0.01)
Text editor powered by tinymce.