1、安装驱动库,可通过GitHub下载,
下载地址https://github.com/Chris--A/Keypad
2、连接电路,键盘的正面看,从右向左依次连接arduino开发板的5-12
3、编写代码
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
//正面从左到右连接
byte rowPins[ROWS] = {5, 6, 7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 10, 11, 12}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
}
void loop() {
char downKey = myKeypad.getKey();
if (downKey){
Serial.println(downKey);
}
}