Using a Keypad With Raspberry Pi

79,495

131

21

Introduction: Using a Keypad With Raspberry Pi

I got two keypads from Jameco.com that I wanted to use with my Raspberry Pi.  Turns out it's quite easy, as long as you know what the pinout structure is for the keypad.

This instructable will take you through the steps I had to go through to discover the pinout on my keypads, and how I hooked it up to my Raspberry Pi.  I'll include the code I'm currently using to "drive" my keypad, and then offer some "next step" type insights into what can be done better, and what could be done next.

Finally, this instructable will really be a sub-instructable that will be part of something I'm calling Voiceberry Pi.

Step 1: Taking a Look at What You've Got

Because my keypad didn't come with a datasheet of any kind, my first step was to make a visual inspection of the keypads.

So, I took a look at the back, saw that there were 10 wires, and three "jumpers" on the outside, but no other indication of how things were wired on the inside.  So, I took out the five screws, and looked at the actual circuit board to trace the leads.

Step 2: Tracing the Leads

I've got 10 leads, but no idea how they lead to the 12 different buttons.  So I have to trace the leads.  The images below trace my particular keypad using red lines.  Dashed lines follow the jumpers in the back.

Pins are either rows or columns.  Rows cover two or more buttons horizontally, and columns cover one or more buttons vertically (even though one key could be a row or a column).  Results of tracing are as follows:

Rows: 1,2,3, and 9
Columns: 4,5,6,7,8, and 10

Button- Row - Col
1  - 1 - 5
2 - 1 - 6
3 - 1 - 7
4 - 2 - 5
5 - 2- 6
6 - 2 - 7
7 - 4 - 5
8 - 4 - 6
9 - 3 - 7
0 - 4 - 7
* - 9 - 8
# - 9 - 10

Step 3: Prepping for Prototyping

Now that I knew what it did, I had to make it do it.  I desoldered the ribbon cable and installed header pins I scavenged off an old video card.  I marked the back of the keypad with the key/pin combinations.

Next I wired it up to the Raspberry Pi.  The actual pins you use don't really matter, as you'll include them in the code, which I've attached to this step.

My code includes a list for the rows:
     rows=[26,24,23,22,11]

A list for the columns:
     cols=[18,16,15,13,7]

And a multidimensional list for the key/button combinations
digits=[["1","2","3"],
        ["4","5","6"],
        ["","","9"],
        ["7","8","0"],
        ["","","","*","#"]]

Finally, there's a loop to poll the columns and see which row was pushed:
    for col in range(0,lenCols):
        if gpio.input(cols[col])==False:
            if colsPusehd[col]==False:
                #print str(cols[col])+" Pushed"
                activeRow=findRow(cols[col])               
                if activeRow>-1:
                    #print(activeRow,col)
                    print digits[activeRow][col]
                sleep(0.1)                   
                colsPusehd[col]=True
        else:
            if colsPusehd[col]==True:
                #print str(cols[col])+" Released"
                sleep(0.1)
                colsPusehd[col]=False

Step 4: Next Steps and Issues

There were two main issues with this project. 

The buttons tended to "bounce"--show more than one press per press.  To eliminate this, I put in a sleep statement of 1/100 of a second in to the code as the button was pressed and another when the button was released.  This made the "bounce" go away.

Additionally, the CPU ran at 100% while polling the rows and columns.  I put in a pause of 1/10 a second at the start of the polling loop, and this dropped the CPU usage to about 50%.  Better, but not great.

Where can I go from here?

First, as already said, the code I created is a little CPU intensive, so the code could be cleaned up to fix that issue.

Next, the code should be object oriented so I can import it into other projects easily.

Third, and I'm not sure if there's a way for this to happen, the code should be event driven.  Poling is not really the best way to do this, even if I can make it less CPU intensive.

Finally, I need to find a use for this.  I mean, just pushing buttons and having the number i push show up on the screen is fun for only so long.

Be the First to Share

    Recommendations

    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • For the Home Contest

      For the Home Contest
    • Big and Small Contest

      Big and Small Contest

    21 Comments

    0
    MartinPacker
    MartinPacker

    2 years ago on Step 4

    Two points:

    1) The way to cut the CPU is to make this interrupt-driven (if you can - and I don’t know how to on Pi).

    2) A good use for this is to allow a button push to kick off automation - perhaps via IFTTT or Zapier or Integromat. In any case a webhook.

    You can see where my interest lies in this: I have a keypad and a Pi and a breadboard and I’d like to prototype a Pi as a kind of Streamdeck.

    0
    GaryM183
    GaryM183

    5 years ago on Step 4

    Hi, I have a use for this code - can it be made to select and play any of 9 preloaded videos? Scenario is a local museum is requesting a screen installation with 9 videos preloaded, each showing a numbered thumbnail. Viewer presses the corresponding video number on the keypad and the video plays. At the end, back to the selection screen. I don't code, but can do the rest. Please let me know if this is a possibility? Cheers, Gaz

    0
    mrmath
    mrmath

    Reply 5 years ago

    Something like this would work, for typing in the number, but the rest of it would need to be coded, too, of course.

    0
    joelm145
    joelm145

    6 years ago

    Hi,
    How can I connect a Blackberry Bold Q10 nano-keyboard to a raspberry?
    I want to make my raspberry portable console with a nano-keyboard of a Blackberry Bold Q10 but i don't know how to connect it XD

    0
    mrmath
    mrmath

    Reply 6 years ago

    I don't think you can. The keyboard is part of the phone, so the circuitry to run the keyboard is in the phone. I wouldn't even know where to start.

    You can buy small bluetooth keyboards that work great with a raspberry pi. I'd look into those if I were you.

    0
    BenN44
    BenN44

    6 years ago

    I wanna couple this with a small display and create a Counter Strike like bomb for airsoft.

    0
    JulianN2
    JulianN2

    7 years ago

    There doesn't seem to be any code attached to this instructable.

    0
    mrmath
    mrmath

    Reply 7 years ago

    The code is mixed in with the text in Step 3

    0
    mjenkins1
    mjenkins1

    7 years ago

    Could you program it to display a different full screen image depending on what number was entered? this would be great for digital signage, perhaps in conjunction with something like Info-beamer pi.

    0
    mrmath
    mrmath

    Reply 7 years ago

    Once you've got the keypad working, you can do anything you want with it.

    0
    JeffA3
    JeffA3

    7 years ago on Introduction

    Mrmath, thank you for the very nice instructable. The lists of which pins correspond to rows and which ones correspond to columns conflict with the table below them: according to the table it looks like pin 4 corresponds to a row, and not a column?

    0
    mrmath
    mrmath

    Reply 7 years ago on Introduction

    It's a weird keypad, and I wouldn't buy it again. But in this case, it's kind of a row in that it gets 7 and 8, and kind of a column in that it gets 8 and 0. But pins 5, 6, and 7 are definitely columns, so I treated pin 4 like a row.

    0
    PC_Payn3
    PC_Payn3

    8 years ago on Introduction

    Hi Everyone, i have a project at home where i can coltrol my lights and water features etc via relays which can be coltrolled over the internet but i have now ordered a 2 in 1 RFID Reader & Ketpad "Image Below" that id like to use as a gate entry that either you can enter a certain code or use the rfid to enter which will turn off a relay, when the relay is turned off the magnetic lock will release and then after a few seconds the relay powers on again and locks.

    CAN ANYBODY HELP

    I have attached some pictures of the 2 in 1 RFID & Keypad, aswel as the sticker that shows what the wires are. But i cannot figure where to wire them to. (Which pins on the Rasp Pi) I understand the Red And black are to go to a 12v power supply.

    Also i have seen so many people using different code, but does anyone have any that would help me at all?

    The wire picture isnt that clear but it shows:


    Colour:Details:BlackGNDRedVCC +12VWhiteD1 / TX / 4R -YellowBUZZERBlueLEDGreenD0 / RX / 4R +Grey26 /34

    I have a Raspberry PI Model B running raspian, i have the following pins available:

    1,3,4,5,8,9,10,14,17,19,20,21,23,24,25

    Look forward to hearing from you Callum

    IMG_3577.JPGIMG_3578.JPG
    0
    km6cg
    km6cg

    9 years ago on Step 4

    I found a discussion of GPIO interrupts at http://ryniker.ods.org/raspberrypi/MagPi/GPIO_interrupts

    I haven't tried it yet, but it seems that this could allow one to suspend execution until a key was pressed.

    My idea for the keyboard is, combine it with a two line display and a wifi adapter and hook up a set of speakers. I'd write a program to load and shuffle music from my DLNA server and put the whole thing in the kitchen. I could also get it to pull and play NPR for me.

    0
    jcksparr0w
    jcksparr0w

    10 years ago on Step 4

    For idea #1, how about a lock of some type?
    #2, Maybe some computer shortcuts.
    #3, I bet you could control something like your house's lighting and electrical outlets somehow.
    The possibilities are endless. The sky's the limit and all those metaphors.

    0
    mrmath
    mrmath

    Reply 10 years ago on Introduction

    Thank you so much! I'm blushing!

    0
    sail4sea
    sail4sea

    10 years ago on Step 4

    I am thinking maybe using a keypad to set the ip address of the pi. Sounds useful for a headless system.