Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to interface keypad with AVR microcontroller (ATmega16)- (Part 11/46)

By Ashutosh Bhatt January 21, 2011

Keypad is most widely used input device to provide input from the outside world to the microcontroller. The keypad makes an application more users interactive.  The concept of interfacing a keypad with the ATmega16 is similar to interfacing it with any other microcontroller. The article of Interfacing keypad with 8051 can be referred for detailed description of the methodology used here. This article explains the interfacing of a 4x3 keypad with AVR microcontroller (ATmega16) and displaying the output on a LCD.
 

 

The algorithm and detailed explanation for keypad interfacing is given in above mentioned article. The brief steps to interface the keypad with AVR are written below:
1.                  Configure the row pins or column pins.
2.                  Make all output pins to low and input pins to high.
3.                  Keep monitoring the port value, where the key pad is connected.

while(1)
{
PORTD=0xF0; //set all the input to one
value=PIND; //get the PORTD value in variable “value”
if(value!=0xf0) //if any key is pressed value changed
{
check1();
check2();
check3();
check4();
}
}
4.                  If there is any change in port value, make one of the output pin of port to zero and rest all high.

void check1(void)
{
//DDRD = 0xf0;
pad =0b11111110;
//pad &= (0<<r1);
_delay_us(10);
if(bit_is_clear(PIND,c1))
LCD_write(‘1’);
else if(bit_is_clear(PIND,c2))
LCD_write(‘2’);
else if(bit_is_clear(PIND,c3))
LCD_write(‘3’);
}
5.                  If any of input pin found zero, write the particular pin data to LCD, else continue with the step (4).

 

Project Source Code

###


// Program to get input from keypad and display it on LCD.
#include
#include
 
#define pad PORTD
#define r1 PD0
#define r2 PD1
#define r3 PD2
#define r4 PD3
 
#define c1 PD4
#define c2 PD5
#define c3 PD6
 
void check1(void);
void check2(void);  
void check3(void);
void check4(void);
 
#define LCD_DATA PORTA //LCD data port
 
#define ctrl PORTB
#define en PB2 //enable signal
#define rw PB1 //read/write signal
#define rs PB0 //resister select signal
 
void LCD_cmd(unsigned char cmd);
void init_LCD(void);
void LCD_write(unsigned char data);
 
unsigned int press;
 
int main()
{
unsigned char value; 
DDRA=0xff; //LCD_DATA port as output port
DDRB=0x07; //signal as out put
DDRD=0x0F;
pad=0xf0;
init_LCD(); //initialization of LCD
LCD_write_string("press a key");
LCD_cmd(0xc0);
 
 
while(1)
{
PORTD=0xF0; //set all the input to one
value=PIND; //get the PORTD value in variable “value”
if(value!=0xf0) //if any key is pressed value changed
{
check1();
check2();
check3();
check4();
}
}
return 0;
}
 
void check1(void)
{
//DDRD = 0xf0;
pad =0b11111110;
//pad &= (0<
_delay_us(10);
if(bit_is_clear(PIND,c1))
LCD_write('1');
else if(bit_is_clear(PIND,c2))
LCD_write('2');
else if(bit_is_clear(PIND,c3))
LCD_write('3');
}
 
 
void check2(void)
{
pad=0b11111101;
/pad &= (0<
_delay_us(10);
if(bit_is_clear(PIND,c1))
LCD_write('4');
else if(bit_is_clear(PIND,c2))
LCD_write('5');
else if(bit_is_clear(PIND,c3))
LCD_write('6');
}
 
void check3(void)
{
pad=0b11111011;
//pad &= (0<
_delay_us(10);
if(bit_is_clear(PIND,c1))
LCD_write('7');
else if(bit_is_clear(PIND,c2))
LCD_write('8');
else if(bit_is_clear(PIND,c3))
LCD_write('9');
}
 
void check4(void)
{
pad =0b11110111;
//pad &= (0<
_delay_us(10);
if(bit_is_clear(PIND,c1))
LCD_write('#');
else if(bit_is_clear(PIND,c2))
LCD_write('0');
else if(bit_is_clear(PIND,c3))
LCD_write('*');
}
 
 
 
void init_LCD(void)
{
 
LCD_cmd(0x38); //initializtion of 16X2 LCD in 8bit mode
_delay_ms(1);
 
LCD_cmd(0x01); //clear LCD
_delay_ms(1);
 
LCD_cmd(0x0E); //cursor ON
_delay_ms(1);
 
LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position
_delay_ms(1);
return;
}
 
 
void LCD_cmd(unsigned char cmd)
{
LCD_DATA=cmd;
ctrl =(0< // making RS and RW as LOW and EN as HIGH
_delay_ms(1);
ctrl =(0< // making RS, RW , LOW and EN as LOW
_delay_ms(50);
return;
}
 
 
void LCD_write(unsigned char data)
{
LCD_DATA= data;
ctrl = (1< // making RW as LOW and RS, EN as HIGH
_delay_ms(1);
ctrl = (1< // making EN and RW as LOW and RS HIGH
_delay_ms(50); // give a 10 milli second delay to get thigs executed
return ;
}
 
void LCD_write_string(unsigned char *str) //take address vaue of the string in pionter *str
{
int i=0;
while(str[i]!='�') // loop will go on till the NULL charaters is soon in string 
{
LCD_write(str[i]); // sending data on CD byte by byte
i++;
}
return;
}
 

###

 


Circuit Diagrams

Circuit-Diagram-of-How-to-interface-keypad-with-AVR-microcontroller-ATmega16

Project Components

  • ATmega16
  • LCD
  • Preset

Project Video


Filed Under: AVR, Electronic Projects
Tagged With: atmega16, avr, keypad, microcontroller
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Voltage mode pushpull is a nonsense SMPS?
  • Input impedance matching network
  • High Side current sensing
  • The comparison of different Tcl script checkers
  • Reducing "shoot-through" in offline Full Bridge SMPS?

RSS Electro-Tech-Online.com Discussions

  • Is AI making embedded software developers more productive?
  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe