Tags: , , ,
The RFID module is a smart card reader which allows among others, to activate a mechanism when the correct card is presented to the reader. We will use here the RC522 module, which is the most common.

Hardware

  • Computer
  • ArduinoUNO
  • USB Cable
  • Module RFID
  • Dupont Cable M/F x7

Scheme

The RFID RC522 module uses the SPI protocol to communicate with Arduino. SPI communication uses specific Boche of Arduino.Le microcontroller pin is as follows (left side RC522, right side Arduino UNO):  
– Vdc/3V3 (Power) <-> 5V/3V3  
– RST (Reset) <-> 9  
– GND (Ground) <-> GND  
– MISO (Master Input Slave Output) <-> 11  
– MOSI (Master Output Slave Input) <-> 10  
– SCK (Serial Clock) <-> 13  
– SS/SDA (slave select) <-> 12
The RFID module must be connected as shown below.

Code


To use the subject RFID module we use SPI and MFRC522 libraries that are used as follows
//Libraries
#include <SPI.h>
#include <MFRC522.h>

//Constants
#define SS_PIN 10
#define RST_PIN 9

//Parameters
const int ipaddress [4] ={103, 97, 67, 25};

//Variables
byte nuidPICC [4] ={0, 0, 0, 0};
MFRC522::MIFARE_Key key ;
MFRC522 rfid  = MFRC522(10, 9);

void setup(){
//Init Serial USB
Serial.begin(9600);
Serial.println(F("Initialize System"));
//init rfid
SPI.begin();
rfid.PCD_Init();
}

void loop(){
readRFID();
}

void readRFID(void ){/* function readRFID */
////Read RFID card

  for (byte i = 0; i < 6; i++) {
        key.keyByte[i] = 0xFF;
  }
 //Look for new 1 cards
  if ( ! rfid.PICC_IsNewCardPresent())
  return;

        //Verify if the NUID has been readed
  if (  !rfid.PICC_ReadCardSerial())
  return;

 //Store NUID into nuidPICC array
        for (byte i = 0; i < 4; i++) {
          nuidPICC[i] = rfid.uid.uidByte[i];
        }

        Serial.print(F("RFID In dec: "));
        //printDec(rfid.uid.uidByte, rfid.uid.size);
        Serial.println();

 //Halt PICC
  rfid.PICC_HaltA();

 //Stop encryption on PCD
  rfid.PCD_StopCrypto1();
}

Applications

  • lock Opening with magnetic card

Sources

Find more tutorials and examples in our code generator
Code Architect