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.


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


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):  
– Vcc (Power) <-> 3V3 (or 5V depending on the module) 
– RST (Reset) <-> 9  
– GND (Ground) <-> GND  
– MISO (Master Input Slave Output) <-> 12
– MOSI (Master Output Slave Input) <-> 11 
– SCK (Serial Clock) <-> 13  
– SS/SDA (slave select) <-> 10
The RFID module must be connected as shown below.

Warning: Depending on the module version, the power voltage may be different (3.3V or 5V). Check the module datasheet to power the module correctly.


To use the subject RFID module we use SPI and MFRC522 libraries that are used as follows

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

#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);

// Init array that will store new NUID
byte nuidPICC[4];

void setup() {
 SPI.begin(); // Init SPI bus
 rfid.PCD_Init(); // Init MFRC522
 Serial.println(F("Scan RFID NUID..."));

void loop() {

void readRFID()
  // Look for new card
  if ( ! rfid.PICC_IsNewCardPresent()) 

    // Verify if the NUID has been readed
  if (  !rfid.PICC_ReadCardSerial())
  if (rfid.uid.uidByte[0] != nuidPICC[0] || 
    rfid.uid.uidByte[1] != nuidPICC[1] || 
    rfid.uid.uidByte[2] != nuidPICC[2] || 
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    Serial.println(F("A new card has been detected."));

    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    Serial.print(F("RFID tag in dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
  // Halt PICC

  // Stop encryption on PCD

 * Helper routine to dump a byte array as dec values to Serial.
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);



  • lock Opening with magnetic card


Find other examples and tutorials in our Automatic code generator
Code Architect

How useful was this post?

Click on a star to rate it!

Average rating 1 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?