
Sebelum melanjutkan membaca, ada baiknya anda membaca dasar-dasar komunikasi SPI terlebih dahulu:
http://www.galinggang.my.id/2023/02/22/komunikasi-menggunakan-spi-serial-peripheral-interface/
RFID RC522 (Radio Frequency Identification) merupakan suatu teknologi yang memanfaatkan frekuensi radio sebagai pengidentifikasi suatu objek. Teknologi RFID ini banyak digunakan dalam proyek sistem checkout otomatis, sehingga pengguna hanya perlu menempelkan kartu ID yang dimilikinya ke mesin pembaca kartu/Reader untuk proses verifikasi. Modul RFID RC522 ini dilengkapi dengan RFID card reader dan RFID tag dalam berbentuk kartu dan gantungan kunci.
Modul RFID card reader RC522 dirancang untuk membangkitkan medan elektromagnetik 13,56 MHz yang digunakan untuk berkomunikasi dengan tag RFID. Modul ini dapat berkomunikasi dengan Mikrokontroler melalui 4 pin Serial Peripheral Interface (SPI) dengan kecepatan data maksimum 10Mbps. Sistem komunikasi SPI pada RFID card reader dipilih karena menghasilkan kecepatan akses data yang lebih tinggi daripada sistem komunikasi data lainnya. Walaupun begitu, modul RFID RC522 ini tetap mendukung komunikasi melalui protokol I2C dan UART.
Komponen RFID RC522

Cara Kerja RFID Card Reader

Sistem RFID terdiri dari komponen transponder (tag) dan transceiver/interrogator (reader). Reader RFID terdiri dari modul frekuensi radio dan antenna yang menghasilkan medan elektromagnetik frekuensi tinggi. Sedangkan, tag berisi microchip yang menyimpan dan memproses informasi, serta antena untuk menerima dan mengirimkan sinyal. Untuk membaca informasi yang dikodekan pada tag, tag harus didekatkan dengan reader.
Pada saat tag didekatkan dengan reader, reader yang menghasilkan medan elektromagnetik menyebabkan elektron bergerak melalui antena tag dan kemudian memberi daya listrik pada chip. Chip yang mendapatkan daya kemudian merespon dengan mengirimkan informasi yang dimilikinya ke reader dalam bentuk sinyal radio lain. Hal ini disebut sebagai backscatter atau perubahan gelombang elektromagnetik/RF. Reader yang dapat mendeteksi sinyal tersebut kemudian dapat mengirimkan data ke Mikrokontroler.
RFID Card Reader RC522 Pinout

Spesifikasi RFID RC522

Contoh Membaca Tag RFID



- Untuk mengakses RFID RC522 secara sederhana diperlukan Library tambahan seperti Library “MFRC522 by Miguel Balboa” yang dapat diunduh pada link berikut: https://github.com/miguelbalboa/rfid
- Setelah itu, buka Arduino, buka Sketch >> Include Library >> Add .ZIP Library, lalu pilih file rfid-master.zip yang baru saja diunduh.
- Kemudian, buka sub menu example dan pilih MFRC522 >> DumpInfo.
- Pada source code DumpInfo, ubah inisialisasi RST_PIN menjadi bernilai 5, menyesuaikan pin RST yang digunakan pada rangkaian.
- Setelah itu, unggah sketsa dan buka serial monitor, maka output terlihat seperti gambar di bawah ini
Informasi Tag RFID
Setiap tag yang digunakan pada RFID biasanya dibedakan dengan kode UID (Unique Identifier) yang “tertulis” di dalam tag RFID. Informasi mengenai kode UID, jenis, ukuran memori, dan lainnya
Memori sebesar 1K yang berada di dalam tag disusun menjadi 16 sektor (dari 0 hingga 15), setiap sektor tersebut dibagi lagi menjadi 4 blok (blok 0 hingga 3). Setiap blok menyimpan 16 byte data (dari 0 hingga 15). Oleh karena itu terdapat 1024 byte data yang dapat disebut sebagai 1K memori.


Blok 3 dari setiap sektor disebut Trailer Sector dan berisi informasi yang disebut Bit Akses untuk memberikan akses read/write ke blok yang tersisa pada suatu sektor. Sehingga hanya ada 3 blok terbawah (blok 0, 1, dan 2) dari setiap sektor yang benar-benar tersedia untuk penyimpanan data. Artinya terdapat 48 byte dari 64 byte per sektor yang dapat digunakan untuk diprogram sendiri.

Selain itu, blok 0 sektor 0 juga dikenal sebagai blok produsen / data produsen yang berisi data produsen IC, dan UID (Unique Identifier) dari pabrikan, seperti gambar di bawah ini:

.
Contoh Menulis Tag RFID dengan Arduino Uno

.


Code:
#include <SPI.h> //include the SPI bus library
#include <MFRC522.h> //include the RFID reader library
#define SS_PIN 53 //slave select pin
#define RST_PIN 5 //reset pin
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] = {"TEST RFID"}; //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");
}
Percobaan 65: Akses RFID RC522

.

- Hubungkan RST dengan pin D5 ATMEGA 2560
- Hubungkan SDA dengan pin D53 ATMEGA 2560
- Hubungkan MOSI dengan pin D51 ATMEGA 2560
- Hubungkan MISO dengan pin D50 ATMEGA 2560
- Hubungkan SCK dengan pin D52 ATMEGA 2560
- Hubungkan board ATMEGA 2560 dengan Komputer menggunakan kabel USB.
- Bukalah IDE Arduino, kemudian ketikkan kode program/sketch atau buka file RFID522_DumpInfo (BACA DUMPINFO RFID) atau RFID_READ_UID (BACA UID RFID)
- Compile menggunakan verify button (tanda ceklist pada IDE arduino) untuk mengecek ada atau tidaknya error/kesalahan dalam pengetikan.
- Upload program ke arduino dengan cara, pilih File > Upload to I/O board, atau tekan tombol tanda panah pada jendela IDE arduino.
Code RFID522_DumpInfo (BACA DUMPINFO RFID) :
/*
* --------------------------------------------------------------------------------------------------------------------
* Example sketch/program showing how to read data 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 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 ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages
* when removing the PICC from reading distance too early.
*
* If your reader supports it, this sketch/program will read all the PICCs presented (that is: multiple tag reading).
* So if you stack two or more PICCs on top of each other and present them to the reader, it will first output all
* details of the first and then the next PICC. Note that this may take some time as all data blocks are dumped, so
* keep the PICCs at reading distance until complete.
*
* @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 RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
delay(4); // Optional delay. Some board do need more time after init to be ready, see Readme
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
Video Demo:
.
Code RFID_READ_UID (BACA UID RFID):
//BACA UID DAN PENCOCOKAN
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 49
#define SS_PIN 53
byte readCard[4];
String MasterTag = "829424D5"; // REPLACE this Tag ID with your Tag ID!!!
String tagID = "";
// Create instances
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
// Initiating
Serial.begin(9600);
SPI.begin(); // SPI bus
mfrc522.PCD_Init(); // MFRC522
}
void loop()
{
//Wait until new tag is available
while (getID())
{
if (tagID == MasterTag)
{
Serial.println("AKSES DITERIMA");
// You can write any code here like opening doors, switching on a relay, lighting up an LED, or anything else you can think of.
}
else
{
Serial.println("AKSES DITOLAK");
}
Serial.println (tagID);
delay(100);
}
}
//Read new tag if available
boolean getID()
{
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return false;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return false;
}
tagID = "";
for ( uint8_t i = 0; i < 4; i++) { // The MIFARE PICCs that we use have 4 byte UID
//readCard[i] = mfrc522.uid.uidByte[i];
tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
}
tagID.toUpperCase();
mfrc522.PICC_HaltA(); // Stop reading
return true;
}
Video Demo: