Home Resources

 

HOW TO: Interface a 4x3 Matrix Keypad to the FlamingICE Microcontroller 

 matrix keypad interface

 

You will need:

 

  • 4x3 Membrane Keypad (or any other similar keypads you have lying around)
  • Four 470? resistors
  • Wires (for connecting the keypad to the microcontroller)
  • A 6V-12V DC Power Supply
  • FlamingICE Microcontroller Board (FI28/FI40/FI80)

 

 

 

Introduction

In this tutorial, we’re going to use the most straight-forward way of obtaining which button is being pressed from the 4x3 Membrane Keypad. Please note that there are tons of methods out there on how to do this and while this might not be the best way, most beginners should find it simple and fast to get started from the hardware connections to the explanations on how the programming is done.

 

 

The Hardware

matrix keypad schematic

 

 

As per the diagram above illustrating the entire interfacing connections, we will be using a FI28 Microcontroller Board to do this. We will be using 7 I/O lines to connect to the 4x3 matrix keypad, with an extra power line that ties the rows ‘high’ through a 470 ? resistor on each row line.  We shall leave the columns alone and connect it directly to the Microcontroller Board.

 

Now that we’ve got the hardware up, let’s start programming.

 

The Software

The 4x3 Matrix Keypad is essentially pushbuttons arranged and connected in an XY matrix. Allow me to illustrate with the following figure.

 

keypad internal circuit

 

So what we are going to try to find out when a key is pressed is the row and column that a closed circuit is occurring. We do this by checking the columns and rows in turn.

We start off by setting the state of the pins used to logic ‘high’. This is to initialize the pins at the beginning.

CODE:

High(1)

High(2)

High(3)

High(4)

High(5)

High(6)

High(7)

It really doesn’t matter which one we check first but we are going check the columns first, then the rows.

 

The pressing of key would result in the flow of current and one or more of the input pins would be logic ‘low’. Since current flows from a higher potential to a lower potential, we would need to set the column pins high and the row pins to a logic low when scanning the columns. This action is reversed when scanning the rows.

 

CODE:

‘scanning the columns

low(1)

low(2)

low(3)

low(4)

high(5)

high(6)

high(7)

‘wait for voltage to settle

Delay(1)

C1 = GetPin(5)

C2 = GetPin(6)

C3 = GetPin(7)

 

Here’s a little trick that we’re going to apply based on our hardware connections.

We are going to combine the logic state of the pins as individual bits into a single byte. However, since we only have 7 lines, we will only use 7 bits.

E.g. we let column3 on pin 7 represent the 7th bit in a byte.

 

keypad_bytePos.png

Following the same concept for the rest of the pins, we apply a little math after getting the logic state of the pins.

 

 

CODE:

 

‘Scanning the columns, and then doing the math

C1 = GetPin(5)

C2 = GetPin(6)

C3 = GetPin(7)

d1 = c1 * 16 + c2 * 32 + c3 * 64

 

‘Scanning the Rows, and then doing the math

C1 = GetPin(1)

C2 = GetPin(2)

C3 = GetPin(3)

C4 = GetPin(4)

d2 = c1 * 1 + c2 * 2 + c3 * 4 + c4 * 8

 

The value obtained later on should correspond to the respective key pressed. The figure below can be used as a reference.

 

keypad_byteVal.png

 

TIP: Using the keys to carry out commands is now as simple as using a select case statement based on the byte value.

 

CODE:

 

'Module1

Public C1, C2, C3, C4 As Integer

Public  d, d1, d2 As Integer

'======================================================================

'======================================================================

 

Public Sub Main()

 

High(1)

High(2)

High(3)

High(4)

High(5)

High(6)

High(7)

 

 

‘keep checking for a key to be pressed
Do

      low(1)

      low(2)

      low(3)

      low(4)

      high(5)

      high(6)

      high(7)

‘wait for voltage to settle

      Delay(1)

      C1 = GetPin(5)

      C2 = GetPin(6)

      C3 = GetPin(7)

      Delay(1)

      C1 = GetPin(5)

      C2 = GetPin(6)

      C3 = GetPin(7)

      d1 = c1 * 16 + c2 * 32 + c3 * 64

 

      high(1)

      high(2)

      high(3)

      high(4)

      low(5)

      low(6)

      low(7)

‘wait for voltage to settle

      Delay(1)

      C1 = GetPin(1)

      C2 = GetPin(2)

      C3 = GetPin(3)

      C4 = GetPin(4)

      Delay(1)

      C1 = GetPin(1)

      C2 = GetPin(2)

      C3 = GetPin(3)

      C4 = GetPin(4)

 

      d2 = c1 * 1 + c2 * 2 + c3 * 4 + c4 * 8

 

      d = d1 + d2

      debug.Print cstr(d)

      Delay(200)

Loop

 

End Sub

 

 

 


download_sourceCode.png