[Solved] Keypad matrix with more than 4 columns issue

Hi all. I'm making a macro keyboard by using a pro micro and a button matrix.
My issue is that the keypad code I've been working with seems to only work when I have max 4 rows. A fifth row of buttons does not work as soon as I add that row and edit the code to how logically I would think it should be changed. The buttons on the right 4 rows still still trigger the serial print but the leftmost buttons do not. (By the way, the rows are up and down due to not wanting to change too much of my code.)

How can I edit my code to allow more than 4 rows? Or is my circuit incorrect? My goal is 48 buttons with a 12x4 matrix (keys) but for now I'm using a 5x2 button matrix.

The edits I made to the regular 4x4 keypad code include changing the number of rows/cols to 5/2,
changing the keymap keys, and changing the pins.
This is the code I'm currently working with:

#include <Keypad.h>

const byte ROWS = 5; //four rows
const byte COLS = 2; //four columns
//define the cymbols on the buttons of the keypads
char keys[ROWS][COLS] = {
byte rowPins[ROWS] = {8,7,6,5,4}; 
byte colPins[COLS] = {3,2}; 

//initialize an instance of class NewKeypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); 

void setup(){
//  Serial.begin(19200);
void loop(){
  char key = keypad.getKey();
  if (key){

And this is my circuit. The row wires are at the top going to pins 8-4.
The column wires are on the right going to pins 3-2.:

Any help would be greatly appreciated!

You appear to have the keypad diodes wired incorrectly. You have the diodes in series, feeding into the column pin, while they should connect from each individual button to the column pin.

Hi, @carloszman43
Welcome to the forum.

Thanks for using code tags, makes things so much easier.

Can you please post a schematic of your project?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

You replied while I was editing my first post to add a bit more information. The diodes are not reversed, they are wired in series on your breadboard. This is how the keypad should be wired (schematic taken from https://www.gammon.com.au/forum/?id=14175 ). You will not need the resistors, the keypad library will use the internal pullup resistors.

Ohh I see. Ok I'll try that.

Ah ok whoops sorry. Thanks for the example schematic!

diodes aren't necessary if only one row is made an output LOW and all others allowed to float. the columns can use internal pull-ups (i.e. INPUT_PULLUP)

a 12x4 matrix would save pins by organizing it as a 7x7 matrix

Oh yeah thanks for the good point! I guess I was focused on how I wanted my keys to look that I forgot that the wiring could be different.

You don't usually need any diodes on a macro keyboard. Diodes are only needed where you have "shift", "ctrl" type keys which would be pressed at the same time as another key. On a QWERTY keyboard, diodes are needed because during high-speed typing, the next key might get pressed before the previous key has been released. On a musical keyboard, the diodes are needed because playing chords means that multiple keys are pressed at once. For a macro keyboard, usually only one key is pressed at once, so no diodes are needed.

Thanks for the warm welcome, Tom! The schematic David shared seems to be right so I'll remember to include a schematic in future topics. I'm glad someone from the UK is online to help with my 4am macro keyboard building :sweat_smile: :relaxed:

What makes you think that Tom is in the UK ?

Oh haha whoops. Australia I mean :blush: I didn't see the emoji that clearly I guess

Thanks for all the help guys! @david_2018 was right about the circuit. Got it to work with a lot more wires. Looking forward to building the real thing soon :blush: