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);
Code: Select all
HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, ROWMARKER, PIN_R1, PIN_R2, PIN_R3, PIN_R4);
Code: Select all
HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, ROWMARKER, PIN_R1, PIN_R2, PIN_R3, PIN_R4);
Code: Select all
HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, ROWMARKER, PIN_R1);
Code: Select all
HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, PIN_C5, PIN_C6, ROWMARKER, PIN_R1);
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();
Code: Select all
Keypad.New_Key();
Code: Select all
Keypad.Read();
Example Sketch:
- /* FILE: HCMatrixKeypad_Example1.ino
- DATE: 09/08/22
- VERSION: 1.0
- AUTHOR: Andrew Davies
- Version 0.1 14/05/14: Original version
- 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.
- This is an example of how to use the the HCMatrixkepday library that has been
- written two work with most types of matrix keypads including keypads sold via our
- own website.
- The current version of library supports the following arrangement (columns x rows)
- of keypads:
- 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)
- KEYPAD PINOUTS FOR ABOVE KEYPADS:
- PIN....4x5.....4x4.....3x4.....4x1.....6x1
- 1......R1......C4......C3......C3......C6
- 2......R2......C3......C2......C4......C5
- 3......R3......C2......C1......C1......C4
- 4......R4......C1......R4......C2......C3
- 5......R5......R4......R3......R1......C2
- 6......C4......R3......R2..............C1
- 7......C3......R2......R1..............R1
- 8......C2......R1........................
- 9......C1................................
- You may copy, alter and reuse this code in any way you like, but please leave
- reference to HobbyComponents.com in your comments if you redistribute this code.
- This software may not be used directly for the purpose of selling products that
- directly compete with Hobby Components Ltd's own range of products.
- THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
- EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
- HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
- INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
- REASON WHATSOEVER.
- */
- /* Include the HCMatrixkeypad library */
- #include <HCMatrixKeypad.h>
- /* Sets how many times the same key must be scanned before it is accepted as
- pressed (settled). If you get multiple key presses then increase this value
- (max 255)*/
- #define DEBOUNCE 10
- #define PIN_C1 2 /* DIO for keypad column 1 */
- #define PIN_C2 3 /* DIO for keypad column 2 */
- #define PIN_C3 4 /* DIO for keypad column 3 */
- #define PIN_C4 5 /* DIO for keypad column 4. Delete if there is no column 4! */
- #define PIN_C5 6 /* DIO for keypad column 5. Delete if there is no column 5! */
- #define PIN_C6 7 /* DIO for keypad column 6. Delete if there is no column 6! */
- #define PIN_R1 12 /* DIO for keypad row 1 */
- #define PIN_R2 11 /* DIO for keypad row 2. Delete if there is no row 2! */
- #define PIN_R3 10 /* DIO for keypad row 3. Delete if there is no row 3! */
- #define PIN_R4 9 /* DIO for keypad row 4. Delete if there is no row 4! */
- #define PIN_R5 8 /* DIO for keypad row 5. Delete if there is no row 5! */
- 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 */
- //HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, ROWMARKER, PIN_R1, PIN_R2, PIN_R3, PIN_R4); /* Uncomment for 4x4 keypad */
- //HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, ROWMARKER, PIN_R1, PIN_R2, PIN_R3, PIN_R4); /* Uncomment for 3x4 keypad */
- //HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, ROWMARKER, PIN_R1); /* Uncomment for 4x1 keypad */
- //HCMatrixKeypad Keypad(DEBOUNCE, PIN_C1, PIN_C2, PIN_C3, PIN_C4, PIN_C5, PIN_C6, ROWMARKER, PIN_R1); /* Uncomment for 6x1 keypad */
- void setup()
- {
- Serial.begin(9600);
- }
- /* Main program */
- void loop()
- {
- /* Scans the keypad once. This line needs to be run repeatedly */
- Keypad.Scan();
- /* Has a new key been pressed */
- if(Keypad.New_Key())
- {
- /* If so the send the key to the serial port */
- Serial.print("Key Pressed: Row ");
- Serial.print(Keypad.Read() / 10); /* 10's column is the keypad row number */
- Serial.print(" Col ");
- Serial.println(Keypad.Read() % 10); /* 1's column is the keypad column number */
- }
- }
Library:
Latest (V.0):
Old V0.1:
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.