Interfacing 4x4 keypad using 5 arduino pins

Rummaging in my old electronic stuff I found a 74HCT157 (Quad 2:1 mux) and immediately crossed to my mind to use it in my current 4x4 keypad interface that uses 8 pins of my uno board...and save 3 pins. It sounds easy but I need help here figuring out how to tell the Arduino that read 4 pins with the select pin S of the 74HCT157 in LOW, save that status and then that read the same 4 pins with the select pin S of the 74HCT157 in HIGH and save that another status and finally put them together. I made a schematic showing the circuitry between keypad, mux and Arduino. (see picture above below). Thank yo for your help!

This won't work. All I inputs to the multiplexer are undriven, all times, irrespective of key presses.

You could use an I/O expander like the MCP23009 as it has programmable I/O. Just add some pull-ups.

Other than the input to the keyboard you need to tie the NOT Enable to low.

Mark

Finally, I finished my small project to integrate a 4x4 keypad with Arduino uno using only five pins. At first I had planned to implement it using a 74HCT157. After some failed design attempts, I moved to the idea of use a 4052 but even as it resolved the rows writing, it took another IC: a 74HC165 for the columns reading. Here my raw code. I don't like it ‘as is’ but it works!:

#define PIN3  3
#define PIN4  4

int latchPin = 5;
int dataPin = 6;
int clockPin = 7;
int input;

void setup() {
  pinMode(PIN3, OUTPUT);
  pinMode(PIN4, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  Serial.begin(9600);
}

uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
	uint8_t value = 0;
	uint8_t i;

	for (i = 0; i < 4; ++i) {
		digitalWrite(clockPin, HIGH);
		if (bitOrder == LSBFIRST)
			value |= digitalRead(dataPin) << i;
		else
			value |= digitalRead(dataPin) << (7 - i);
		digitalWrite(clockPin, LOW);
	}
	return value;
}

void loop() {
  
  setMUX(LOW, LOW);
  digitalWrite(7, HIGH);
  digitalWrite(5, HIGH);
  input = shiftIn(6, 7, LSBFIRST);
  digitalWrite(5, LOW);
  
  switch (input) {
    case 14:
      Serial.println("1");
      break;
    case 13:
      Serial.println("2");
      break;
    case 7:
      Serial.println("3");
      break;
    case 11:
      Serial.println("A");
      break;
  }
  delay(10); 
  
  setMUX(LOW, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(5, HIGH);
  input = shiftIn(6, 7, LSBFIRST);
  digitalWrite(5, LOW);
  
  switch (input) {
    case 14:
      Serial.println("4");
      break;
    case 13:
      Serial.println("5");
      break;
    case 7:
      Serial.println("6");
      break;
    case 11:
      Serial.println("B");
      break;
  }
  delay(10); 
  
  setMUX(HIGH, LOW);
  digitalWrite(7, HIGH);
  digitalWrite(5, HIGH);
  input = shiftIn(6, 7, LSBFIRST);
  digitalWrite(5, LOW);
  
  switch (input) {
    case 14:
      Serial.println("7");
      break;
    case 13:
      Serial.println("8");
      break;
    case 7:
      Serial.println("9");
      break;
    case 11:
      Serial.println("C");
      break;
  }
  delay(10); 

setMUX(HIGH, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(5, HIGH);
  input = shiftIn(6, 7, LSBFIRST);
  digitalWrite(5, LOW);
   
  switch (input) {
    case 14:
      Serial.println("*");
      break;
    case 13:
      Serial.println("0");
      break;
    case 7:
      Serial.println("#");
      break;
    case 11:
      Serial.println("D");
      break;
  }
  delay(10);  

}
  
void setMUX(uint8_t a, uint8_t b)
{
  digitalWrite(PIN3, a);
  digitalWrite(PIN4, b);
  //delay(1000);
}

I can post here the connections between the two ICs and the Arduino if requested.

Palliser:
Finally, I finished my small project to integrate a 4x4 keypad with Arduino uno using only five pins. At first I had planned to implement it using a 74HCT157. After some failed design attempts, I moved to the idea of use a 4052 but even as it resolved the rows writing, it took another IC: a 74HC165 for the columns reading. Here my raw code. I don't like it ‘as is’ but it works!:

Congrats. What about it don't you like?

Palliser:
I can post here the connections between the two ICs and the Arduino if requested.

Please do. It's always good I think to see how lots of different approaches can be used to resolve the one challenge.

Geoff

Here the schematic showing interconnection between the 4052, 74HC165 and Arduino uno. Notice that it is only used half of the capacity in both ICs.