将I2C与4×4矩阵键盘配合使用

参考:https://www.cnblogs.com/eagler8/p/15179162.html
原文地址:https://www.makeriot2020.com/index.php/2020/10/05/using-i2c-with-a-4x4-matrix-keypad/

将I2C与4×4矩阵键盘配合使用

在这里插入图片描述

使用矩阵键盘是将多个控制按钮添加到项目的一种非常简单的方法,无论是输入密码还是控制不同的设备。让我们看一下电路
在这里插入图片描述4×4矩阵键盘的电路图

如我们所见,要构建4×4矩阵键盘,我们将需要16个瞬时开关。按照上图将他们连接在一起。然后,您可以将其与您最喜欢的微控制器进行接口以读取所按下的键……

但是需要8个引脚来控制此键盘……?不,我们只需要2个引脚。也就是说,如果我们使用这些PCF8574 I2C IO端口扩展器模块之一。它们更可靠,也很便宜。

让我们看看如何将键盘连接到I2C模块
在这里插入图片描述
带PCF8574 I2C端口扩展器模块和4×4膜矩阵键盘
在这里插入图片描述
将两者连接在一起,请注意,我们不连接INT引脚
在这里插入图片描述
连接电源(VCC,GND和I2C线
在这里插入图片描述
连接到Arduino或您首选的微控制器。我们使用了Arduino Uno,还可以将I2C连接到A4(SDA)和A5(SCL)。

现在,我们需要安装一些库

键盘库,您可以从下面的链接下载它

Keypad-master和Keypad_I2C

代码

#include <Key.h>
#include <Keypad.h>
#include <Keypad_I2C.h>

#define I2CADDR 0x26 // Set the Address of the PCF8574

const byte ROWS = 4; // Set the number of Rows
const byte COLS = 4; // Set the number of Columns

// Set the Key at Use (4x4)
char keys [ROWS] [COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

// define active Pin (4x4)
byte rowPins [ROWS] = {0, 1, 2, 3}; // Connect to Keyboard Row Pin
byte colPins [COLS] = {4, 5, 6, 7}; // Connect to Pin column of keypad.

// makeKeymap (keys): Define Keymap
// rowPins:Set Pin to Keyboard Row
// colPins: Set Pin Column of Keypad
// ROWS: Set Number of Rows.
// COLS: Set the number of Columns
// I2CADDR: Set the Address for i2C
// PCF8574: Set the number IC
Keypad_I2C keypad (makeKeymap (keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574);

void setup () {
  Wire .begin (); // Call the connection Wire
  keypad.begin (makeKeymap (keys)); // Call the connection
  Serial.begin (9600);

}
void loop () {

  char key = keypad.getKey (); // Create a variable named key of type char to hold the characters pressed

  if (key) {// if the key variable contains
    Serial.println (key); // output characters from Serial Monitor
  }
}
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值