Overview
It's a GPIO expander, it's a keypad matrix driver... it's the Adafruit TCA8418 Keypad Matrix and GPIO Expander Breakout - a cute and powerful I2C GPIO expander and keypad matrix driver! This chip is quite fancy, with the ability to act as your I2C multi-tool for handling keypads, buttons or LEDs.
This chip has 18 total 'I/O' pins, 10 columns and 8 rows. You can of course arrange them as a matrix of buttons for a total of 80 switches. Or you can use any subset as individual GPIO input or outputs. The nicest part of the keypad driver is that it has a 10-element event queue, so even if you don't get to the interrupt immediately, keypress and release events will be held for you. Since it's I2C, it's very easy to use with any microcontroller or computer.
GPIO expanders work like this: you have a board with some number of GPIO but not enough for your project - maybe you need more buttons or LEDs. You could upgrade to a board with massive number of GPIO like the Grand Central, or you could pop on one of these boards.
Connect it over I2C and then you can send/receive I2C commands to control the GPIO pins to write and read them. It's going to be slower than direct GPIO access, but maybe that doesn't matter if it takes a millisecond instead of a microsecond. You only need the two I2C pins, and you can even share the I2C port with other sensors and devices. Heck, you can even add more expanders for massive I/O control!
For GPIO expansion, any of the 18 pins can be used as input or output. For inputs, you can also set an optional pull-up and an interrupt output on the INT pin for rising or falling signals. There's also a debouncer that can add a little low-pass filtering for noisy tactile switches. For outputs, you can set them high or low, great for basic LED driving.
We've written both Arduino and CircuitPython/Python libraries for the TCA8418, so you can get started whether you have an Arduino-compatible UNO or a Raspberry Pi 4 - or anything in between. There's also a Linux Kernel driver module if you want to have this natively supported by a single-board-computer.
To get you going fast, we spun up a custom-made PCB in the STEMMA QT form factor, making it easy to interface with. There's a built in 3.3V regulator and level shifting on the I2C lines so you can use this with 3V or 5V microcontrollers.
The STEMMA QT connectors on either side are compatible with the SparkFun Qwiic I2C connectors. This allows you to make solderless connections between your development board and the TCA8418 or to chain it with a wide range of other sensors and accessories using a compatible cable. QT Cable is not included, but we have a variety in the shop.
Page last edited March 08, 2024
Text editor powered by tinymce.
Pinouts
The default I2C address is 0x34.
Power Pins
- VIN - this is the power pin. Since the keypad chip uses 3 VDC, we have included a voltage regulator on board that will take 3-5VDC and safely convert it down. To power the board, give it the same power as the logic level of your microcontroller - e.g. for a 5V microcontroller like Arduino, use 5V.
- 3Vo - this is the 3.3V output from the voltage regulator, you can grab up to 100mA from this if you like.
- GND - common ground for power and logic.
I2C Logic Pins
- SCL - I2C clock pin, connect to your microcontroller I2C clock line. This pin is level shifted so you can use 3-5V logic, and there's a 10K pullup on this pin.
- SDA -I2C data pin, connect to your microcontroller I2C data line. This pin is level shifted so you can use 3-5V logic, and there's a 10K pullup on this pin.
- STEMMA QT - These connectors allow you to connectors to dev boards with STEMMA QT connectors or to other things with various associated accessories.
GPIO Pins
- C0-C9 - GPIO or column pins in a keypad matrix. Column number corresponds with pin number
- R0-R7 - GPIO or row pins in a keypad matrix. Row number corresponds with pin number
Page last edited March 08, 2024
Text editor powered by tinymce.
Python & CircuitPython
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(f"Row {row}, Column {col}, Key {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)
Page last edited March 08, 2024
Text editor powered by tinymce.
Arduino
Using the TCA8418 with Arduino involves wiring up the breakout to your Arduino-compatible microcontroller, installing the Adafruit_TCA8418 library and running the provided example code.
Wiring
Wire as shown for a 5V board like an Uno. If you are using a 3V board, like an Adafruit Feather, wire the board's 3V pin to the TCA8418 VIN.
Here is an Adafruit Metro wired up to the TCA8418 using the STEMMA QT connector:
STEMMA
- Board 5V 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
Here is an Adafruit Metro wired up using a solderless breadboard:
STEMMA
- Board 5V 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
Library Installation
You can install the Adafruit TCA8418 library for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for Adafruit TCA8418, and select the Adafruit TCA8418 library:
If asked about dependencies, click "Install all".
If the "Dependencies" window does not come up, then you already have the dependencies installed.
#include <Adafruit_TCA8418.h>
Adafruit_TCA8418 keypad;
#define ROWS 3
#define COLS 4
char keymap[COLS][ROWS] = {
{'*', '0', '#'},
{'7', '8', '9'},
{'4', '5', '6'},
{'1', '2', '3'},
};
void setup() {
Serial.begin(115200);
if (!keypad.begin(TCA8418_DEFAULT_ADDR, &Wire)) {
Serial.println("keypad not found, check wiring & pullups!");
while (1)
;
}
// configure the size of the keypad matrix.
// all other pins will be inputs
keypad.matrix(ROWS, COLS);
// flush the internal buffer
keypad.flush();
}
void loop() {
if (keypad.available() > 0) {
// datasheet page 15 - Table 1
int k = keypad.getEvent();
bool pressed = k & 0x80;
k &= 0x7F;
k--;
uint8_t row = k / 10;
uint8_t col = k % 10;
if (pressed)
Serial.print("PRESS\tR: ");
else
Serial.print("RELEASE\tR: ");
Serial.print(row);
Serial.print("\tC: ");
Serial.print(col);
Serial.print(" - ");
Serial.print(keymap[col][row]);
Serial.println();
}
}
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You'll see the key event, row number, column number and key name appear in the serial monitor as you press the keys.
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)
Button
- Input to breakout R0 (green wire)
- GND to board GND
/***************************************************
@file tca8418_gpio.ino
This is an example for the Adafruit TCA8418 Keypad Matrix / GPIO Expander Breakout
Designed specifically to work with the Adafruit TCA8418 Keypad Matrix
----> https://www.adafruit.com/products/XXXX
These Keypad Matrix use I2C to communicate, 2 pins are required to
interface.
The Keypad Matrix has an interrupt pin to provide fast detection
of changes. This example shows the working of polling.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Adafruit_TCA8418.h>
Adafruit_TCA8418 tio;
void setup()
{
Serial.begin(115200);
while (!Serial) {
delay(10);
}
Serial.println(__FILE__);
if (! tio.begin(TCA8418_DEFAULT_ADDR, &Wire)) {
Serial.println("TCA8418 not found, check wiring & pullups!");
while (1);
}
// SET INPUT MODE
for (int pin = 0; pin < 18; pin++)
{
tio.pinMode(pin, INPUT_PULLUP);
}
delay(1000);
}
void loop()
{
// SHOW PIN STATUS
for (int pin = 0; pin < 18; pin++)
{
Serial.print(tio.digitalRead(pin));
Serial.print(' ');
}
Serial.println();
// other code here
delay(1000);
}
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. All 18 of the TCA8418's inputs are read and their status, 1 for HIGH and 0 for LOW, are printed to the Serial Monitor every second.
Page last edited March 08, 2024
Text editor powered by tinymce.
Downloads
Page last edited March 08, 2024
Text editor powered by tinymce.