4*4
矩阵键盘
,可以作为简单的控制器输入,能用于复杂的arduino
控制。本文主要介绍如何驱动4*4
矩阵键盘。
材料
4*4
矩阵键盘
Arduino Uno 1
块
8P
线
双排针或排针

这里简单介绍一种矩阵键盘的工作原理,
4*4
矩阵键盘有
8
个引脚,
4
个一组,分别对应行和列,通过按键扫描的方法,对不同行(列)分别输入高低电平,然后读取不同列(行)上的电平,从而知道键盘上的某一按键按下。
例如,当第
1
行输出低电平,其他行输出高电平,分别读取依次列上的状态,如果第
1
列为低,结果为(
1,1
),按键为
1
,如果第
2
列为低,则结果为(
1,2
)按键为
2
安装
4*4
矩阵键盘
4*4
矩阵键盘有一个
8
孔的排母,理论上可以直接插到
0-7
脚上,但
0,1
脚用于串口通信,所以只能选择
2~13
脚,这里选用了
2-9
脚。
首先,选取一个
16 PIN
的双排针,将双排针长的那一排的一面引脚插到键盘排母里


另一面插
8P
线,
8P
线另一头按键盘正面从左到右的顺序,线接
2 PIN
排针,再接
5 PIN
排针,


2 PIN
的排针插到
Arduino
的8,9
脚,5 PIN
的排针插到2~5
脚

总的硬件电路

定义Arduino IO口
byte rowPins[ROWS] = {9, 8, 7, 6}; //连接到行数字小键盘的管脚
byte colPins[COLS] = {5, 4, 3, 2};//连接到列数字小键盘的管脚
示例程序
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
Serial.println(customKey);
}
}

测试可用