A matrix keypad could save the number of micro-controller I/O usage. For instant a 4x4 matrix keypad uses only 8 pins of a micro-controller but it yield 16 different key values. Using an 8-bit micro-controller digital I/O port is common for most of programmers.
However when the there is a I/O constraint we need an I/O expansion, for example adding serial interface I/O expansion chip to a micro-controller.These chips need only a few wires of a micro-controller.
![]() |
| 16 Key Membrane Switch Keypad 4X4 3X4 Matrix Keyboard For Arduino Diy Kit |
In this post I will show an example of using the PCF8574AP to make a 4x4 matrix keypad with a character LCD. For more information about using the PCF8574 series please visit these posts,
- ATMega644P TWI PCF8574 I/O Expansion Interfacing
- PIC16F887 PCF8574AP I2C LCD Example using XC8
- PIC16F887 PCF8574AP I2C 4x4 KeyPad using XC8
- PIC16F887 PCF8574 I2C Example using XC8
- A DIY TWI LCD Using PCF8574AP For Arduino
- ATMega32 TWI interfaces to PCF8574A parallel port expansion
- Reading The Digital Input Output From PCF8574 Using PIC12F629
The TWI master micro-controller of the ATMega644P periodically scan for any key press. The result of will display on an on-board 16x2 character LCD.
Source Code:
- /*
- * 10-i2c_pcf8574ap_keypad.c
- *
- * Created: 2/2/2026 9:32:41 PM
- * Author : Admin
- */
- #include <avr/io.h>
- #include <util/delay.h>
- #define F_CPU 16000000UL
- //DIY PCF8574AP LCD Module (A2:A0=0)
- const char pcf8574_w=0x40;
- const char pcf8574_r=0x41;
- //PCF8574 (A2:A0=0)
- const char pcf8574a_w=0x70;
- const char pcf8574a_r=0x71;
- //DIY Arduino PCF8574T LCD Module
- const char pcf8574T_LCD_W=0x4E;
- const char pcf8574T_LCD_R=0x4F;
- void i2cInit(void){
- TWSR|=0x00; //Prescaler Selection Bit
- TWBR=0xF0; //Baud Rate Generator
- TWCR=(1<<TWEN); //Enable The TWI Module
- PORTC|=(1<<0);
- PORTC|=(1<<1);
- }
- void i2cStart(void){
- TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
- while((TWCR&(1<<TWINT))==0);
- }
- void i2cWrite(unsigned char data){
- TWDR=data;
- TWCR=(1<<TWINT)|(1<<TWEN);
- while((TWCR&(1<<TWINT))==0);
- }
- unsigned char i2cRead(char ACK){
- if(ACK==0)
- TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
- else
- TWCR=(1<<TWINT)|(1<<TWEN);
- while((TWCR&(1<<TWINT))==0);
- return TWDR;
- }
- void i2cStop(){
- TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
- _delay_us(10);
- }
- void pcf8574Write(char data){
- i2cStart();
- i2cWrite(pcf8574a_w);
- i2cWrite(data);
- i2cStop();
- }
- char pcf8574Read(void){
- i2cStart();
- i2cWrite(pcf8574a_r);
- char temp=i2cRead(1);
- i2cStop();
- return temp;
- }
- uint8_t key_16[][4]={'1','2','3','A',
- '4','5','6','B',
- '7','8','9','C',
- '*','0','#','D'};
- uint8_t keyScan(void){
- char data=0,temp,key;
- for(uint8_t i=0;i<4;i++){
- data=0xFF;
- data&=~(1<<i);
- pcf8574Write(data);
- _delay_ms(5);
- data=pcf8574Read();
- data&=0xF0;
- if((data&0x10)==0) {temp=key_16[i][0]; break;}
- else if((data&0x20)==0){temp=key_16[i][1]; break;}
- else if((data&0x40)==0){temp=key_16[i][2]; break;}
- else if((data&0x80)==0){temp=key_16[i][3]; break;}
- else temp=0;
- _delay_ms(10);
- }
- return temp;
- }
- int main(void)
- {
- /* Replace with your application code */
- i2cInit();
- lcd_init();
- lcd_text("ATMega644P TWI");
- lcd_xy(1,2);
- lcd_text("PCF8574AP KeyPad");
- _delay_ms(10000);
- lcd_clear();
- char temp,charCount=0,newLine=0,line=1;
- while (1)
- {
- temp=keyScan();
- if(temp!=0){
- lcd_data(temp);
- charCount++;
- _delay_ms(500);
- }
- if(charCount>16){
- newLine=1;
- charCount=0;
- line+=1;
- }
- if(newLine){
- newLine=0;
- if(line==2) lcd_xy(1,2);
- else{
- lcd_xy(1,1);
- lcd_command(0x01);
- _delay_ms(5);
- line=1;
- }
- }
- }
- }
Schematic:
| Schematic and Simulation |
| Simulation Program |
ATMega644P Experiment Board:
This PCB was offered by PCBWay.
I have been using PCBWay for many years now. PCBWay fabricate PCBs at low cost, fast processing time for only 24 hours, and fast delivery time using any carrier options. This double side 10cmx10cm can be fabricate at only 5USD for 5 to 10pcs by PCBWay. It's a standard PCB with silk screen and solder mask.
![]() |
| 10 PCBs for only 5USD |
For different size of PCB we can instantly quote on PCBWay website using a zip PCB Gerber file without account.
![]() |
| PCBWay Instant Quote |
%20Pin%20Out.jpg)


No comments:
Post a Comment