RFID-RC522的使用[通俗易懂]

射频识别技术RFID(RadioFrequencyIdentification),又称为电子标签、无线射频识别,是一种非接触式的自动识别技术,通过无线电讯号识别特定目标并读写相关数据而无需识别系统与特定目标之间建立机械或光学接触。可用于识别高速运动物体并可同时识别多个标签,过程中无需人工干预,操作快捷方便。可工作于各种环境,实现对各类物体或设备(人员、物品)在不同状态(移动、静止或恶劣环境)下…

大家好,又见面了,我是你们的朋友全栈君。

射频识别技术RFID(Radio Frequency Identification),又称为电子标签、无线射频识别,是一种非接触式的自动识别技术,通过无线电讯号识别特定目标并读写相关数据而无需识别系统与特定目标之间建立机械或光学接触。可用于识别高速运动物体并可同时识别多个标签,过程中无需人工干预,操作快捷方便。可工作于各种环境,实现对各类物体或设备(人员、物品)在不同状态(移动、静止或恶劣环境)下的自动识别和管理。

RFID系统主要由应答器、阅读器和高层应用组成,其中的应答器包括集成电路芯片。阅读器用于产生射频载波与应答器进行信息交互。高层应用包括信息的管理和决策。

图片

接线

Arduino Uno     <——>   RFID-RC522

10            <——>                SDA

13            <——>                SCK

11           <——>                MOSI

12            <——>                MISO

–null–     <——>                IRQ (IRQ是中断才用到的此处没有用到可以不接)

GND       <——>                GND

9              <——>                RST

3.3V        <——>               3.3V

需要下载有关的第三方库

图片

实现读取卡片信息的代码:

/*
 * --------------------------------------------------------------------------------------------------------------------
 * Example sketch/program showing how to read new NUID from a PICC to serial.
 * --------------------------------------------------------------------------------------------------------------------
 * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
 * 
 * Example sketch/program showing how to the read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
 * Reader on the Arduino SPI interface.
 * 
 * When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
 * then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
 * you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
 * will show the type, and the NUID if a new card has been detected. Note: you may see "Timeout in communication" messages
 * when removing the PICC from reading distance too early.
 * 
 * @license Released into the public domain.
 * 
 * Typical pin layout used:
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
 */
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
 
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key; 
// Init array that will store new NUID 
byte nuidPICC[4];
void setup() { 
  Serial.begin(9600);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522 
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }
  Serial.println(F("This code scan the MIFARE Classsic NUID."));
  Serial.print(F("Using the following key:"));
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}
 
void loop() {
  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if ( ! rfid.PICC_IsNewCardPresent())
    return;
  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;
  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));
  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }
  if (rfid.uid.uidByte[0] != nuidPICC[0] || 
    rfid.uid.uidByte[1] != nuidPICC[1] || 
    rfid.uid.uidByte[2] != nuidPICC[2] || 
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    Serial.println(F("A new card has been detected."));
    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   
    Serial.println(F("The NUID tag is:"));
    Serial.print(F("In hex: "));
    printHex(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
    Serial.print(F("In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
  }
  else Serial.println(F("Card read previously."));
  // Halt PICC
  rfid.PICC_HaltA();
  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}

/**
 * Helper routine to dump a byte array as hex values to Serial. 
 */
void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}
/**
 * Helper routine to dump a byte array as dec values to Serial.
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

两种不同形状的卡片所对应的识别结果:
图片

图片

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/129411.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • nginx相关面试题_结构化面试题库及答案

    nginx相关面试题_结构化面试题库及答案Nginx是什么?Nginx(enginex)是一个由c语音编写的高性能的HTTP和反向代理web服务器可以用来做什么?1、http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。2、虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。基于端口的,不同的端口基于域名的,不同域名3、反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多..

    2022年8月26日
    5
  • UART接口简介_uart接口速度

    UART接口简介_uart接口速度UART即通用异步收发传输器(UniversalAsynchronousReceiver/Transmitter),它是一种串行通信的物理接口形式。它将要传输的资料在串行通信与并行通信之间加以转换。作为把并行输入信号转成串行输出信号的芯片,UART通常被集成于其他通讯接口的连结上。一、UART硬件连接UART有4个pin(VCC,GND,RX,TX),用的TTL电平,低电平为0(0V),高电平为1(3.3V或以上)。如下图:引脚介绍:…

    2025年11月17日
    4
  • .Net 接入CAS 遇到的坑

    .Net 接入CAS 遇到的坑

    2021年7月2日
    106
  • 使用DataBaseMetaData得到表的主键

    使用DataBaseMetaData得到表的主键try{   Stringuser=”ahcwtest”;   Stringpassword=”ahcwtest”;   Stringdriver=”oracle.jdbc.driver.OracleDriver”;   Stringurl=”jdbc:oracle:thin:@192.168.71.17:1521:orcl”;   Stringsql=”SEL

    2022年6月20日
    27
  • Servlet工作原理解析(二)「建议收藏」

    Servlet工作原理解析(二)「建议收藏」上一篇文章简单介绍了Servlet容器的启动、初始化的过程,(链接在这里Servlet工作原理解析(一)),接下来介绍Servlet的创建以及实例化。一:创建Servlet对象 如果Servlet的load-on-startup配置项大于0,那么在Context容器启动时就会被实例化。 前面提到的在解析配置文件时会读取默认的globalWebXml,在conf下的web

    2022年10月4日
    2
  • 国产化替代方案_excel表格为什么替换不了

    国产化替代方案_excel表格为什么替换不了PHPExcel上一版本1.8.1于2015年发布。该项目已不再维护,可以使用,但是不建议再使用。所有用户都应该迁移到其直接后继者PhpSpreadsheet或其他替代方案。PhpSpreadsheet打破了兼容性,大大提高了代码库质量(命名空间,PSR合规性,最新PHP语言功能的使用等)。文档地址:https://phpspreadsheet.readthedocs.io/en/develo…

    2025年12月14日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号