HCMatrixkeypad - Matrix keypad library

Useful guides, libraries, and example sketches to support our Arduino based products.
admin
Site Admin
Posts: 866
Joined: Sun Aug 05, 2012 4:02 pm

HCMatrixkeypad - Matrix keypad library

Post by admin » Thu May 15, 2014 10:22 am

Description:

This library has been written to be used with our range of matrix keypads. However the library should also work fine with any matrix keypad that matches one of the key arrangements the library supports. The library currently supports the following keypads sold via our web store:

4 x 5 (see SKU: HCPROJ0003)
4 x 4 (see SKU: HCPROJ0001 & HCPROJ0002)
3 x 4 (see SKU: HCPROJ0004)
6 x 1 (see SKU: HCPROJ0007)
4 x 1 (see SKU: HCPROJ0005)

You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area

On Windows:
My Documents\Arduino\libraries\
My Documents\Arduino\libraries\
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/
Documents/Arduino/libraries/
Documents/Arduino/libraries/
or similarly for Linux.

To use the library just include the HCMatrixkeypad.h header file and create one of the following instances of the HCMatrixkeypad library. E.g:

For a keypad with 4 columns and 5 rows:

Code: Select all

HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, ROWMARKER, PIN_R1, PIN_R2, PIN_R3, PIN_R4, PIN_R5);
For a keypad with 4 columns and 4 rows:

Code: Select all

HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, ROWMARKER, PIN_R1, PIN_R2, PIN_R3, PIN_R4);
For a keypad with 3 columns and 4 rows:

Code: Select all

HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, ROWMARKER, PIN_R1, PIN_R2, PIN_R3, PIN_R4);
For a keypad with 4 columns and 1 row:

Code: Select all

HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, ROWMARKER, PIN_R1);
For a keypad with 6 columns and 1 row:

Code: Select all

HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, PIN_C5, PIN_C6,  ROWMARKER, PIN_R1);
Where:
DEBOUNCE is a decimal value from 1 to 255 which determines the amount of averaging used before a keypress is confirmed.
PIN_C1, PIN_C2, PIN_C3, PIN_C4, PIN_C5, PIN_C6 are the DIO pin numbers used for the keypads columns.
PIN_R1, PIN_R2, PIN_R3, PIN_R4, PIN_R5 are the DIO pin numbers used for the keypads rows.
ROWMARKER is predefined by the library and is used to separate the column DIO parameters from the row DIO parameters at compile time. This should be left as is.

The following functions are available with this library:

Code: Select all

Keypad.Scan();
Makes one complete scan of the keypad. This must be continuously run in a loop.

Code: Select all

Keypad.New_Key();
Used to test if a new key has been pressed. Returns a boolean true if key had been pressed, or false if no new key has been pressed. If a key has been pressed no further scanning of the keypad will be made until this function is executed.

Code: Select all

Keypad.Read();
Returns a decimal value of type Byte which indicates the column/row of the last key pressed (0 if no key is currently pressed) where the 10's value indicates the row number and the 1's value indicates the column number. I.e: a value of 53 would indicate that the last key pressed was on Row 5, column 3.

Example Sketch:
  1. /* FILE:    HCMatrixKeypad_Example1.ino
  2.    DATE:    09/08/22
  3.    VERSION: 1.0
  4.    AUTHOR:  Andrew Davies
  5.  
  6. Version 0.1 14/05/14: Original version
  7. Version 1.0 09/08/22: Pin parameter names from changed from Cx and Rx to pinCx and PinRx to fix redefinition error with LGTF boards.
  8.  
  9. This is an example of how to use the the HCMatrixkepday library that has been
  10. written two work with most types of matrix keypads including keypads sold via our
  11. own website.
  12.  
  13. The current version of library supports the following arrangement (columns x rows)
  14. of keypads:
  15.  
  16. 4 x 5 (see SKU: HCPROJ0003)
  17. 4 x 4 (see SKU: HCPROJ0001 & HCPROJ0002)
  18. 3 x 4 (see SKU: HCPROJ0004)
  19. 6 x 1 (see SKU: HCPROJ0007)
  20. 4 x 1 (see SKU: HCPROJ0005)
  21.  
  22.  
  23. KEYPAD PINOUTS FOR ABOVE KEYPADS:
  24.  
  25. PIN....4x5.....4x4.....3x4.....4x1.....6x1
  26. 1......R1......C4......C3......C3......C6
  27. 2......R2......C3......C2......C4......C5
  28. 3......R3......C2......C1......C1......C4
  29. 4......R4......C1......R4......C2......C3
  30. 5......R5......R4......R3......R1......C2
  31. 6......C4......R3......R2..............C1
  32. 7......C3......R2......R1..............R1
  33. 8......C2......R1........................
  34. 9......C1................................
  35.  
  36.  
  37. You may copy, alter and reuse this code in any way you like, but please leave
  38. reference to HobbyComponents.com in your comments if you redistribute this code.
  39. This software may not be used directly for the purpose of selling products that
  40. directly compete with Hobby Components Ltd's own range of products.
  41.  
  42. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  43. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  44. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  45. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  46. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  47. REASON WHATSOEVER.
  48. */
  49.  
  50. /* Include the HCMatrixkeypad library */
  51. #include <HCMatrixKeypad.h>
  52.  
  53. /* Sets how many times the same key must be scanned before it is accepted as
  54.    pressed (settled). If you get multiple key presses then increase this value
  55.    (max 255)*/
  56. #define DEBOUNCE 10
  57.  
  58. #define PIN_C1 2  /* DIO for keypad column 1 */
  59. #define PIN_C2 3  /* DIO for keypad column 2 */
  60. #define PIN_C3 4  /* DIO for keypad column 3 */
  61. #define PIN_C4 5  /* DIO for keypad column 4. Delete if there is no column 4! */
  62. #define PIN_C5 6  /* DIO for keypad column 5. Delete if there is no column 5! */
  63. #define PIN_C6 7  /* DIO for keypad column 6. Delete if there is no column 6! */
  64.  
  65. #define PIN_R1 12 /* DIO for keypad row 1 */
  66. #define PIN_R2 11 /* DIO for keypad row 2. Delete if there is no row 2! */
  67. #define PIN_R3 10 /* DIO for keypad row 3. Delete if there is no row 3! */
  68. #define PIN_R4 9  /* DIO for keypad row 4. Delete if there is no row 4! */
  69. #define PIN_R5 8  /* DIO for keypad row 5. Delete if there is no row 5! */
  70.  
  71. HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, ROWMARKER, PIN_R1, PIN_R2, PIN_R3, PIN_R4, PIN_R5); /* Uncomment for 4x5 keypad */
  72. //HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, ROWMARKER, PIN_R1, PIN_R2, PIN_R3, PIN_R4); /* Uncomment for 4x4 keypad */
  73. //HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, ROWMARKER, PIN_R1, PIN_R2, PIN_R3, PIN_R4); /* Uncomment for 3x4 keypad */
  74. //HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, ROWMARKER, PIN_R1); /* Uncomment for 4x1 keypad */
  75. //HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, PIN_C5, PIN_C6, ROWMARKER, PIN_R1); /* Uncomment for 6x1 keypad */
  76.  
  77.  
  78. void setup()
  79. {
  80.     Serial.begin(9600);
  81. }
  82.  
  83. /* Main program */
  84. void loop()
  85. {
  86.   /* Scans the keypad once. This line needs to be run repeatedly */
  87.   Keypad.Scan();
  88.  
  89.   /* Has a new key been pressed  */
  90.   if(Keypad.New_Key())
  91.   {
  92.     /* If so the send the key to the serial port */
  93.     Serial.print("Key Pressed: Row ");
  94.     Serial.print(Keypad.Read() / 10); /* 10's column is the keypad row number */
  95.     Serial.print(" Col ");
  96.     Serial.println(Keypad.Read() % 10); /* 1's column is the keypad column number */
  97.   }
  98.  
  99. }



Library:

Latest (V.0):
HCMatrixKeypad_V1.zip

Old V0.1:
HCMatrixKeypad.zip

Libraries, example code, and diagrams are provided as an additional free service by Hobby Components and are not sold as part of this product. We do no provide any guarantees or warranties as to their accuracy or fitness for purpose.

Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.
You do not have the required permissions to view the files attached to this post.

cc10978
Posts: 2
Joined: Fri Oct 14, 2016 5:06 pm

Re: HCMatrixkeypad - Matrix keypad library

Post by cc10978 » Fri Oct 14, 2016 5:22 pm

Hi admin, you specify the pinout for the I/O pins in the example sketch, but not the power pins. Which is the power and which is the earth (specifically for HCPROJ0002) and are the pins numbered for looking at it from the button side or the back? Thanks.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMatrixkeypad - Matrix keypad library

Post by andrew » Sat Oct 15, 2016 9:25 am

Most types of matrix keypads, including the one you have don't require a power supply and so there are no power pins.

For your keypad (HCPROJ0002), if you orientate it so that the buttons are facing you with the connector at the bottom of the keypad the pinout from left to right is as follows:


1) Not connected
2) Col 1
3) Col 2
4) Col 3
5) Col 4
6) Row 1
7) Row 2
8) Row 3
9) Row 4
10) Not connected
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

cc10978
Posts: 2
Joined: Fri Oct 14, 2016 5:06 pm

Re: HCMatrixkeypad - Matrix keypad library

Post by cc10978 » Sat Oct 15, 2016 6:54 pm

OK, Thanks! :)

tterry
Posts: 1
Joined: Wed Oct 19, 2016 12:46 am

Re: HCMatrixkeypad - Matrix keypad library

Post by tterry » Wed Oct 19, 2016 12:51 am

thank you.

kosspeed
Posts: 1
Joined: Tue Oct 18, 2016 5:46 pm

Re: HCMatrixkeypad - Matrix keypad library

Post by kosspeed » Tue Nov 29, 2016 1:10 pm

thank you

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library

Post by RetroBoy » Mon Aug 08, 2022 5:30 pm

Hi,

Is it easy enough to link a Keypad press to the (HCARDU0018) 8-Channel 5V Relay Module, I need to know whether if 1 was the last key pressed then key 4 gets pressed, key 1's action needs to be reversed ( this is Model Railway Point Control ). The other point is that each key needs to control 2 relays, so if 'a' is on - 'b' is off and vice-versa.

Regards S.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMatrixkeypad - Matrix keypad library

Post by andrew » Tue Aug 09, 2022 8:24 am

I need to know whether if 1 was the last key pressed then key 4 gets pressed, key 1's action needs to be reversed

A pressed key will continuously be returned by the Read() function until another button is pressed and has been scanned in by running the Scan() function.

To check if the key returned by the Read() function is still valid just make sure you run the Scan() & New_Key() function first (see example sketch). That way if the first key is no longer pressed and a new key is pressed the Read() will be updated with the new key.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library

Post by RetroBoy » Tue Aug 09, 2022 10:37 am

Good Morning Andrew,

Thanks for the quick reply, point noted.

Regards S.

RetroBoy
Posts: 73
Joined: Sat Feb 26, 2022 11:29 am
Location: U.K.

Re: HCMatrixkeypad - Matrix keypad library using HCPROJ0004 3x4 12 Key Matrix

Post by RetroBoy » Thu Aug 18, 2022 11:09 am

Good Morning,

Is there a Simple Way for this combination to be able to Output Values from 00 - 99, instead of having to store and add within the sketch code ( Keypad.Read(); Store; Keypad.Read(); Store )?

Regards S.

Post Reply

Return to “Arduino”