Advertisement
arduino rfid with em-18

Arduino RFID Access Control with EM-18

The use of Radio Frequency IDentification (RFID) technologies is growing. Many different applications are implemented in various sectors, and used for very different purposes. RFID enables wireless data collection by readers from electronic tags attached to or embedded in objects, for identification and other purposes. This article describes the construction of a simple Arduino RFID Access control DIY (Do It Yourself) Project using Arduino UNO and a RFID reader module (EM-18) to control an LED and a Relay. Circuit schematic and Arduino Sketch (source code) are provided here.

The reader transmits radio frequency when powered ON. When the tag is placed near the reader, the RFID tag will receive the radio frequency via the antenna inside tag. The radio frequency received will be converted into electrical power that is enough for the tag to transmit the data back to the RFID reader. Further, the reader will transmit the tag ID to the external device by serial communication. A wide range of reader modules are now available. The most common and easy to use reader is EM-18. This module read the RFID passive tag and sends the tag ID to the Arduino microcontroller.

arduino rfid with em-18

Reading The RFID Tag ID

First of all upload this simple program to Arduino Uno using the IDE:

int count = 0;
void setup()
{
	Serial.begin(9600);
}
void loop()
{
	if(Serial.available())
	{
		count = 0; // Reset count to zero// Keep reading Byte by Byte from the Buffer till the Buffer is empty
		{
			char input = Serial.read();
			Serial.print(input);
			count++; //
			delay(5); //
		}
		Serial.println();
		Serial.print("Tag Length : ");
		Serial.print(count);
		Serial.println(" Bytes");
	}
}

Now wire the hardware as shown here.

arduino em18 rfid

Open the Serial Monitor in Arduino IDE, hold the RFID Tag very close to the central part of the RFID Reader, and write down the displayed Tag ID. This uniqued ID is essential in the next skecth! (Our Tag ID is 51005F46642C)

The Access Control

Here, the system is configured to compare the unique id (of the tag we used) with the ID of any tag being exhibited.

Remove the existing connection and upload this new sketch to Arduino:

/* Arduino Simple RFID Access Control
Using EM-18 RFID Reader Module
Prefatory Code For Novices
An inspired design. Thanks to Internet!
T.K.Hareendran
Project designed & tested at TechNode on 17.02.2014
http://www.electroschematics.com */
#define RELAYPIN 13
#define WARNLEDPIN 12
char tag[] ="51005F46642C"; // Replace with your own Tag ID
char input[12];				// A variable to store the Tag ID being presented
int count = 0;				// A counter variable to navigate through the input[] character array
boolean flag = 0;			// A variable to store the Tag match status
void setup()
{
	Serial.begin(9600);		// Initialise Serial Communication with the Serial Monitor
	pinMode(RELAYPIN,OUTPUT);	// RELAY OUTPUT
	pinMode(WARNLEDPIN,OUTPUT); //WRONG TAG INDICATOR
}
void loop()
{
	if(Serial.available())// Check if there is incoming data in the RFID Reader Serial Buffer.
	{
		count = 0; // Reset the counter to zero
		/* Keep reading Byte by Byte from the Buffer till the RFID Reader Buffer is	empty 
		   or till 12 Bytes (the ID size of our Tag) is read */
		while(Serial.available() && count < 12) 
		{
			input[count] = Serial.read(); // Read 1 Byte of data and store it in the input[] variable
			count++; // increment counter
			delay(5);
		}
		/* When the counter reaches 12 (the size of the ID) we stop and compare each value 
		    of the input[] to the corresponding stored value */
		if(count == 12) // 
		{
			count =0; // reset counter varibale to 0
			flag = 1;
			/* Iterate through each value and compare till either the 12 values are 
			   all matching or till the first mistmatch occurs */
			while(count<12 && flag !=0)  
			{
				if(input[count]==tag[count])
				flag = 1; // everytime the values match, we set the flag variable to 1
				else
				flag= 0; 
                               /* if the ID values don't match, set flag variable to 0 and 
                                  stop comparing by exiting the while loop */
				count++; // increment i
			}
		}
		if(flag == 1) // If flag variable is 1, then it means the tags match
		{
			Serial.println("Access Allowed!");
			digitalWrite(RELAYPIN,HIGH);
			delay (5000);
			digitalWrite (RELAYPIN,LOW);
		}
		else
		{
			Serial.println("Access Denied"); // Incorrect Tag Message
			digitalWrite(WARNLEDPIN,HIGH);
			delay(5000);
			digitalWrite(WARNLEDPIN,LOW);
		}
		/* Fill the input variable array with a fixed value 'F' to overwrite 
		all values getting it	empty for the next read cycle */
		for(count=0; count<12; count++) 
		{
			input[count]= 'F';
		}
		count = 0; // Reset counter variable
	}
}

arduino rfid relay

ARD-F3

Re-wire the hardware connection (with some modifications) as shown in the diagram above. Here, pin D12 of Arduino is used to drive a standard 5mm LED, and D13 is used to drive an Electro-Magnetic Relay through a transistor-based relay driver. Everytime the tag values match, D13 goes high for 5 seconds.The Relay can be used to control an external load, like a door-striker. If the tag values don’t match, D13 remains in low state, but warning is raised by the LED connected at D12.

9 Comments

Join the conversation!

Error! Please fill all fields.
  • joaomb84gmail-com

    Hi I had the same problem, when I connected the tx from the EM-18 to the Rx of Arduino I cant´t send the code to the Arduino and cant see the cards being read.

    There is any thing I can do?

    • T.K.Hareendran

      There’s no issues with the code,that’s why I got my Tag ID is 51005F46642C through the same procedure. Please recheck your hardware wiring and/or the code. If the problem persists,feel free to revert.

  • Christian

    hi, please can you help me with a medical device ( Stryker Serfas RF) this machine uses RFID tag to prevent reuse of the probes wands, it is located on the deck of the wand, i want to know if you can help me to find the way to avoid this chip i reuse this wands, thanks a lot

  • Hans

    Where did you buy the RFID module from? I can’t find a good place for an EM-18

    Thanks!

  • samTECH

    dear Sir,
    I used these above code and circuit connection respectively which is posted by you but the difficulty is we don’t get values on serial display.

    So kindly request to you just check this code and give me some guideline.
    hope you will give me positive reply as soon as possible.

    • vishad

      use this code for check 12 digit rfid code..
      int count = 0; // count = 0
      char input[12]; // character array of size 12
      boolean flag = 0; // flag =0
      void setup()
      {
      Serial.begin(9600); // begin serial port with baud rate 9600bps
      }
      void loop()
      {
      if(Serial.available())
      {
      count = 0;
      while(Serial.available() && count < 12) // Read 12 characters and store them in input array
      {
      input[count] = Serial.read();
      count++;
      delay(5);
      }
      Serial.print(input); // Print RFID tag number

      if((input[0] ^ input[2] ^ input[4] ^ input[6] ^ input[8] == input[10]) &&
      (input[1] ^ input[3] ^ input[5] ^ input[7] ^ input[9] == input[11]))
      Serial.println("No Error");
      else
      Serial.println("Error");
      }
      }

    • joaomb84gmail-com

      I have the same problem!!

    • Aniket Dhamankar

      I am not able to read the serial number of my rfid tag i have done everthing properly as mentioned above plzz let me know about the trouble as sonn as possible

  • Munesh Parmar

    Hello sir
    sir I want to read Rfid code on My Android device Via Arduino with Usb cable. can you help me in this probleam

Looking for the latest from TI?