Single-Wire 4×4 Matrix Keypad Using AVR

Nitin Kisan Khaire has many years of experience in product development and software validation. He has worked for engraving machine development using CPLD, FPGA and microcontrollers


This tutorial demonstrates a different way of interfacing a 4×4 matrix keypad with ATmega32 AVR microcontroller. Keypad is one of the most important input devices used in electronics projects. It is one of the easiest ways to give commands or instructions to an electronic system. But when designing a traditional matrix keypad, you require more number of input/output (I/O) pins. For example, if you are developing a 4×4 keypad of 16 keys, you require eight digital I/O lines of the microcontroller device.

This project uses only a single analogue pin to achieve the same goal, so the remaining pins are available for other purposes.

Fig. 1 shows the block diagram of the single-wire keypad with liquid-crystal display (LCD). The system consists of a 4×4 keypad that inputs data to the microcontroller through a single wire, ATmega32 microcontroller for processing the received signal, power supply section and a 16×2 LCD for displaying the keys.

Fig. 1: Block diagram of single-wire 4×4 keypad

Circuit and working

Complete circuit diagram of the single-wire 4×4 matrix keypad using AVR is shown in Fig. 2. It is built around step-down transformer X1, 16 pushbutton switches (S1 through S16), 16 resistors (R1 through R16), a 5V regulator 7805 (IC1), an ATmega32 microcontroller (IC2), an LCD (LCD1) and a few other components. It has three sections: power supply, display and keypad.

Fig. 2: Circuit diagram of single-wire 4×4 keypad

Download original Image

Power supply

The 230V AC mains is stepped down by a 230V AC primary to 9V, 250mA secondary transformer (X1), rectified by a bridge rectifier module (BR1) and filtered by capacitors C1 and C2. The voltage is regulated by 7805 regulator IC. LED1 glows to indicate the presence of power in the circuit.

The regulated 5V DC powers the microcontroller and LCD1. You can also use a 9V AC mains adaptor to drive the circuit.

Switch S17 is used to power the circuit. Connect switch S17 to 9V if you do not use transformer X1.


The project uses a 16×2, Hitachi HD44780-controlled LCD module (LCD1) for display. Typically, a 16-character, 2-line LCD module has eight data lines (D0 through D7) for data transfer. In this project only four data lines (D4 through D7) are used for data transfer. Data lines D0 through D3 are disabled. Data transfer between HD44780 and the microcontroller completes after the 4-bit data is transferred twice. Port-D pins PD0 through PD3 of the microcontroller (IC2) are connected to data lines D4 through D7 of LCD1. LCD1 control lines, i.e., read/write (R/W), register-select (RS) and enable (EN), are connected to PD5, PD4 and PD6 of IC2, respectively.


A total of 16 pushbutton switches are used and arranged in a 4×4 keypad matrix on the PCB. One terminal of each pushbutton is connected to common ground, while the other terminal is connected to resistors R1 through R16 in series. Refer Fig. 2 and Table I for details.

In this project, we use the voltage divider circuit principle for identification of each key/switch pressed. Here, 1-kilo-ohm resistor R19 is fixed and connected to 5V power supply as shown in Fig. 2. When you press any pushbutton switch, the corresponding resistor, which is connected in series with the pushbutton, comes in picture to form a voltage divider circuit. As a result, you get a variable voltage at pin 39 (PA1) of IC2, which is analogue-to-digital converter channel-1 pin or ADC1 pin of the microcontroller.
Voltage obtained at ADC1 pin is given by the relationship:


R19 has a constant value of one kilo-ohm and Vin is also a constant value of 5V. Ri value is decided when you press any of the pushbutton keys from the keypad. It can be any of the resistors R1 through whose values are listed in Table I.

Software program

To work with Atmel AVR microcontroller using ‘C’ programming language, you need two software tools: AVR Studio and WinAVR. Both of these tools are freely available for download from the Internet. The program for the microcontroller is written in ‘C’ language using AVR Studio 4.

‘C’ program functions

Functions are written for the ADC module, LCD1 and keypad using ‘C’ program. Working is very simple as all pushbuttons or switches are connected to only one pin of the microcontroller (ADC1). The program reads the input switches through ADC1 pin periodically. After decoding the ADC value, the corresponding switch number/value is displayed on LCD1. Hysteresis approach is used for each pushbutton to analyse the ADC value, and based on that value, the program detects/decodes the pushbutton being pressed. The decoding is done using following functions:

Display_Key_on_LCD (unsigned long ADC_

// This function prints the key pressed on the LCD after decoding the ADC value


// This function executes every 100 msec


// This function is used for acquiring ADC value on ADC1 pin


Please enter your comment!
Please enter your name here