Arduino - Keypad 键盘

news2024/9/21 12:45:43

Arduino - Keypad

Arduino - Keypad

The keypad is widely used in many devices such as door lock, ATM, calculator…
键盘广泛应用于门锁、ATM、计算器等多种设备中。

In this tutorial, we will learn:
在本教程中,我们将学习:

  • How to use keypad 3x4 and keypad 4x4 with Arduino.
    如何将键盘 3x4 和键盘 4x4 与 Arduino 一起使用。
  • How to read value from keypad 3x4 and keypad 4x4 with Arduino.
    如何使用 Arduino 从键盘 3x4 和键盘 4x4 读取值。
  • How to verify the password inputted from keypad
    如何验证从键盘输入的密码

About Keypad 关于键盘

Keypad

The keypad is a set of buttons arranged in rows and columns (called matrix). Each button is called key
键盘是一组按行和列排列的按钮(称为矩阵)。每个按钮都称为键

Keypad has various types. Two popular types for DIY projects are keypad 3x4 (12 keys) and keypad 4x4 (16 keys).
键盘有多种类型。DIY 项目的两种流行类型是键盘 3x4(12 键)和键盘 4x4(16 键)。

Pinout 引脚排列

Keypad pins are divided into two groups: row and column.
键盘引脚分为两组:行和列。

Keypad 3x4 has 7 pins: 4 row-pins (R1, R2, R3, R4) and 3 column-pin (C1, C2, C3).
键盘 3x4 有 7 个引脚:4 个排引脚(R1、R2、R3、R4)和 3 列引脚(C1、C2、C3)。

Keypad 4x4 has 8 pins: 4 row-pins (R1, R2, R3, R4) and 4 column-pin (C1, C2, C3, C4).
键盘 4x4 有 8 个引脚:4 个排引脚(R1、R2、R3、R4)和 4 列引脚(C1、C2、C3、C4)。

Keypad Pinout

How It Works 它是如何工作的

This section is the in-depth knowledge. DON’T worry if you don’t understand. Ignore this section if it overloads you, and come back in another day. Keep reading the next sections.
本节是深入的知识。如果您不明白,请不要担心。如果它使您超负荷,请忽略此部分,并在另一天再回来。继续阅读下一节。

The process of detecting the key pressing is called scanning keypad.
检测按键的过程称为扫描键盘。

It is called “scanning” because it checks one key by one key.
它被称为“扫描”,因为它逐个键检查一个键。

Row-pins are connected to Arduino’s output pins
Row-pin连接到Arduino的输出引脚

Column pins are connected to Arduino’s input pins (INPUT_PULLUP, in this state, the value of the input pin is HIGH if the key is not pressed).
列引脚连接到Arduino的输入引脚(INPUT_PULLUP,在此状态下,如果未按下该键,则输入引脚的值为HIGH)。

For each row: 对于每一行:

  • Sets all row-pins is HIGH.
    将所有行引脚设置为高电平。
  • Sets only the current row-pin to LOW.
    仅将当前行引脚设置为低电平。
  • Reads the state of each column.
    读取每列的状态。
    • If a column-pin is HIGH ⇒ key at (row, column) is NOT pressed.
      如果列引脚为高电平,则不按下 (行、列) 处⇒键。
    • If a column-pin is LOW ⇒ key at (row, column) is pressed.
      如果列引脚为低电平,则按下 (行、列) 处⇒键。
  • Repeats the above process for the next row-pins.
    对下一个行引脚重复上述过程。

※ NOTE THAT: ※ 注意事项:

The above is one of the methods to scan keypad. We can invert all HIGH to LOW and all LOW to HIGH to scan keypad.
以上是扫描键盘的方法之一。我们可以将所有高电平反转为低电平,将所有低电平反转为高电平以扫描键盘。

Why does keypad is arranged and connected as a matrix? This makes the scanning process complicated. Why do not use each key as an independent button, then the state of the key is simply determined by reading the state of a button?
为什么键盘是以矩阵的形式排列和连接的?这使得扫描过程变得复杂。为什么不把每个键都当成一个独立的按钮,那么键的状态就是通过读取一个按钮的状态来确定的呢?

⇒ As we know, an independent button requires one Arduino’s pin and GND. Let’s take keypad 4x4 as an example. If we each key as an independent button, it requires 16 Arduino pin for 16 keys plus GND pin. If we arranged a connected key in matrix form, we just need to use 8 Arduino’s pin, so we can save Arduino’s pin. In short, the answer is: to save the Arduino pins.
⇒ 众所周知,一个独立的按钮需要一个Arduino的引脚和GND。让我们以键盘 4x4 为例。如果我们每个按键作为一个独立的按钮,它需要 16 个 Arduino 引脚用于 16 个按键加上 GND 引脚。如果我们以矩阵形式排列一个连接的密钥,我们只需要使用 8 个 Arduino 的引脚,这样我们就可以保存 Arduino 的引脚。简而言之,答案是:保存Arduino引脚。

Wiring Diagram 接线图

Arduino Keypad Wiring Diagram

This image is created using Fritzing. Click to enlarge image
此图像是使用 Fritzing 创建的。点击放大图片

How To Program For Keypad 如何为键盘编程

Thanks to Keypad library, using keypad with Arduino is a piece of cake, no matter whether you understand how the keypad works or not.
多亏了键盘库,无论您是否了解键盘的工作原理,使用带有 Arduino 的键盘都是小菜一碟。

Arduino Code Arduino代码

Keypad 3x4 键盘 3x4

#include <Keypad.h>

const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

void setup(){
  Serial.begin(9600);
}

void loop(){
  char key = keypad.getKey();

  if (key){
    Serial.println(key);
  }
}

Keypad 4x4 键盘 4x4

#include <Keypad.h>

const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

void setup(){
  Serial.begin(9600);
}

void loop(){
  char key = keypad.getKey();

  if (key){
    Serial.println(key);
  }
}

Quick Steps 快速步骤

  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
    导航到 Arduino IDE 左侧栏上的 Libraries 图标。
  • Search “keypad”, then find the keypad library by Mark Stanley, Alexander Brevig
    搜索“键盘”,然后找到 Mark Stanley、Alexander Brevig 的键盘库
  • Click Install button to install keypad library.
    单击“安装”按钮安装键盘库。

Arduino keypad library

  • Copy the above code and open with Arduino IDE
    复制上面的代码并使用Arduino IDE打开
  • Click Upload button on Arduino IDE to upload code to Arduino
    单击Arduino IDE上的“上传”按钮,将代码上传到Arduino
  • Open Serial Monitor 开放式串行监视器
  • Press some keys on keypad
    按键盘上的一些键
  • See the result in Serial Monitor
    在串行监视器中查看结果

Keypad and Password 键盘和密码

A popular application of keypad is the password input. In this application, we specify two special keys:
键盘的一个流行应用是密码输入。在此应用程序中,我们指定了两个特殊键:

  • A key to start/re-start the password input. For example, key ""
    用于启动/重新启动密码输入的密钥。例如,键“
  • A key to terminate the password input. For example, key “#”
    用于终止密码输入的键。例如,键“#”

The password will be a string that contains the remaining keys, except for two selected special keys.
密码将是一个字符串,其中包含其余密钥,但两个选定的特殊密钥除外。

When a key is pressed.
按下某个键时。

  • If the key is NOT neither "" nor “#”, append the key to the user’s input password string.
    如果密钥既不是“
    ”也不是“#”,则将密钥追加到用户的输入密码字符串中。
  • If the key is “#”, compare the user’s input password string with the password to determine the input password is correct or not, and then clear the user’s input password string
    如果密钥为“#”,则将用户的输入密码字符串与密码进行比较,以确定输入密码是否正确,然后清除用户的输入密码字符串
  • If the key is "", clear the user’s input password string
    如果密钥为“
    ”,请清除用户的输入密码字符串

Keypad - Password Code 键盘 - 密码代码

/*

 * Created by ArduinoGetStarted.com
   *
 * This example code is in the public domain
   *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-keypad
   */

#include <Keypad.h>

const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

const String password = "1234"; // change your password here
String input_password;

void setup(){
  Serial.begin(9600);
  input_password.reserve(32); // maximum input characters is 33, change if needed
}

void loop(){
  char key = keypad.getKey();

  if (key){
    Serial.println(key);

    if(key == '*') {
      input_password = ""; // clear input password
    } else if(key == '#') {
      if(password == input_password) {
        Serial.println("password is correct");
        // DO YOUR WORK HERE
        
      } else {
        Serial.println("password is incorrect, try again");
      }

      input_password = ""; // clear input password
    } else {
      input_password += key; // append new character to input password string
    }
  }
}
  • Run above code 运行上述代码
  • Open Serial Monitor 开放式串行监视器
  • Press “123456” keys and press “#”
    按“123456”键,按“#”
  • Press “1234” keys and press “#”
    按“1234”键,按“#”
  • See the result on Serial Monitor
    在串行监视器上查看结果

Video Tutorial 视频教程

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
我们正在考虑制作视频教程。如果您认为视频教程是必不可少的,请订阅我们的 YouTube 频道,为我们制作视频提供动力。

Additional Knowledge 其他知识

  • How to use the multiple passwords for keypad
    如何使用键盘的多个密码
  • How to input a multiple digits number using the keypad
    如何使用键盘输入多位数字

Challenge Yourself 挑战自我

  • Display the pressed key of the keypad on LCD. Hint: Refer to Arduino - LCD
    在 LCD 上显示键盘的按键。提示:请参阅Arduino - LCD
  • Make a door lock with password protection using the keypad.
    使用键盘制作带有密码保护的门锁。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1872493.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

WPF----自定义滚动条ScrollViewer

滚动条是项目当中经常用到的一个控件&#xff0c;大部分对外项目都有外观的需求&#xff0c;因此需要自定义&#xff0c;文中主要是针对一段动态的状态数据进行展示&#xff0c;并保证数据始终在最新一条&#xff0c;就是需要滚动条滚动到底部。 1&#xff0c;xaml中引入 <…

【微服务】Alibaba Cloud Linux环境下Docker以及MySQL安装

部署Docker 1.安装dnf dnf是新一代的rpm软件包管理器 yum -y install dnf2.安装社区版Docker&#xff08;docker-ce&#xff09; 添加docker-ce的dnf源 dnf config-manager --add-repohttps://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo安装Alibaba Cloud…

【03】从0到1构建AI生成思维导图应用 -- Agent 搭建

【03】从0到1构建AI生成思维导图应用 – Agent 搭建 大家好&#xff01;最近自己做了一个完全免费的AI生成思维导图的网站&#xff0c;支持下载&#xff0c;编辑和对接微信公众号&#xff0c;可以在这里体验&#xff1a;https://lt2mind.zeabur.app/ 上一章&#xff1a;https…

Arduino - TM1637 4 位 7 段显示器

Arduino - TM1637 4 位 7 段显示器 Arduino-TM1637 4 位 7 段显示器 A standard 4-digit 7-segment display is needed for clock, timer and counter projects, but it usually requires 12 connections. The TM1637 module makes it easier by only requiring 4 connectio…

【yolov8系列】ubuntu上yolov8的开启训练的简单记录

前言 yolov8的广泛使用&#xff0c;拉取yolov8源码工程&#xff0c;然后配置环境后直接运行&#xff0c;初步验证自己数据的检测效果&#xff0c;在数据集准备OK的情况下 需要信手拈来&#xff0c;以保证开发过程的高效进行。 本篇博客更注意为了方便自己使用时参考。顺便也记录…

yolov5驾驶员不规范行为检测

1 项目介绍 1.1 摘要 随着汽车工业的迅速发展和交通拥堵的加剧&#xff0c;驾驶员在行车过程中的不规范行为成为了导致交通事故频发的重要因素之一。为了减少交通事故的发生&#xff0c;保障道路安全&#xff0c;提高驾驶员的行车安全意识&#xff0c;本研究致力于实现驾驶员…

深度学习Week18——学习残差网络和ResNet-50算法

文章目录 深度学习Week18——学习残差网络和ResNet-50算法 一、前言 二、我的环境 三、前期工作 1、配置环境 2、导入数据 2.1 加载数据 2.2 配置数据集 2.3 数据可视化 2.4 再次检查数据 四、构建ResNet-50网络模型 五、编译模型 六、训练模型 七、模型评估 八、指定图片预测 …

速通python!!!!!!!

生成验证码图片 创建一个随机字符串&#xff08;验证码&#xff09;并存储在会话&#xff08;session&#xff09;中&#xff0c;以便后续验证。使用Java的图形API&#xff08;如java.awt和javax.imageio&#xff09;来生成一个包含该随机字符串的图片。可以添加一些干扰元素&a…

20240627优雅草新产品取得原始软件著作权授权

https://doc.youyacao.com/22/2153 20240627优雅草新产品取得原始软件著作权授权 介绍 历程消息&#xff1a;优雅草2024年新产品最新取得原始著作权两份&#xff0c;2款产品将在近期完成为商业授权产品在蜻蜓松鼠官网售卖&#xff0c;本两款产品是智慧园区能源监测管理系统解…

Redis数据迁移-RedisShake

redis-shake是阿里云Redis团队开源的用于Redis数据迁移和数据过滤的工具。 一、基本功能 redis-shake它支持解析、恢复、备份、同步四个功能 恢复restore&#xff1a;将RDB文件恢复到目的redis数据库。 备份dump&#xff1a;将源redis的全量数据通过RDB文件备份起来。 解析deco…

通义灵码上线 Visual Studio 插件市场啦!

通义灵码&#xff0c;是阿里云出品的一款基于通义大模型的智能编码辅助工具&#xff0c;提供行级/函数级实时续写、自然语言生成代码、单元测试生成、代码优化、注释生成、代码解释、研发智能问答、异常报错排查等能力&#xff0c;提供代码智能生成、研发智能问答能力。 通义灵…

Appium+python自动化(二十九)- 模拟手指在手机上多线多点作战 - 多点触控(超详解)

简介 在网页中我们经常使用缩放操作来便利的查看具体的信息&#xff0c;在appium中使用MultiAction多点触控的类来实现。MultiAction是多点触控的类&#xff0c;可以模拟用户多点操作。主要包含加载add()和执行perform()两个方法. 问题思考 在使用地图App中&#xff0c;我们…

【高级篇】主从复制与高可用性:构建坚若磐石的数据库基础设施(十二)

引言 在上一章《备份与恢复》中,我们深入探讨了如何通过各种备份策略和恢复技术,确保数据的安全性和业务的连续性。然而,为了应对更大规模的业务挑战和灾难恢复需求,仅仅依靠备份是不够的。本章,我们将聚焦于MySQL的主从复制与高可用性技术,从原理到实践,从配置到优化,…

Eslint prettier airbnb规范 配置

1.安装vscode的Eslint和prettier 插件 eslint&#xff1a;代码质量检查工具 https://eslint.nodejs.cn/docs/latest/use/getting-started prettier&#xff1a;代码风格格式化工具 https://www.prettier.cn/docs/index.html /* eslint-config-airbnb-base airbnb 规范 esl…

数字化转型:华为如何用V型方法论重塑IT团队

数字化转型不仅要求企业重新审视其业务模式&#xff0c;更要求企业的IT运作方式发生根本性的变革。传统IT开发方式由于其局限性&#xff0c;已经无法满足企业快速响应市场需求、实现业务创新的需求。 传统IT开发方式的局限性 传统IT开发方式往往采用需求交接式的工作模式&…

从零开始:视频直播美颜SDK的开发与接入详解

开发一款功能强大的美颜SDK并将其接入视频直播应用&#xff0c;成为许多开发者和企业的迫切需求。本篇文章&#xff0c;小编将详细介绍如何从零开始开发和接入视频直播美颜SDK。 一、美颜SDK的基本概念 美颜SDK是一组工具和库&#xff0c;帮助开发者在应用程序中实现美颜效果…

靶机渗透之DC-8

一、信息收集 扫一下子网段&#xff0c;发现靶机ip为192.168.145.130。 nmap -sP 192.168.145.* 进一步进行端口、系统等信息扫描&#xff0c;开放的端口为80、22&#xff0c;中间件为apache。 nmap -sT -T4 -sV -O -sC -p1-65535 192.168.145.130 再扫一下网站目录&#xf…

网络抓包分析工具

摘要 随着网络技术的快速发展&#xff0c;网络数据的传输和处理变得日益复杂。网络抓包分析工具作为网络故障排查、性能优化以及安全审计的重要工具&#xff0c;对于提升网络管理的效率和准确性具有重要意义。本文旨在设计并实现一款高效、易用的网络抓包分析工具&#xff0c;…

从ChatGPT代码执行逃逸到LLMs应用安全思考

摘要 11月7日OpenAI发布会后&#xff0c;GPT-4的最新更新为用户带来了更加便捷的功能&#xff0c;包括Python代码解释器、网络内容浏览和图像生成能力。这些创新不仅开辟了人工智能应用的新境界&#xff0c;也展示了GPT-4在处理复杂任务方面的惊人能力。然而&#xff0c;与所有…

OZON与WB平台自养号测评:优势与搭建步骤解析

随着俄罗斯跨境电商市场的蓬勃发展&#xff0c;OZON和WB平台吸引了越来越多的国内卖家入驻。为了提升产品权重、增加曝光度并加速销售&#xff0c;许多卖家选择通过自养号测评的方式来优化店铺运营。自养号测评在OZON和WB平台上具有多重显著优势。 一、自养号测评的优势 1. 权…