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. 

If the dependencies are already installed, you must make sure you update them through the Arduino Library Manager before loading the example!

Keypad Example Code

#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.

GPIO Example Code

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.

This guide was first published on Sep 21, 2022. It was last updated on May 30, 2024.

This page (Arduino) was last updated on May 30, 2024.

Text editor powered by tinymce.