4X4 Keypad with multiplexer

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:

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

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.

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.

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

The mux library link is:

Thanks for the response!!

This cannot work. See my explanation above.

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?

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?

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.

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.

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

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.

Maybe consider this:

You can connect the keypad and the 8 buttons to it

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?

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

Written a library for Keypad 4x4 or smaller

For the 8 push buttons

Ok, Pro Micro has these pins:


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!

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!

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