Arduino Enhancement 19—Use of RFID Module

Radio frequency identification "Radio Frequency IDentification, RFID" technology, also known as radio frequency identification, is a communication technology that can identify specific targets and read and write related data through radio signals without the need to establish a mechanism or system between the identification system and the specific target. Optical contact, widely used in corporate / campus cards, stored-value cards for highways, highway tolls, parking lots, community management, etc. This article introduces the use of FRID-RC522 module.

1. RFID module

RFID technology uses wireless radio frequency to carry out non-contact two-way data transmission between the reader and the radio frequency card to achieve the purpose of target identification and data exchange.

  • A tag (a radio frequency card) is composed of a coupling element and a chip. The tag contains a built-in antenna for communication with the radio frequency antenna.
  • Reader: A device that reads (and can also write in the reader card) tag information.
  • Antenna: transmits radio frequency signals between tags and readers.

The RFID-RC522 module used in this experiment uses the MFRC522 chip, SPI communication method, supports Mifarel S50, S70, Pro, Desfire and other types of cards. The attached white card and keychain are S50 cards, and each card has its own logo ( UID).

RFID module

2. Install the driver library

Arduino has RC522 driver library, click "Project"-"Load Library"-"Manage Library", find the latest version of "MFC522" to install

Install the library

3. Experimental materials

  • Uno R3 development board
  • Supporting USB data cable
  • Breadboard and supporting cable
  • RFID-RC522 module and supporting S50 white card and special-shaped card

4. Experimental steps

1. Build the circuit diagram according to the schematic diagram.

The 3.3V and GND of the RC522 module respectively correspond to the 3.3V and GND of the development board. The MOSI, MISO and SCK of the module are respectively connected to the SPI interfaces 11, 12, 13 of the development board, and the SDA and RST of the module are connected to the digital pins of the development board. 10, 9.

The experimental schematic diagram is shown below:

Experimental schematic

The physical connection diagram is shown below:

Physical connection diagram

2. Create a new sketch, copy the following code to replace the automatically generated code and save it.

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN);

byte nuidPICC[4]; //存储读取的UID

void setup() {
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
  Serial.println("RC522初始化完成...");
}

void loop() {
  //搜索新卡
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // 验证NUID是否可读
  if ( ! rfid.PICC_ReadCardSerial())
    return;

  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

  // 检查是否MIFARE卡类型
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
      piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
      piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println("不支持读取此卡类型");
    return;
  }

  // 保存读取到的UID
  for (byte i = 0; i < 4; i++) {
    nuidPICC[i] = rfid.uid.uidByte[i];
  }
    
  Serial.print("十六进制UID:");
  printHex(rfid.uid.uidByte, rfid.uid.size);
  Serial.println();

  Serial.print("十进制UID:");
  printDec(rfid.uid.uidByte, rfid.uid.size);
  Serial.println();

  // 使放置在读卡区的IC卡进入休眠状态,不再重复读卡
  rfid.PICC_HaltA();

  // 停止读卡模块编码
  rfid.PCD_StopCrypto1();
}

// 十六进制输出
void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

//十进制输出
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : "");
    Serial.print(buffer[i], DEC);
  }
}

3. Connect the development board, set the corresponding port number and development board type, and download the program.

Program download

5. Experimental phenomena

Open the serial monitor and set the baud rate to 9600 which is consistent with the program. Move the card close to the antenna sensing area of ​​the module to read the card ID.

Experimental phenomena


Pay attention to the public number "TonyCode" and reply "1024" to get 1000G learning materials.
personal blog
Insert picture description here

Published 73 original articles · praised 275 · 270,000 views +

Guess you like

Origin blog.csdn.net/TonyIOT/article/details/105386024