17/08/2018

RFID enabled tag

By snorlaxprime

This RFID enabled tag tutorial show a simple RFID circuit that connects to Arduino. The circuit will detect the RFID card and play a simple tone on the speaker when the registered card is tapped and will play another tone when un-registered card is tapped.

RFID with speaker1

I had stumble across a simple circuit to simulate RFID lock and unlock via Instructables from educ8s and his website. So I used his code with little modification and added my own code from the previous post on simple Super mario speaker song, and this circuit was born.

Here are the list of components needed for this circuit:

  • Arduino Uno or nano
  • RFID card reader RC522
  • RFID card/token
  • 1K ohm resistor
  • Speaker

Step 1: Load the RFID library for your arduino.

You can do this by selecting Tools->Include Library-Manage Library, and search for “MFRC522” on the Library Manager dialog box. Once you had the library installed, then you can proceed to the next step.

Step 2. Connect the diagram as per below

RFID Tutorial with Speaker_bb

The RFID RC522 card reader have 5 connection as follows:

RST -> Pin 9 of Arduino

SDA -> Pin 10 of Arduino

MOSI -> Pin 11 of Arduino

MISO -> Pin 12 of Arduino

SCK -> Pin 13 of Arduino

Then you can connect the speaker to arduino as per the previous simple Super Mario song. In another words one end of the speaker to the GND and the other end to Pin 3 via 1K resistor.

Step 3. Load the code

You can load the following code to the arduino:

    /////////////////////////////////////////////////////////////////
   //                  Arduino RFID Tutorial               //
  //     RST -> PIN9
 //     MISO -> PIN12
 //     MOSI -> PIN11
 //     SCK  -> PIN13
 //     SDA  -> PIN10
 //    OLED Display connection
 //     SCL  -> Analog pin 5
 //     SDA  -> Analog pin 4
 //   Speaker connection 
 //     R100 ->pin 3 & GND
 /////////////////////////////////////////////////////////////////


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

const int speakerPinMelody =  3;
// wright melody
int melody[] = {NOTE_E7, NOTE_E7};
int durations[] = {12, 12};
// wrong melody
int wrongmelody[] = {NOTE_E7, NOTE_G6};
int wrongdurations[] = {12, 12};


#define SS_PIN 10
#define RST_PIN 9
 
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key; 

int code[] = {106,25,48,91}; //This is the stored UID
String uidString;
void setup() {
  
  Serial.begin(115200);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522 
  
  pinMode(speakerPinMelody, OUTPUT);
}

void loop() {
  if(  rfid.PICC_IsNewCardPresent())
  {
      readRFID();
  }
  delay(100);

}
void playSong(int theSong[], int theTempo[], int arraySize) {
  // iterate over the notes of the melody:
  int totalNotes = arraySize;
  Serial.println("Total note:" + String(totalNotes));
  for (int thisNote = 0; thisNote < totalNotes; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / theTempo[thisNote];
    tone(speakerPinMelody, theSong[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    Serial.println("Delay:" + String(pauseBetweenNotes));
    delay(pauseBetweenNotes);
    Serial.println("Stop");
    // stop the tone playing:
    noTone(speakerPinMelody);
  }
  delay(10);
  Serial.println("Final Delay");
}

void readRFID()
{
  
  rfid.PICC_ReadCardSerial();
  Serial.print(F("\nPICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }

  
    Serial.println("Scanned PICC's UID:");
    printDec(rfid.uid.uidByte, rfid.uid.size);

    uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);
    
    int i = 0;
    boolean match = true;
    while(i<rfid.uid.size)
    {
      if(!(rfid.uid.uidByte[i] == code[i]))
      {
           match = false;
      }
      i++;
    }

    if(match)
    {
      Serial.println("\nI know this card!");
      int totalNotes = int(sizeof(melody))/ sizeof(int) ;
      playSong(melody, durations, totalNotes);
    }else
    {
      int totalNotes = int(sizeof(melody))/ sizeof(int) ;
      playSong(wrongmelody, wrongdurations, totalNotes);
      Serial.println("\nUnknown Card");
    }


    // Halt PICC
  rfid.PICC_HaltA();

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

void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

If you need the “pitches.h” file you can get it from the previous post about simple Super Mario Speaker Song.

Step 4. Test the circuit

Once the load is loaded successfully, it is time to put it to test. Open your serial connection window from Tools->Serial Monitor. Select the baud rate of 115200 as per the code. Then put the RFID card or RFID fob next to the reader, you should see the serial no of the card in the serial monitor window. The speaker should also beep twice to indicate that this is not the right card. Now you need to modify the following code:

int code[] = {106,25,48,91}; //This is the stored UID

to match your RFID card serial no. Once you had done that re-upload the code back to the Arduino. And if you had done this correctly your correct RFID card should have a difference tone compare to the one that is not being registered, as shown as the following clip.

As the final thought, this circuit can be use to simulate a simple cash register. I was looking at my daughter’s toy cash register, which doesn’t make any noise when the card is being scanned. With a little modification, this can be retrofitted into the cash register and it will make her super happy to have a near real cash register that will beep when the card is touched.