Step 3: RFID Door Controller

Here, we add the RFID module to what we have put together to date.

The  RFID module communicates with its UART serial serial port to the Arduino via the SoftwareSerial library.  Also on the RFID module is a signal pin.  When a RFID Tag is within range (6cm max) an LED on lights up and the signal pin changes its state.  That change of state can be monitored by the arduino and so triggers other actions, like reading the Tag ID.

Commands are sent of through the software serial port to the RFID module.

The RFID module supports from 3.3v and 5v and it works at 13.56Mhz.
The integrated reader has a  detection distance of up to 6cm at 5v.

The command and response data structure always start with the hex bytes 0xAA 0xBB. The next byte contains the length of the command or data, followed by the bytes of the command or data.  The last byte is a CRC checksum of the bytes that make up the length and command or data.

RFID Data Format:

Head Length Command XOR Checksum
AA BB 01 01 03



With the RFID Serial port baud rate is set to 19200 a request read Tag command is sent.

Its format in HEX is AA BB 02 20 22 (5 bytes).

If for some reason there is no Tags within the working area of RFID module, then the RFID module responds with an error code AA BB 02 DF DD (5 bytes). The error code is the inverse of the command requested.
The read command is Hex 20 and its inverse is DF,  you can see this easier where you look at
the binary  00100000 inverse is 11011111.


If there is a Tag in range then the RFID module reponds with the Tags ID
ie: AA BB 06 20 5E 97 25 C7 0D (9 bytes). 5E 97 25 C7 is the Tag ID.

Below are two programs for the Arduino,  the first is used to find out what the Tag ID values are and the second uses those Tag ID values to activate the door lock.


Click to see larger Image

The 5 wiring for the RFID module:

RFID Module Arduino
Ground (Black) Ground
+5v (Red) +5v
Signal (Yellow) Digital Pin 5
Serial Tx (Blue) Digital Pin 3
Serial Rx (Green) Digital Pin 2
The code below is used to read the RFID tag IDs,  these will be displayed on the LCD and via the serial monitor.

#include <SoftwareSerial.h>
#include <
LiquidCrystal.h>

#define rxPin 3
#define txPin 2
SoftwareSerial RFID(rxPin, txPin);  
//SoftwareSerial(rxPin, txPin)

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

//#define ledPin 13
//  RFID tag present.  1 = no  0 = yes
int tag_there = 1;
int reading_tag = 0;  // prevents multipul reads of the same tag.

// command to read RFID Tag
unsigned char CMD[] = {0xAA, 0xBB, 0x02, 0x20, 0x22};   


// define array for the response.

byte response[32];                            


void setup()
{
   
// set up the LCD's number of columns and rows:
 lcd.
begin(16, 2);
 
// Print a message to the LCD.
 lcd.
print("RFID Reader");
 
 
Serial.begin(19200);
 
Serial.println("Serial Ready");

 RFID.
begin(19200);
 
Serial.println("RFID Ready");

 
pinMode(5, INPUT);    // input from RFID module tag in range.
} // end setup

//----------------------------------------------------------------------------------
// Main Loop 

void loop()
{
 lcd.
setCursor(0, 1);

 tag_there=
digitalRead(5);  
 
//  Look at RFID card to see if a Tag is in range.
 
// 1 = no tag true
 
// 0 = tag found false

if (tag_there == 0) {
   lcd.
print("tag: ");
   reading_tag = 1;
   readTag(response);  
// read the tag id into response
   
Serial.print(response[0],HEX);  // check response [bytes 0 1] AA BB
   
Serial.println(response[1],HEX);
   
Serial.println(response[2],HEX);  // the size of response [byte 2]
   
Serial.println(response[3],HEX);  // the command [byte 3]

//  if this byte is the invert of the command sent then it's a error

// (1s complement of the command) - 20 if read or DF if error
   
Serial.print(response[4],HEX);    // start of tag id number -

// the data returned, [byte 4] -> [byte 4]+([byte 2] - 1)
   
Serial.print(response[5],HEX);
   
Serial.print(response[6],HEX);
   
Serial.print(response[7],HEX);
   
Serial.println(response[8],HEX);
   
Serial.println(response[9],HEX);

   
// AABB 2 DF DD0000 0      error reading tag
   
// AABB 6 20 6CD01E54D0 0  tag read (my test tag number)

   if (response[3]==0x20)  // was read ok?

// if so. loop through the ID number displaying it to the LCD
   {

     
for (int rfid_key = 4; rfid_key < (4 + (response[2]-1)); rfid_key++)

{
       lcd.
print(response[rfid_key],HEX);
     }
   }
 }
 
else
 {
  lcd.
println("                ");
  reading_tag = 0;
 }

 
delay(200);
}
// End Main loop 
//----------------------------------------------------------------------------------
// send the read command to the RFID care and then read the response
void readTag(byte readdata[32])
{
 RFID.
write(CMD,5);     //  {0xAA, 0xBB,0x02, 0x20, 0x22};  

// send command to read RFID card
 
delay(100);               // wait for it to process
 
 
int index=0;
 
while(RFID.available() > 0) // read in result
 {
   
byte Val = RFID.read();  // read a char

   

   readdata[index] = Val;
   index++;
  }
}
// End read Tag

 hint: if the LCD shows only tag, but no ID, the rx and tx may be connected the other way round or the like.

Below is the code use to read and RFID Tag and if it's the right one, will open the lock.

Note the line, byte theKey[] = {0x6C,0xD0,0x1E,0x54,0xD0};

The numbers in red is the RFID Tag ID key that we got from the previous program,

you will need to change this to match your RFID Tags ID number.


Complete code for available on purchase

Ending notes:

Above is a basic look at working with RFID module but is can do so much more and so here is a collection of notes that I collected when developing this project. Have a look at this PDF if you would like to know more about working with the RDIF reader/writer.


Pin Definitions of RFID card module:

Pin Symbol IO Type Description Arduino or notes

J1-1

RXD

I/O

Uart Receiver serial link to arduino pin 2

J1-2

TXD

I/O

Uart Transmitter serial link to arduino pin 3

J1-3

OUT1

I/O

Output 1

J1-4

OUT2

I

Output 2

J1-5

RST

I

Reset, active-low
floating for power-on reset by default

J1-6

BUZ

I

Buzzer output, high level drive Could be for different model

J1-7

SIG

O

Interrupt output,
LOW level indicates card in the field
arduino digital pin 5
J1-8

VCC

Power

Power Positive to 5v [3v3 can be used but has a smaller field range]
J1-9

GND

GND

Power Negative to gnd  
Zircon - This is a contributing Drupal Theme
Design by WeebPal.