Electronics, Photography, Gadgets

Interfacing The PICAXE M-Series Microcontroller With A 4×3 Matrix Keyboard

After my son discovered that disclosing your PIN for a hard coded alarm is not such a great idea, we have embarked on moving our alarm system to a micro-controller which can then be programmed to allow the user to change the PIN at run-time. Our microcontroller of choice is the PICAXE which is available here. For this project, I chose the PICAXE 20M, but this was a bad idea as the memory on this device is much too small for this kind of project as I will show later. In addition to the 20M, I also ordered the AXE029 breadboard adapter, the AXE033 Serial LCD module and a 4×3 matrix keypad for data entry. I also should mention that the service from tech supplied in the U.K. is very good – I live in Germany and the package was dispatched the same day and arrived two days later.


4x3 Matrix Keypad


Our first job in this project was to interface the keypad and LCD module to the microcontroller. In this post I will describe how the keypad can be interfaced to the 20M and in a later post I will describe the LCD module and post the code for the first version of the alarm. The pins on the keypad are arranged as follows (reading left to right and looking at the keys): (1) Not Connected (2) Column 2 (3) Row 1 (4) Column 1 (5) Row 4 (6) Column 3 (7) Row 3 (8) Row 2 (9) Not Connected. The row pins (R1 – R4) should be connected to output pins of the 20M as follows: (1) Output0 -> R1 (2) Output1 -> R2 (3) Output2 -> R3 (4) Output3 -> R4. The column pins of the keypad (C1 – C3) should be connected to the input pins of the 20M as follows:  (1) C1 -> input0 (2) C2 -> input1 (3) C3 -> input2. Don’t forget that the input pins 0 – 2 on the PICAXE have to be pulled down to 0v using 10k resistors so that the pins aren’s floating when no input is present from the keypad. The circuit described above can be seen in the following schematic.


PICAXE 20M Keypad Interface Schematic


The next step is to program the microcontroller so that it scans the key pad. To do this, simply loop through the rows one at a time (output0 – output3) and test which column is set. Below is the code I developed to do this. In a nutshell, the main routine loops across all of the row pins and sets them high one by one. The keyscan routine is called for each row scan and it checks if a column pin is high – if true, it calculates the key number using the simple calculation row * 3 + column and waits until the key is released. The displayKey routine displays the number on an LCD module.

symbol row = b1
symbol key = b2
pause 500
key = 0
for row = 0 to 3
high row
gosub keyscan
low row
if key > 0 then gosub displayKey
next row
goto main
if pin0 = 1 then
key = row * 3 + 1
do loop while pin0 = 1
elseif pin1 = 1 then
key = row * 3 + 2
do loop while pin1 = 1
elseif pin2 = 1 then
key = row * 3 + 3
do loop while pin2 = 1
serout 7, N2400, (254,1,#key)

Update: This post described how to interface an alternative 4×3 Keypad with a different pin-out configuration.


  1. Dibyendu Sengupta

    December 6, 2010 at 8:50 am

    I want purchase matric keypad 4×3,please send the purhcase Procedure.

Leave a Reply to Alan Southall Cancel reply

Your email address will not be published.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2018 MOV AX, BLOG

Theme by Anders NorenUp ↑