Are you are the type of person that can never find their keys or are you constantly running into your building wasting those designated “elevator minutes” on having to rummage through your bag to find your ID card to get through security? Well if that sounds familiar to you from your own experiences or someone that’s close to you comes to mind, then we have just the remedy for you.

In this tutorial we are going to show you how to control a magnetic door lock using RFID technology. But first let's have a look at relays and RFID readers.

What is RFID?

RFID stands for Radio Frequency Identification. This type of technology uses radio signals to transfer and receive data. The transmitters are called tags. Each tag has a unique ID code that serves to distinguish one tag from another.

 

RFID Tag

RFID Tag

When you put the tag close to an RFID reader, the module picks up the radio signal from the transmitter and reads it.



RFID Reader

RFID Reader 

For more information on RFIDs you can refer to these tutorials:

 http://www.jayconsystems.com/tutorials/em18/ 

http://www.jayconsystems.com/tutorials/RFIDmonitor/

 

How Do Relays Work?

A relay is just a like a regular switch but controlled electronically. Which means that instead of activating it manually, we use an electric signal to open or close the switch. Usually relays are used in circuits that work with high voltages, or alternating current (AC). We can identify two different types of relay connections: normally opened (NO) and normally closed (NC).  

Normally Open Relay Circuit

 

Normally Open Relay Circuit

In this figure we see the basic mechanism of a normally open relay. When switch S1 closes, it energizes the coil which generates a magnetic field. This magnetic field attracts switch S2, forcing it to close and letting current flow in the second circuit to feed the load. Note the voltage difference between the low current circuit and the high current circuit. This means that we can use a component like an Arduino UNO, which can only provide a maximum of 5V, to control a circuit that requires more than 5V.

Normally Closed Relay Circuit

Normally Closed Relay Circuit

 This diagram shows the basic mechanism of a normally closed relay. In this circuit S2 is always closed thus feeding the load with current while S1 is opened. When S1 closes, it energizes the coil which generates a magnetic field. This magnetic field attracts S2, forcing it to open. When this happens the high current circuit stops feeding the load with current. So in other words, an NC circuit is the opposite of an NO circuit.

 The diagram below shows how these two circuits are implemented in a relay:

 

Relay Pin Layout

Relay Pin Layout

 The numbers represent the physical pins of the relay. Therefore, when current flows between pins 2 and 5, the coil energizes creating a magnetic field that will attract the switch. Pin 1 is referred to as a common, this pin is attached to the high voltage power supply that will feed the high current circuit. Pin 4 is used for NC connection, while pin 3 is used for NO connection. Below is another representation of this circuit:

Relay Pin Layout

Relay Pin Layout

In this project the high current circuit is the magnetic door lock which operates at 12V DC. The low current circuit is the Arduino UNO which operates at 5V. 

 

 You will need the following items for this project:

Wiring
Connect 5V and GND from the Arduino UNO to the power rails of the protoboard:

Connect the RFID reader as follows:

Power Lines Connection

Power Lines Connection

 

RFID Reader Connection

RFID Reader Connection  

 

RFID Reader Wiring

 

RFID Reader Wiring

 

EM-18 Pinout Numbers

EM-18 Pinout Numbers

EM-18 Pinout Names

EM-18 Pinout Names

 

RFID Reader

Arduino UNO

VCC

5V

GND

GND

RS232

Pin 0

 

The negative side of the buzzer connects to pin 3 of RFID reader, and the positive side to 5V:

 Buzzer Connection

 

Buzzer Connection

 

Buzzer Connection to Pin 3

Buzzer Connection to Pin 3 

 

Connect cathode side of green LED to pin 6 and cathode side of red LED to pin 5 in the Arduino UNO. Then connect the anode side of each LED to a 330 ohm resistor. The other end of each resistor is connected to ground. 

 

 LEDs Connection

 

LEDs Connection

 

Connect the relay to the Arduino UNO as follows:

Relay Connection to Arduino UNO

Relay Connection to Arduino UNO 

 

Relay Pin Identifiers

Relay Pin Identifiers

 

          Relay

                Arduino UNO

            Pin A

                      GND 

            Pin B

                       VCC

            Pin C

Pin 7

  

Now connect the relay to the magnetic door lock and power supply: 

Power Supply.

Power Supply

  

             Pin 1

          Red Wire of Lock

            Pin 3

       12V Power Supply

     Black Wire of Lock

      GND of Power Supply

 

Code

 Upload this code to find out the code of your RFID tag.

 

*Note: You need to disconnect the wire going to pin 0 of the Arduino UNO before uploading the code. Once the code is uploaded you can connect it again.

void setup() 
{
  Serial.begin(9600);
  Serial.println("Scan RFID Tag!");
}
void loop()                  
{
  if (Serial.available()) {
      Serial.print((char)Serial.read());
  }
}

Code Explanation

This code serves to find out the ID code of our tag. The program sets the baud rate to 9600 bps and constantly checks if there is incoming data from the RFID reader. When a tag gets close to the reader, the Arduino UNO stores the ID code in a buffer to read it and print it on the serial monitor.  

Code

Upload this code to control the magnetic door lock with your RFID tag. Once again, you need to disconnect the wire going to the Arduino UNO before uploading the code.

#define RELAYPIN 7
#define GRANTED 6
#define DENIED 5

char tag[] ="7A003159E5F7";		// 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 input[] array        
boolean flag = 0;			// A variable to store the Tag match status

void setup()
{
        Serial.begin(9600);	        // Initialize Serial Communication with the Serial Monitor
        pinMode(RELAYPIN,OUTPUT);       // RELAY OUTPUT
        pinMode(GRANTED,OUTPUT);        //GREEN LED OUTPUT 
        pinMode(DENIED,OUTPUT);         //RED LED OUTPUT 

}
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 tag.


Code Explanation

#define RELAYPIN 7
#define GRANTED 6
#define DENIED 5

char tag[] ="7A003159E5F7";		
char input[12];				
int count = 0;				   
boolean flag = 0;

Here we assign pins 7, 6, and 5 from the Arduino to the relay, green LED, and red LED accordingly. Then we create a buffer (tag) that stores the ID code of our known tag. Next we create another array (input) that will hold the ID code of the tag that will be presented to the RFID reader. Lastly we declare the variable count as integer to go through each of the characters in the arrays, and initialize it to zero. We also declare the variable flag as boolean to check if the string comparison between tags is equal or not. The variable is initialized to zero (false).  

 

void setup()
{
        Serial.begin(9600);			
        pinMode(RELAYPIN,OUTPUT);	
        pinMode(GRANTED,OUTPUT);      
        pinMode(DENIED,OUTPUT);         

}

Here we set the baud rate to 9600 bps, and set the relay and LEDs as outputs.

void loop()
{
	if(Serial.available())
	{
		count = 0; 		
		while(Serial.available() && count < 12) 
		{
			input[count] = Serial.read();
			count++; 
			delay(5);
		}

In the For Loop we constantly check if a tag has been presented against the RFID reader. If the case is true, we reset the counter to zero so that each time there is new incoming data in the serial buffer we point to the very first character of the ID number. Then we use a While Loop to store the incoming data in the input buffer.

if(count == 12) 
		{
			count =0;
			flag = 1;
			while(count

This If Statement  serves to check if the count has reached 12, which is the size of the ID number. When the count reaches 12 we reset count to zero and set flag to one. Then we use a While Loop to compare character by character the input buffer to our known tag number to see if they are the same or not. If they are equal we set flag to 1, otherwise we set it to 0.

 This If Statement is used to check if flag is equal to one. If the case is true, then we turn on the green LED and the relay for five seconds. This will make the relay open the switch from the high current circuit, which will cause the magnet to shut off, thus, granting us access to the room.  

 

if(flag == 1) 
		{
			digitalWrite(RELAYPIN,HIGH);
                        	digitalWrite(GRANTED,HIGH);
			delay (5000);
			digitalWrite (RELAYPIN,LOW);
                       		 digitalWrite (GRANTED,LOW);
		}



If flag is not equal to 1, then we turn the red LED on for three seconds to indicate that the tag presented to the RFID reader does not match our known tag.

for(count=0; count<12; count++) 
		{
			input[count]= 'F';
		}
		count = 0;
	}
}

 

The last part of the code is used overwrite whatever value the input array is storing with the value “F”  throughout the array. This will get it ready for the next reading.

Results

When the power supply is turned on, it activates the magnet which attracts the metal plate. This metal plate is attached to the door while the magnet is attached to the door frame or wall. You can use this video as a reference to install the lock:

 

 If we use the right ID the green LED is turned on and the magnet turns off for five seconds, letting the metal plate move freely. If the wrong ID is scanned, the red LED is turned on for three seconds and the magnet stays on.


You can modify the code to let as many RFID tags unlock the door as you want.