# 4X4 Keypad with multiplexer

**URL:** <https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710>\
**Category:** Programming\
**Created:** [January 17, 2025, 11:53am UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710 "2025-01-17T11:53:15Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![giuli4131](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/giuli4131/32/1128258_2.png) [@giuli4131](https://forum.arduino.cc/u/giuli4131)\
**Post date:** [January 17, 2025, 11:53am UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/1 "2025-01-17T11:53:15Z")

</div>

Hello everybody! I´m new in the site

I´m actually making a MIDI controller with an arduino micro. I´d like to connect a 4x4 keypad to send MIDI notes.

My idea is to connect the keypad trough a 16 channel multiplexer (HC4067) but here is the problem:  
When I connect the keypad directly to arduino pins it works perfectly, but when I modify the code to work with the mux, it doesn´t.

My question is if it´s posible to use the keypad wired to a mux.

Here is the code for the mux:

```arduino
#include <Arduino.h>
#include "Mux.h"

using namespace admux;

/*
 * Creates a Mux instance.
 *
 * 1st argument is the SIG (signal) pin (Arduino digital input pin 3).
 * 2nd argument is the S0-S3 (channel control) pins (Arduino pins 8, 9, 10, 11).
 */
Mux mux(Pin(9, INPUT_PULLUP, PinType::Digital), Pinset(5, 6, 7, 8));

void setup() {
  // Serial port initialization.
  Serial.begin(9600); while (!Serial) /* Waits for serial port to connect (needed for Leonardo only) */;
}

/**
 * Reads the 16 channels and reports on the serial monitor if the corresponding
 * push button is pressed.
 */
void loop() {
  byte data;
  for (byte i = 0; i < mux.channelCount(); i++) {
    data = mux.read(i) /* Reads from channel i (returns HIGH or LOW) */;

    Serial.print("Push button at channel "); Serial.print(i); Serial.print(" is "); Serial.println(data == LOW ? "pressed" : "not pressed");
  }
  Serial.println();

  delay(1500);
}

```

I would like to know if what I want to do is really possible.  
Beyond what was previously mentioned, the idea of the project is to use two multiplexers: one to control potentiometers (which is already working) and another multiplexer for digital inputs.

Thanks!!

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [January 17, 2025, 12:10pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/2 "2025-01-17T12:10:52Z")

</div>

Post the schematic wire of your project, this will make it easier for us to help you.  
Post your Admux namespace contents.  
Also post the link to the mux.h library, as the one I found gives a compilation error with your code.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [January 17, 2025, 12:49pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/3 "2025-01-17T12:49:44Z")

</div>

> [@giuli4131](#):
>
> it´s posible to use the keypad wired to a mux

Not possible. A 4x4 keypad is internally wired as a matrix with 4 columns and 4 rows. The keypad therefore has only 8 connections and there is no way to directly access the 16 keys.

If you have 16 separate keys, you can physically arrange them as a 4x4 matrix but keep the connections to the 16 keys separate. Now you can use your HC4067.

With a 4x4 internally wired matrix, you could use the HC4067 to multiplex either the rows or the columns, but it would save you only 2 Arduino pins. You could use 2x HC4067, one for the rows and one for columns. This would require only 5 Arduino pins. But this does not seem like a very efficient way.

I would recommend using a PCF8574 chip.

---

<div class="post-metadata">

**Author:** ![giuli4131](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/giuli4131/32/1128258_2.png) [@giuli4131](https://forum.arduino.cc/u/giuli4131)\
**Post date:** [January 17, 2025, 12:51pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/4 "2025-01-17T12:51:08Z")

</div>

For practical porpuses I made this drawing with the UNO board because that app hasn´t got the micro. However the pin numbers connections are the same

 ![Captura de pantalla 2025-01-17 094935](https://europe1.discourse-cdn.com/arduino/original/4X/a/f/0/af0826ef9ca1a4e332c47df4e945a786510904c5.jpeg)

The mux library link is:

> **[GitHub - stechio/arduino-ad-mux-lib: Arduino library for controlling analog/digital...](https://github.com/stechio/arduino-ad-mux-lib)**
>
> Arduino library for controlling analog/digital multiplexers (A/D mux)

Thanks for the response!!

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [January 17, 2025, 12:56pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/5 "2025-01-17T12:56:55Z")

</div>

> [@giuli4131](#):
>
> I made this drawing

This cannot work. See my explanation above.

---

<div class="post-metadata">

**Author:** ![Dave\_Lowther](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dave_lowther/32/1092885_2.png) [@Dave\_Lowther](https://forum.arduino.cc/u/Dave_Lowther)\
**Post date:** [January 17, 2025, 1:05pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/6 "2025-01-17T13:05:01Z")

</div>

> [@giuli4131](#):
>
> My idea is to connect the keypad trough a 16 channel multiplexer (HC4067) but here is the problem:  
> When I connect the keypad directly to arduino pins it works perfectly, but when I modify the code to work with the mux, it doesn´t.

Why do you want to use a multiplexer? Is it because you don't have 8 pins available to access the keypad directly from the Arduino?

---

<div class="post-metadata">

**Author:** ![giuli4131](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/giuli4131/32/1128258_2.png) [@giuli4131](https://forum.arduino.cc/u/giuli4131)\
**Post date:** [January 17, 2025, 1:09pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/7 "2025-01-17T13:09:02Z")

</div>

Thanks for the recomendation

I was recently reading something about PCF8574 chip. The correct way to wire it is directly to GND, VCC and 2 digital pins? And it able to expand up to 8 digital inputs? That´s right?

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [January 17, 2025, 1:12pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/8 "2025-01-17T13:12:46Z")

</div>

> [@giuli4131](#):
>
> 2 digital pins?

2 specific digital pins, yes. The SDA and SCL pins. Take care because these are different pins on the Uno compared to Pro Micro.

Also if you use a bare chip on a breadboard, don't forget to add a 0.1uF ceramic bypass capacitor.

> [@giuli4131](#):
>
> And it able to expand up to 8 digital inputs?

Yes. Or 8 outputs. Or any combination of inputs and outputs. With the 4x4 keypad you would use 4 of the pins as inputs and 4 as outputs.

---

<div class="post-metadata">

**Author:** ![giuli4131](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/giuli4131/32/1128258_2.png) [@giuli4131](https://forum.arduino.cc/u/giuli4131)\
**Post date:** [January 17, 2025, 1:18pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/9 "2025-01-17T13:18:44Z")

</div>

Exactly

I've connected 14/15 potentiometers using one multiplexer (this is already working)  
And for buttons, my idea was to put the 4x4 keypad + another 8 individual buttons using another multiplexer

So, if I wire the keypad directly, digital inputs are´t sufficient

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [January 17, 2025, 1:25pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/10 "2025-01-17T13:25:46Z")

</div>

> [@giuli4131](#):
>
> another 8 individual buttons using another multiplexer

Use another PCF8574 for these.

Or a PCF8575 which was 16 input/outputs.

In fact, you could wire your 8 individual buttons together with your 4x4 keypad to make a single 6x4 keypad. This would require 10 of the PCF8575's pins, leaving 6 unused.

You could use 4 of those unused pins to control the channel selection pins of your HC4067.

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [January 17, 2025, 1:29pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/11 "2025-01-17T13:29:22Z")

</div>

Maybe consider this:

> **[Adafruit TCA8418 Keypad Matrix and GPIO Expander Breakout](https://www.adafruit.com/product/4918)**
>
> It's a GPIO expander, it's a keypad matrix driver... its the Adafruit TCA8418 Keypad Matrix and GPIO Expander Breakout - a cute and powerful I2C GPIO expander and keypad matrix ...

You can connect the keypad and the 8 buttons to it

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [January 17, 2025, 1:58pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/12 "2025-01-17T13:58:38Z")

</div>

> [@giuli4131](#):
>
> the idea of the project is to use two multiplexers: one to control potentiometers (which is already working) and another multiplexer for digital inputs.

I agree you will need one multiplexer for up to 16 potentiometers, because Pro Micro doesn't have 16 analog inputs.

But for your other digital inputs, it may be possible to wire them all without any extra multiplexers or other chips. Including the channel selection inputs of your multiplexer.

So far you have mentioned  
4x4 keypad  
8 additional pushbuttons  
HC4067 multiplexer

Is there anything more?

---

<div class="post-metadata">

**Author:** ![giuli4131](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/giuli4131/32/1128258_2.png) [@giuli4131](https://forum.arduino.cc/u/giuli4131)\
**Post date:** [January 17, 2025, 2:11pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/13 "2025-01-17T14:11:01Z")

</div>

For the initial step of the project that´s all. Maybe later, i´d like to add more pushbuttons, but if I do this, i think that´s neccesary to include an expansion port.

For the moment with the mux for potentiometers + keypad + 8 pushbuttons is sufficient

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [January 17, 2025, 2:28pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/14 "2025-01-17T14:28:04Z")

</div>

Written a library for Keypad 4x4 or smaller

- [GitHub - RobTillaart/I2CKeyPad: Arduino library for 4x4 (or smaller) keypad connected to an I2C PCF8574.](https://github.com/RobTillaart/I2CKeyPad)

For the 8 push buttons

- [GitHub - RobTillaart/PCF8574: Arduino library for PCF8574 - I2C IO expander](https://github.com/RobTillaart/PCF8574)

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [January 17, 2025, 2:59pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/15 "2025-01-17T14:59:58Z")

</div>

Ok, Pro Micro has these pins:

 ![images (4)](https://europe1.discourse-cdn.com/arduino/original/4X/8/2/0/8208b721b8f7d8e9c2e849a077db6e010fc0fe92.jpeg)  
On Pro Micro, pins 0 and 1 are available to use as digital inputs or outputs (unlike Uno and some other Arduino models). So you have 18 available pins.

For your circuit, you need to dedicate 1 analog input to connect to the multiplexer's output, to read up to 16 pots.

You can combine your 4x4 keypad with your 8 additional buttons to make a 4x6 keypad. This will require 10 pins. 4 outputs to select the row and 6 inputs to read the columns.

That's 11 of 18 pins used so far.

You could use 4 more output pins to control the multiplexer. This would leave you with 3 unused pins.

I would suggest that you keep pins 2 & 3 available, because these are SDA and SCL and can be used to add "expansion ports" as you call them.

However, alternatively, you could use those same 4 outputs that select the keypad row to additionally control the multiplexer. Then you would still have 7 unused pins!

If you do that, I would suggest not using OUTPUT mode and HIGH on those pins. That is because there is a danger, albeit very small, that if multiple keypad keys are pressed at the same time, a short-circuit could happen between one pin which is set OUTPUT & HIGH and another pin which is set OUTPUT & LOW. So instead, use INPUT\_PULLUP.

EDIT: That would remove the small danger of a short circuit, but there would then also be a small danger of multiple keys pressed at the same time could cause the multiplexer to select the wrong channel, causing the Arduino's analog input pin to read the wrong potentiometer. No danger of damage, but an unexpected effect! But like I said, a very small danger, probably not worth worrying about. I think 3 keys, in two different rows and 2 different columns, would have to be pressed simultaneously for this to happen!

---

<div class="post-metadata">

**Author:** ![giuli4131](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/giuli4131/32/1128258_2.png) [@giuli4131](https://forum.arduino.cc/u/giuli4131)\
**Post date:** [January 17, 2025, 3:31pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/16 "2025-01-17T15:31:49Z")

</div>

Thank you so much for the support guys!!

I´ll try connecting the pins like that, and in the future I´ll probably buy an expansion port for the additional buttons.

Greetings!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 16, 2025, 3:32pm UTC](https://forum.arduino.cc/t/4x4-keypad-with-multiplexer/1343710/17 "2025-07-16T15:32:23Z")

</div>

Closed on July 16, 2025, 3:32pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
