Keypad 4×5 for microcontrollers v. 1.1 using PIC16F877

Summary of Keypad 4×5 for microcontrollers v. 1.1 using PIC16F877


This article describes a simple, scalable 4x5 matrix keypad design intended for PIC16F877 microcontrollers (5V), but usable with other PICs or microcontrollers and with logic/analog circuits after resistor adjustment. It explains wiring: rows pulled up to Vdd, columns driven by MCU through protection resistors, and reading method by driving one column low and sampling row inputs with debounce/settling delays. The design supports 1–4 rows and 1–5 columns and notes typical interrupt/timer-driven scanning code.

Parts used in the 4x5 keypad:

  • Normally open 4-pin keys (matrix keys)
  • Resistors R1 to R4 (row pull-up resistors)
  • Protection resistors R5 to R8 (row to MCU I/O)
  • 220 ohm protection resistors R9 to R13 (column drive resistors)
  • Connector JP1 (Vdd positive supply)
  • Connector JP2 (rows to microcontroller)
  • Connector JP3 (columns from microcontroller)
  • Microcontroller (example PIC16F877)
  • Wiring/traces for matrix rows and columns

What’s this?

The circuit here described is a simple 4×5 keypad that can be used in particular with the PIC 16F877 microcontroller, for which it has been designed considering a supply of 5V, 16F877 I/O pins leakage current, the voltage level recognized as a high or low state while in TTL or ST mode, etc.. Since this circuit is quite simple, and the resistors’ value not critical, it should be suitable for other PICs and other microcontrollers, too. This keypad can also be used with logic and analogue crcuits; in such a case, it could be necessary to modify resistors’ value. We will refer, in this page, to the use with a PIC 16F877. The keys used are normally open, with four pins (see image below); when not pressed, only pins on the same side are connected together (left or right side in the image below); when the key is pressed, the four pins are connected all together.

Keypad

A characteristic of this circuit is the possibility of scaling: it can in fact be realized with 1 to 4 rows and 1 to 5 columns (if you still have free rows or columns, you can add later other keys and other resistors); you can build, as an example, a 12 keys telephonic keypad, a simple 4 keys keypad for the 4 directions, a keypad to play tris 🙂 … in one word, what you like.

The figure below shows how it works (classic).

Only the positive supply is connected to the circuit (connector JP1); each row of the matrix keyboard is connected to Vdd by its own pull-up (R1 to R4), and, through a protection resistor (R5 to R8), to the microcontroller I/O pin used to read the state of that row (connector JP2). Columns are driven by PIC (connector JP3), still through 220 Ohm protection resistors (R9 to R13).

Driving columns and reading rows’ state

Referring to this keypad as an interface to a microcontroller (not necessarily a 16F877), the reading of keys’ status is related to the matrix structure. As previously said, each key (or switch) has four pins, connected two by two; in the top view of the circuit (above here), the upper pin on the left is connected to a row, the upper one on the right to a column. The lower pin on the left is not used, the lower one on the right simply carries the column signal to the key of the next row. Let’s consider a certain row: when no key connected to it is being pressed, all the row pins of that row are kept in the high state by the row pull-up; when a key is pressed, the column pin is connected to the row pin; if, in that moment, that column is in high impedance, it is driven at the row state; if it is instead driven low by the microcontroller, also the row is driven at the column state (low state). The microcontroller’s ports connected to the rows must always be set as inputs, or, in general, must be kept in high impedance; in general, also the columns must be in high impedance. To read the state of the keys of a column, it is needed to drive low that column (only one) and to wait a few milliseconds both for the row’s signal settling and to avoid the key’s rebound; at this moment the column’s state can be read on ports connected to rows. A suitable waiting time is five-ten milliseconds. As it is known which is the driven column, reading the rows’ state it is possible to know the keys’ status on that column. At this moment the pin driving that column must be put again in a high impedance state, and, possibly after a few milliseconds, a new column can be driven low and so on. Of course, it is not necessary to waist all this time in waiting: when waiting for settling, and in general among different readings, the microcontroller can execute (thousand of) other instructions. A typical way of reading keys’ status could be using interrupts. As an example, in the PICs the timer could be set to generate an interrupt every a few tens milliseconds. A variable coding the action to take (action), and a variable keeping track of the column to read (coltoread) could be used. The first time that the interrupt routine is executed, it sees those two variables at zero (they should have been set to zero, of course): in such a case, the action to take is to drive low the column 0 and to increment the variable coding the action to take. When the next interrupt occurs, the interrupt driver sees such a variable at 1, so reads the rows’ status for the column 0, puts it again in high impedance, increments the column number, set to 0 the variable coding the action to take. Of course, when the column number that has just been read is the highest for your keypad, the variable counting it (coltoread) must be set to 0. In a “pseudo” C++:

 

For more detail: Keypad 4×5 for microcontrollers v. 1.1 using PIC16F877

Quick Solutions to Questions related to the 4x5 keypad:

  • Can this keypad be used with microcontrollers other than PIC16F877?
    Yes, the circuit is simple and resistors' values are not critical, so it should be suitable for other PICs and other microcontrollers too.
  • What supply voltage is this keypad designed for?
    It has been designed considering a supply of 5V.
  • How are the rows of the matrix connected?
    Each row is connected to Vdd by its own pull-up resistor (R1 to R4) and through protection resistors (R5 to R8) to the microcontroller I/O pins.
  • How are the columns driven?
    Columns are driven by the microcontroller through 220 ohm protection resistors (R9 to R13).
  • What pin states must microcontroller ports connected to rows have?
    The ports connected to the rows must always be set as inputs or kept in high impedance.
  • How is a key press detected?
    Drive one column low and read the row inputs after a few milliseconds; a pressed key connects column to row causing the row to read low.
  • How long should you wait for signal settling and debounce?
    A suitable waiting time is five to ten milliseconds.
  • Is the keypad scalable?
    Yes, it can be realized with 1 to 4 rows and 1 to 5 columns and expanded later if free rows or columns remain.
  • Can the keypad be used with logic and analog circuits?
    Yes, but it may be necessary to modify resistor values for such use.
  • What is a typical scanning method suggested?
    Use a timer to generate interrupts every few tens of milliseconds; drive one column low, read rows in the interrupt routine, then release and proceed to the next column.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter