Main Site Documentation

Tech Talk with Gus 13 - Keypad Matrix


Today, we’ll be talking about ‘Membrane Keypad Matrices’. How they work and how to use them in your projects.

Membrane Keypad Question

So then, if I energize all the outputs and then use an interrupt on the input pins, when I press a button, I can then cycle through the outputs quickly to determine which element in the column was actually pushed? Can I do this instead of constantly poling the keypad?


@ Mr. John Smith - in a deterministic environment yes that should work. I haven’t tried it though.


Thanks for the videos, another thing to look forward to on Fridays!

Where is the code for this project? (it may be hidden in plain sight and I’ve missed it)


@ mhectorgato - will post when I get to my PC. It is the holidays and I am busy drinking :dance:


@ Gus - Well don’t drink too much; it looks like you’re slimming down.


@ Gus - Thanks Gus, good topic for me. I remember WBSimms (may not have that correct) put a project on codeshare that used interrupts to minimize the lines used. This looks like a candidate for that hardware/software. If there is a 3x4 and a 4x4 available there are probably more variations as well. Thank you again.


And here is the code

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHI.Pins;

namespace KeyMatrix
    public class Program
        static Bitmap LCD = new Bitmap(480, 272);
        static Font fnt = Resources.GetFont(Resources.FontResources.Arial48);

        static void Print(string str)
            LCD.DrawText(str, fnt, Microsoft.SPOT.Presentation.Media.Color.White, 200, 100);
        public static void Main()
            OutputPort Row1 = new OutputPort(FEZCobraIII.Gpio.D7, false);
            OutputPort Row2 = new OutputPort(FEZCobraIII.Gpio.D6, false);
            OutputPort Row3 = new OutputPort(FEZCobraIII.Gpio.D5, false);
            OutputPort Row4 = new OutputPort(FEZCobraIII.Gpio.D4, false);

            InputPort Col1 = new InputPort(FEZCobraIII.Gpio.D3, false, Port.ResistorMode.PullUp);
            InputPort Col2 = new InputPort(FEZCobraIII.Gpio.D2, false, Port.ResistorMode.PullUp);
            InputPort Col3 = new InputPort(FEZCobraIII.Gpio.D1, false, Port.ResistorMode.PullUp);
            InputPort Col4 = new InputPort(FEZCobraIII.Gpio.D0, false, Port.ResistorMode.PullUp);


            while (true)
                // First Row
                Row1.Write(false); Row2.Write(true); Row3.Write(true); Row4.Write(true);
                if (!Col1.Read())
                if (!Col2.Read())
                if (!Col3.Read())
                if (!Col4.Read())
                // Second Row
                Row1.Write(true); Row2.Write(false); Row3.Write(true); Row4.Write(true);
                if (!Col1.Read())
                if (!Col2.Read())
                if (!Col3.Read())
                if (!Col4.Read())
                // Third Row
                Row1.Write(true); Row2.Write(true); Row3.Write(false); Row4.Write(true);
                if (!Col1.Read())
                if (!Col2.Read())
                if (!Col3.Read())
                if (!Col4.Read())
                // Forth Row
                Row1.Write(true); Row2.Write(true); Row3.Write(true); Row4.Write(false);
                if (!Col1.Read())
                if (!Col2.Read())
                if (!Col3.Read())
                if (!Col4.Read())



Wow, sober after just a couple of days!


No bounce issues with the above code? :open_mouth:


@ Mike - scanning every 20 ms, no bounce :slight_smile:


from the code above:

OutputPort Row1 = new OutputPort(FEZCobraIII.Gpio.D7, false);
OutputPort Row2 = new OutputPort(FEZCobraIII.Gpio.D6, false);

Row1.Write(false); Row2.Write(true); Row3.Write(true); Row4.Write(true);

If I press the “1” and “2” keys at the same time I am shorting the Row1 (low) and Row2 (high) output pins together.
Does the FEZCobra have built in current limiting to protect it against the short between the low and high outputs? - Don M


@ dwmorris - Interesting observation. No there is no current limiting so adding resistors on the output lines would be needed.