RFID READ AND MATCH USING SERIAL INTERRUPT



In this tutorial we are going to see how to create a serial interrupt to read a serial RFID number.

COMPONENTS REQUIREMENT

EM18 RFID READER

ARDUINO UNO

RFID TAG

CODE

String inputString = "";
volatile bool stringComplete = false;
const char rfid1[12] = {"25008A4FD939"};
volatile char rfidmatch[12] = {};
volatile int rfidno = 0;
volatile int count1,count = 0;
 void setup() {
  Serial.begin(9600);
}
 
void loop() {
  Serial.println();
  Serial.print("RFID MATCHED:");
  Serial.print(rfid());
  delay(1000);
}
 
int rfid()
{
  if (stringComplete)
  {
    Serial.println();
    Serial.println(inputString);
    Serial.println();
    for (int i = 0; i < 12; i++)
    {
      rfidmatch[i] = inputString[i];
    }
    inputString = "";
    for (int i = 0; i < 12; i++)
    {
      if (rfid1[i] == rfidmatch[i])
      {
        count1++;
      }
    }
 
    if (count1 == 12)
    {
      rfidno = 1;
 
    }
    else
    {
      rfidno = 0;
    }
    stringComplete = false;
  }
  count1 = 0;
 
  return rfidno;
}
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (count == 11) {
      count = 0;
      stringComplete = true;
      break;
    }
    count++;
  }
}

 

CODE EXPLANATION:

void setup ()

In void setup function, Serial communication is enabled with 9600 baud rate.

Void SerialEvent ()

 In SerialEvent function is triggered whenever a Serial data is placed in Serial buffer.

Once the data is placed the SerialEvent function is triggered and the received data is stored in Inputstring

If 12 characters is readed the condition breaks and it enables the stringComplete = true.

 

Int rfid ()

In rfid function, the input string is stored in an character array and it is matched with const char rfid1 [12] array.

If the 12 digits are matched the value of rfidno becomes 1 or it becomes 0.

Void loop ()

 

In void loop function it continuously prints the rfidno value with a time delay of 1 sec.

 


Comments