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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js 后退[通俗易懂]

    js 后退[通俗易懂]&lt;buttonclass="float"type="button"onclick="window.location.href=’__MODULE__/Payment/seller’;"style="margin-left:2%;"&gt;返&amp;nbsp;&amp;nbsp;回&lt;/button&gt;&lt;buttontype=&q

    2022年7月25日
    6
  • 第七章 vShpere新功能之容错(FT)的实现

    第七章 vShpere新功能之容错(FT)的实现

    2021年8月7日
    75
  • 批处理FOR 中的Delims和Tokens总结「建议收藏」

    批处理FOR 中的Delims和Tokens总结「建议收藏」本篇为《FOR入门与精通秘籍》的补充内容。由于《FOR入门与精通秘籍》是当初刚开始学FOR时一边学一边写的,思维难免受人家影响,思考的也不够全面,所以存在很多缺点,但又不想做过多修改,只好补写了这篇东西。       在For命令语句的参数F中,最难理解的就是Delims和Tokens两个选项,本文简单的做一个比较和总结。“For/f”常用来解析文本,读取字符串。分工上,delims负

    2022年10月12日
    0
  • phpstorm激活码2021年4月【在线注册码/序列号/破解码】

    phpstorm激活码2021年4月【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    33
  • 十进制小数转为二进制小数方法是什么_二进制转十进制计算题

    十进制小数转为二进制小数方法是什么_二进制转十进制计算题首先需要明确一点就是并不是所有的十进制小数都能用二进制数精确表示的。这是由于进制的局限性导致的。例如三进制的0.1,用十进制表示是无限循环小数0.333333…..十进制的小数转换为二进制的方法为十进制小数乘2,取出整数作为二进制小数第一位,余下的小数再乘以2,取出整数作为小数第二位,如此反复,直至余下的小数为0。例:十进制0.3750.375*2=0.75————0(0.75的整数部分为0)0.75*2=1.5————1(1.5的整数…

    2022年9月24日
    0
  • initramfs学习「建议收藏」

    initramfs学习「建议收藏」(一)helloworld一、initramfs是什么在2.6版本的linux内核中,都包含一个压缩过的cpio格式的打包文件。当内核启动时,会从这个打包文件中导出文件到内核的rootfs文件系统,然后内核检查rootfs中是否包含有init文件,如果有则执行它,作为PID为1的第一个进程。这个init进程负责启动系统后续的工作,包括定位、挂载“真正的”根文件系统设备(如果有的话)。

    2022年8月11日
    10

发表回复

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

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