108个传感器之-射频识别模块RC522(47)

108个传感器之-射频识别模块RC522(47)

介绍

借助基于RFID的步行自动结帐解决方案,可以用来实现快速购物车结账。不需要再等待某人一一敲响购物车中的每个物品;现在,利用附加在项目上的RFID标签,将几乎立即检测到购物车中的每个项目。

对于大多数基于RFID的Arduino项目,RC522 RFID读取器/写入模块是一个不错的选择。它是低功率,低成本,非常坚固,易于接入,并且在业余爱好者中非常受欢迎。

工作原理

RFID或射频标识系统由两个主要组件组成,标签添加到要识别的客户端和读取标签的服务端。

读取器由射频模块和产生高频电磁场的天线组成。而标签通常是一个被动设备(没有电池)。它由一个存储和处理信息的微芯片以及用于接收和传输信号的天线。

How RFID Technology Works Reader Writer System Tag Communication

引脚连接

连接示意图

RC522 Arduino
VCC 3.3V
GND GND
RST A0
MISO / SCL / Tx A5
MOSI A6
SCK A9
SS / SDA / Rx A7

Tips: 用例使用的是 esp32-c3-supermini,对应的引脚是 A4、A5,如果在 platformIO 上开发的话,直接输入引脚名称会提示当前的引脚对应数字号。

代码示例

写入端

这个是给卡片写入数据的案例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <Arduino.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN A7
#define RST_PIN A0

MFRC522 mfrc522(SS_PIN, RST_PIN); // instatiate a MFRC522 reader object.
MFRC522::MIFARE_Key key; // create a MIFARE_Key struct named 'key', which will hold the card information

// this is the block number we will write into and then read.
int block = 2;

byte blockcontent[16] = {"Last-Minute-Engg"}; // an array with 16 bytes to be written into one of the 64 card blocks is defined
// byte blockcontent[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //all zeros. This can be used to delete a block.

// This array is used for reading out a block.
byte readbackblock[18];

void setup()
{
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card (in case you wonder what PCD means: proximity coupling device)
Serial.println("Scan a MIFARE Classic card");

// Prepare the security key for the read and write functions.
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF; // keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
}
}

void loop()
{
// Look for new cards
if (!mfrc522.PICC_IsNewCardPresent())
{
return;
}

// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial())
{
return;
}
Serial.println("card selected");

// the blockcontent array is written into the card block
writeBlock(block, blockcontent);

// read the block back
readBlock(block, readbackblock);
// uncomment below line if you want to see the entire 1k memory with the block written into it.
// mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

// print the block contents
Serial.print("read block: ");
for (int j = 0; j < 16; j++)
{
Serial.write(readbackblock[j]);
}
Serial.println("");
}

// Write specific block
int writeBlock(int blockNumber, byte arrayAddress[])
{
// this makes sure that we only write into data blocks. Every 4th block is a trailer block for the access/security info.
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3; // determine trailer block for the sector
if (blockNumber > 2 && (blockNumber + 1) % 4 == 0)
{
Serial.print(blockNumber);
Serial.println(" is a trailer block:");
return 2;
}
Serial.print(blockNumber);
Serial.println(" is a data block:");

// authentication of the desired block for access
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK)
{
Serial.print("PCD_Authenticate() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3; // return "3" as error message
}

// writing the block
status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);
// status = mfrc522.MIFARE_Write(9, value1Block, 16);
if (status != MFRC522::STATUS_OK)
{
Serial.print("MIFARE_Write() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 4; // return "4" as error message
}
Serial.println("block was written");
}

// Read specific block
int readBlock(int blockNumber, byte arrayAddress[])
{
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3; // determine trailer block for the sector

// authentication of the desired block for access
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));

if (status != MFRC522::STATUS_OK)
{
Serial.print("PCD_Authenticate() failed (read): ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 3; // return "3" as error message
}

// reading a block
byte buffersize = 18; // we need to define a variable with the read buffer size, since the MIFARE_Read method below needs a pointer to the variable that contains the size...
status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize); //&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number
if (status != MFRC522::STATUS_OK)
{
Serial.print("MIFARE_read() failed: ");
Serial.println(mfrc522.GetStatusCodeName(status));
return 4; // return "4" as error message
}
Serial.println("block was read");
}

读取端

小结

待完善…

参考

dev 的艺术空间

RC522

108个传感器之-射频识别模块RC522(47)

http://blog.jzxer.cn/20250119/20250119-rc522/

作者

dev

发布于

2025-01-19

更新于

2025-01-07

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×