Matrix Keypad Interfacing with Arduino

The keyboard matrix is the arrangement of circuit connections between the keyboard controller and all the keys on the keyboard. Each key does not have its own dedicated circuit; instead, each key is placed at the intersection of a matrix row and a matrix column. The keyboard repeatedly applies current to each column in turn, and checks to see which rows output current. From this, the keyboard can deduce which keys in that column have been depressed. The matrix takes the form of a printed circuit, either on a conventional PCB, or on membrane sheets.

The matrix arrangement allows for current to flow backwards through part of the circuit, which leads to “ghost” keypresses being detected when certain groups of three keys are held down. This ghosting phenomemon can be prevented by placing diodes in series with each switch (so long as the keyboard has a PCB) or by using capacitive sensing. The problem can be mitigated by using a gaming-optimised matrix or by blocking any combination of three keys or more. The latter prevents keyboard shortcuts from functioning, so the matrix is laid out in such a way as to ensure that pressing groups of three or four keys works so long as all but one of the keys are modifiers. Keyboard shortcuts involving multiple modifiers is a relative modern phenomenon on PCs, so vintage keyboards without diode protection will either ghost or block certain shortcuts.

As a result of this and the fact that keys on the keyboard are not all square, the matrix arrangement does not take the form of a regular grid. Adjacent matrix intersections do not always correspond with adjacent keys and vice versa.

Contents

1. Where Matrix keypad is used?
2. What is Matrix Keypad?
3. How to detect a key press?
4. 4×4 Keypad Pin diagram
5. Arduino 4×4 Keypad connections
6. 4×4 Matrix Keypad Library.
7. Arduino 4×4 keypad code

Where Matrix keypad is used?

In many applications we need user input for the system, It may be a set point for controller, data, preset values, etc. But some applications require multiple keys for entering numbers and other special functions. Such applications are:

  1. Mobile phone
  2. Code lock system
  3. Access control systems
  4. Data entry equipment’s

What is Matrix keypad?

If we need to enter 0 to 9 and A to F, we need 16 buttons, as we know that single key requires single IO line, It means for entering 0 to 9 and A to F we need 16 keys i.e. 16 IO Lines, but our Arduino have limited IO lines, and circuit become complex because of large number of wires, We use Keypad Matrix as shown in figure for 16 Keys It uses only 8 IO Lines.

4x4 Matrix Keypad
4×4 Matrix Keypad

How to detect a key press in Arduino?

Before we start exploring matrix keypad let us see a single key detection algorithm.
Reading a IO line using Arduino command

digitalRead(6)

This command gives result in HIGH or LOW (1 or 0) depending on the voltage at arduino pin 6

Example: As shown in below figure we have a switch connected on arduino pin 6 and when switch is pressed LED on pin 13 should become on.

Arduino Single Key Detection
Single Key Detection

Note:  PIN 13 have on board LED, so it is not shown in circuit

Before we program arduino understand few basic concepts

Where to connect second pin of switch?

Switch second pin in most cases is connected to Ground. And IO pin where we have connected switch is held at high (pulled up).

What is pull up?

Pull up is putting IO pin in high(up) state by means of resistor. One end of resistor is connected to IO pin and second to the +5V. Normally 4.7K to 10K resistor is used.

Why 4.7K to 10K Ohm resistor?

To keep the current in safe limit (normally close to 500uA to 1mAmp)
V=IR
I=V/R=5/4.7K=1mAmp
Use of very high pull up resistor may introduce noise; resistor must have value more than noise current level.

How we can eliminate this resistor?

This resistor can be eliminated by using internal pull up resistor of microcontroller  ATmega328.
Internal pull up can be activated by making that pin high “digitalWrite(Pin,HIGH);” and defining it as input “pinMode(Pin,INPUT);”

Program for single key interface with arduino

Let us move to our 4×4 Matrix keypad interface


4×4 Matrix Keypad Connections with Arduino

Connect Arduino to keypad as shown in figure.

Arduino Connections with keypad
Arduino Connections with keypad

Rows A,B,C,D are connected to arduino pin 9,8,7,6 and columns 1,2,3,4 are connected to arduino pin 5,4,3,2.

We are observing pressed key on serial terminal. So we have not connected any display.

Arduino Code for 4×4 Keypad interfacing

We are using Keypad Library for this code

Results

Press some keys and observe the result on serial terminal. This way you can make digital code lock projects also.

Leave a Reply

Your email address will not be published. Required fields are marked *