RFID
本文采用RFID-RC522
参数
VCC 模块供电3.3V
GND 接地
接线
SDA–10
SCK–13
MOSI–11
MISO–12
RST–9
代码
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
| #include <SPI.h> #include <MFRC522.h>
MFRC522 rfid(10, 9); String item;
String MFRC522_ReadCardUID(MFRC522 *_name){ String _CardUID = ""; for (byte _i = 0; _i < _name->uid.size; _i++){ if(_name->uid.uidByte[_i] < 0x10) _CardUID += "0"; _CardUID += String(_name->uid.uidByte[_i], HEX); } return _CardUID; }
boolean MFRC522_IsNewCard(MFRC522 *_name){ if(!_name->PICC_IsNewCardPresent()) return false; if(!_name->PICC_ReadCardSerial()) return false; return true; }
void setup(){ Serial.begin(9600); SPI.begin(); rfid.PCD_Init(); item = ""; }
void loop(){ if(MFRC522_IsNewCard(&rfid)){ item = MFRC522_ReadCardUID(&rfid); Serial.println(item); delay(500); rfid.PICC_HaltA(); rfid.PCD_StopCrypto1(); }
}
|
功能
刷卡后在串口显示器显示对应字符串