Main Site Documentation

Tech Talk with Gus 13 - Keypad Matrix


#1

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


Membrane Keypad Question
#2

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?


#3

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


#4

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)


#5

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


#6

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


#7

@ 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.


#8

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.Clear();
            LCD.DrawText(str, fnt, Microsoft.SPOT.Presentation.Media.Color.White, 200, 100);
            LCD.Flush();
        }
        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);

            Print("Go!");

            while (true)
            {
                // First Row
                Row1.Write(false); Row2.Write(true); Row3.Write(true); Row4.Write(true);
                if (!Col1.Read())
                    Print("1");
                if (!Col2.Read())
                    Print("2");
                if (!Col3.Read())
                    Print("3");
                if (!Col4.Read())
                    Print("A");
                // Second Row
                Row1.Write(true); Row2.Write(false); Row3.Write(true); Row4.Write(true);
                if (!Col1.Read())
                    Print("4");
                if (!Col2.Read())
                    Print("5");
                if (!Col3.Read())
                    Print("6");
                if (!Col4.Read())
                    Print("B");
                // Third Row
                Row1.Write(true); Row2.Write(true); Row3.Write(false); Row4.Write(true);
                if (!Col1.Read())
                    Print("7");
                if (!Col2.Read())
                    Print("8");
                if (!Col3.Read())
                    Print("9");
                if (!Col4.Read())
                    Print("C");
                // Forth Row
                Row1.Write(true); Row2.Write(true); Row3.Write(true); Row4.Write(false);
                if (!Col1.Read())
                    Print("*");
                if (!Col2.Read())
                    Print("0");
                if (!Col3.Read())
                    Print("#");
                if (!Col4.Read())
                    Print("D");


                Thread.Sleep(20);
            }
        }
    }
}


#9

Wow, sober after just a couple of days!


#10

No bounce issues with the above code? :open_mouth:


#11

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


#12

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


#13

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