0% found this document useful (0 votes)
57 views5 pages

How To Interface Keypad With PIC12F675 - Saeed's Blog

This document provides instructions for interfacing a 4x4 or 4x3 keypad with a PIC12F675 microcontroller. It describes a circuit that uses diodes to generate unique voltages for each key press, which are read by the PIC's ADC. The code samples show initializing the keypad and ADC, reading key presses, and displaying the value on an LCD. Downloads include the code compiled for MPLAB and a Proteus simulation.

Uploaded by

vali29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views5 pages

How To Interface Keypad With PIC12F675 - Saeed's Blog

This document provides instructions for interfacing a 4x4 or 4x3 keypad with a PIC12F675 microcontroller. It describes a circuit that uses diodes to generate unique voltages for each key press, which are read by the PIC's ADC. The code samples show initializing the keypad and ADC, reading key presses, and displaying the value on an LCD. Downloads include the code compiled for MPLAB and a Proteus simulation.

Uploaded by

vali29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

How to interface keypad with PIC12F675 ?

This post provides a simple method to interface any keypad (e-g 4x4 or 4x3
etc) with PIC12F675 microcontroller. The code for PIC12F675 is written in C
language using MPLAB with HI-TECH C compiler. You can download this code
from the 'Downloads' section at the bottom of this page.

In this post, it is assumed that you know,


How to use ADC of PIC12F675 microcontroller. If you don't then please
read this page [http://saeedsolutions.blogspot.com/2012/07/pic12f675-adc-code-
proteus-simulation.html] .
How to interface LCD with PIC12F675 microcontroller. If you don't then
please read this page [http://saeedsolutions.blogspot.com/2012/07/pic12f675-lcd-
interfacing-code-proteus.html] .

The circuit required to interface keypad with PIC12F675 is shown below.

[http://4.bp.blogspot.com/-
CXyn_ntEFkc/T_wZZXu9BnI/AAAAAAAAAro/dV2QbS_PWfM/s1600/PIC12F675+Keyp
ad+CCT.JPG]
Figure 1. Interfacing keypad with PIC12F675 circuit

The result of simulating the code in Proteus is shown above in the figure. This
code is written in such a way that when you press any key from the keypad,
then the value of that key is displayed on the LCD. For example, in the above
figure LCD screen is displaying '1', because this picture was taken after
pressing '1' from the keypad.

In the above circuit, PIC12F675 is running on it's internal 4MHz oscillator. GP0,
GP1 and GP2 pins are used to interface LCD through 4094 IC as explained in
the "PIC12F675 LCD Interfacing Code + Proteus Simulation
[http://saeedsolutions.blogspot.com/2012/07/pic12f675-lcd-interfacing-code-proteus.html]
" post. LCD is used here just to show the pressed key value. Usually 8 pins are
used to interface 4x4 keypad with any microcontroller. 4 pins are used to attach
columns and 4 pins are used to attach rows and scanning algorithm code is
used to check for any pressed key.

But PIC12F675 doesn't have 8 pins. By using some diodes we can generate
different voltage on each pressed key on a single wire (as shown above in the
circuit). And then by using built-in ADC of PIC12F675 we can read this voltage
value to determine pressed key easily. In this way by using only one pin of
PIC12F675 we can successfully interface keypad with it. To interface keypad,
only GP4(AN3) pin is being used in the circuit shown above.

In the above circuit, rows of the keypad have 1N4007 diode attached with them.
If you don't have 1N4007 diode, then any simple 0.7 volt[1] diode can be used
here. RowA has 3 diodes, RowB has 2 diodes, RowC has one diode and RowD
has no diode attached with it. These diodes are pulled-up from one side by a
470 ohm resistor. Column1 wire of keypad is attached with the ground. Column2
is attached with a schottky diode[2]. Column3 is attached with a 2.7v zener
diode. Column4 is attached with a 3.0v zener diode. Using this circuit you can
easily interface keypad with PIC12F675 microcontroller.

Code
The code of the main function is shown below.

[http://3.bp.blogspot.com/-m0uVs-tOZ-
o/T_wjzJlEGUI/AAAAAAAAAr0/ROAZcRdWGsA/s1600/Main+function+keypad.JPG]
Figure 2. Main code for interfacing keypad with PIC12F675

In the main function, keypad is initialized using InitKeypad() function (This


function uses GP4 as keypad input). Then LCD is initialized using InitLCD()
function. After that, in the while(1) loop, GetKey() function is used to read any
pressed key value. GetKey() function is a blocking function, i-e code hangs in it
until the user presses any key. After a key is pressed from the keypad, then LCD
screen is cleared using ClearLCDScreen() function and the value of pressed
key is displayed on the LCD screen using WriteDataToLCD() function and then
again code starts to wait in the GetKey() function for the user to press another
key.

The code used to initialize keypad is shown below.


[http://4.bp.blogspot.com/-43RF2_gwDe8/T_wnI8nH-
ZI/AAAAAAAAAsA/GI1o791JO4Y/s1600/InitKeypad+function+code.JPG]
Figure 3. InitKeypad function code

In the InitKeypad() function, ADC on GP4 pin is initialized by using InitADC()


function which is explained in the "PIC12F675 ADC code + Proteus simulation
[http://saeedsolutions.blogspot.com/2012/07/pic12f675-adc-code-proteus-
simulation.html] " post.

GetKey() function code is shown below.

[http://3.bp.blogspot.com/-XM5T71fJBcQ/T_wwg_2o-
LI/AAAAAAAAAsM/sZ5CWKcB8kc/s1600/GetKey+function+code.JPG]
Figure 4. GetKey function code
In the GetKey() function shown above, ADC value from GP4 pin is read using
GetADCvalue(AN3) function. Then this value is compared with many different
voltage ranges[3], each pressed key generates it's own voltage on the GP4 pin,
so by comparing the voltage, pressed key can be found. After finding the
pressed key, code waits for the user to stop pressing the key to avoid multiple
readings of the same key again and again. In the end of function value of
pressed key is returned.

You can leave your comments in the comment section below.

Notes and References


[1]Here 0.7v means forward biased voltage drop of the diode at a nominal
current of 10mA.
[2]Schottky diode [http://en.wikipedia.org/wiki/Schottky_diode] has 0.3v forward
biased voltage drop at a nominal current of 10mA.
[3]Each pressed key produces some unique voltage on the GP4 pin. I
measured voltage due to each pressed key, then made a range of voltages to
detect pressed key. These voltage ranges coded in the GetKey() function
identify which key is being pressed.

Downloads
Keypad interfacing code using PIC12F675 was compiled in MPLAB v8.85 with
HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10. To
download code and Proteus simulation click here [http://bit.ly/2nqXtyF] .

Further Reading Suggestions


You may also like,
PIC12F675 LCD Interfacing Code + Proteus Simulation
[http://saeedsolutions.blogspot.com/2012/07/pic12f675-lcd-interfacing-code-
proteus.html]
PIC12F675 based digital clock using display (Code + Proteus simulation)
[http://saeedsolutions.blogspot.com/2012/07/pic12f675-based-digital-clock-using-
lcd.html]
PIC12F675 ADC code + Proteus simulation
[http://saeedsolutions.blogspot.com/2012/07/pic12f675-adc-code-proteus-
simulation.html]
PIC12F675 PWM Code + Proteus Simulation
[http://saeedsolutions.blogspot.com/2012/07/pic12f675-pwm-code-proteus-
simulation.html]

Posted 10th July 2012 by Saeed


Labels: PIC micro-controller Projects, PIC12F675 Projects

0 Add a comment
Please upgrade to a supported browser to get a reCAPTCHA challen
Enter Comment
Why is this happening to me?

You might also like