/* Arduino I2C-Keypad using an I2C_Expander module

Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include <Keypad_I2C.h> // I2C Keypad library by Joe Young https://github.com/joeyoung/arduino_keypads
#include <LiquidCrystal_I2C.h>  // I2C LCD Library by Francisco Malpartida https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home

#define lcd_addr 0x27     // I2C address of typical I2C LCD Backpack
#define keypad_addr 0x20  // I2C address of I2C Expander module (A0-A1-A2 dip switch to off position)

// LCD Pins to I2C LCD Backpack - These are default for HD44780 LCD's
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

// Create instance for LCD called: i2c_lcd
LiquidCrystal_I2C i2c_lcd(lcd_addr,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

// Define the keypad pins
const byte ROWS = 4; 
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

// Keypad pins connected to the I2C-Expander pins P0-P6
byte rowPins[ROWS] = {0, 1, 2, 3}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 5, 6};    // connect to the column pinouts of the keypad

// Create instance of the Keypad name I2C_Keypad and using the PCF8574 chip
Keypad_I2C I2C_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS, keypad_addr, PCF8574 );

void setup() {
  
  i2c_lcd.begin (16,2); //  our LCD is a 16x2, change for your LCD if needed
  
  // LCD Backlight ON
  i2c_lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  i2c_lcd.setBacklight(HIGH); 
  i2c_lcd.clear(); // Clear the LCD screen
                
  I2C_Keypad.begin();
  
}

void loop() {

  // Put value of pressed key on keypad in key variable
  char key = I2C_Keypad.getKey();

  // Light up key pressed on LCD
  switch (key) {
    case '1':
      i2c_lcd.setCursor(0,0);
      i2c_lcd.print(key);
    break;

    case '2':
      i2c_lcd.setCursor(0,1);
      i2c_lcd.print(key);
    break;

    case '3':
      i2c_lcd.setCursor(3,0);
      i2c_lcd.print(key);
    break;

    case '4':
      i2c_lcd.setCursor(3,1);
      i2c_lcd.print(key);
    break;

    case '5':
      i2c_lcd.setCursor(6,0);
      i2c_lcd.print(key);
    break;

    case '6':
      i2c_lcd.setCursor(6,1);
      i2c_lcd.print(key);
    break;

    case '7':
      i2c_lcd.setCursor(9,0);
      i2c_lcd.print(key);
    break;

    case '8':
      i2c_lcd.setCursor(9,1);
      i2c_lcd.print(key);
    break;

    case '9':
      i2c_lcd.setCursor(12,0);
      i2c_lcd.print(key);
    break;

    case '0':
      i2c_lcd.setCursor(12,1);
      i2c_lcd.print(key);
    break;

    case '*':
      i2c_lcd.clear();
    break;

    case '#':
      i2c_lcd.setCursor(15,0);
      i2c_lcd.print(key);
    break;
    }
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
$abcdeabcde151015202530354045505560fghijfghij
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
keypad1:R1
keypad1:R2
keypad1:R3
keypad1:R4
keypad1:C1
keypad1:C2
keypad1:C3
keypad1:C4
8bit I2CBreakout
chip1:R1
chip1:R2
chip1:R3
chip1:R4
chip1:C1
chip1:C2
chip1:C3
chip1:C4
chip1:SDA
chip1:SCL
chip1:GND
chip1:VCC