Keyboard matrix help (Teensy 3.0; constant output when nothing is pressed)

Not open for further replies.


New member
Hello everyone,

First of I want to share that I have experience with programming but zero of it with microcontrollers, so I apologise beforehand for any stupid mistakes.

I want to make a simple keyboard matrix of 6 buttons to work, but the output I am getting from the teensy seems random to me.

I am using pins 3-7. I was using the ones from 2-6 but I changed them in an attempt to solve the issue.
Pins 3-5 are columns. Pins 6 and 7 are rows.

This is the code. I also tried with Keyboard.send_now(). I am not using Keyboard.print() because later on I would like to use the keys for media controlls via HID codes.
#include <usb_keyboard.h>
int keyArray[] = {0x1E /*1*/, 0x1F /*2*/, 0x20 /*3*/, 0x21 /*4*/, 0x22 /*5*/, 0x23 /*6*/};

void setup()
   //pinMode(13, OUTPUT);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(6, INPUT);
   pinMode(7, INPUT);

void loop()
    /* Testing code
    digitalWrite(13, HIGH);
    digitalWrite(13, LOW);
    for(int i = 3; i <= 5; i++)
      digitalWrite(i, HIGH);
      for(int j = 6; j <= 7; j++)
          int index = (i - 3) * 2 + (j - 6);
          keyboard_keys[0] = keyArray[index];;

          keyboard_keys[0] = 0;
      digitalWrite(i, LOW);

When I tried seeing what was idling with the below code it always returns the same button when I'm not pressing anything. i - 5, j - 6 - which corresponds to the bottom left button on the image.

Here's an image of my soldering. Red numbers are pin numbers. White numbers are what is supposed to be output from the keys.

What is actually being output is this:

Expected output: --- Actual output:
1 --------------------- 13
2 --------------------- 24(5)
3 --------------------- 35
4 --------------------- 4(5)6
5 --------------------- 51
6 --------------------- 62(5)
The 5 in the brackets sometimes is there, sometimes it's not....
When leaving it idle while testing the actual output it sometimes starts spamming 5s, "as it should" since it 's the key output from the debug println above, but not before scrambling through other numbers.
In other cases it continues outputting random numbers. I have observed that it is relevant with the position of the teensy but I have not reached a concrete scenario and I couldn't find any potential shortages in my solders.
I also tried playing around with the pinmodes - result was either the same or nothing was returned.

Thanks in advance. Any help is highly appreciated.


  • IMAG0151.jpg
    83.5 KB · Views: 201
Use the Bounce library for detecting button changes. It's much easier than digitalRead() and it automatically handles mechanical chatter in the buttons.

Start with File > Examples > Teensy > USB_Keyboard > Buttons
Thanks for the answer but I am trying to accomplish this with a matrix. I have 3 columns and 2 rows and as far as I understand Bounce requires each button to be individually connected to a teensy pin and the GND pin.

I tried to define the rows as buttons and check with fallingEdge() for key presses. I also tried with the Keypad example but nothing is being printed in both cases.

Also I don't think this is a problem with mechanical chatter since it's not a single button repeating. The idle spam is endless, it doesn't stop shortly after key presses.
Not open for further replies.