RFID

RFID & Ardiuno

RFID (Radio Frequency Identification) is a simple and effective way to track and identify multiple unique objects in a space. The majority of sensor technologies do not support tracking multiple targets, as with InfraRed or Ultrasonic sensors. In these situations, the InfraRed or Ultrasonic beam will bounce off the nearest object and disregard anything behind them.

With RFID, the isolation and identification of unique objects is achieved by assigning a distinct signature to each. RFID, a common identification technology, can interface quite easily with microcontrollers and computers. RFID networks require that both a RFID Reader and Passive or Active RFID tags to function effectively. Passive tags get their power from the energy of the radio wave itself, as a result Passive RFIDs only work over short distances. Whereas, Active tags are powered via battery and can work over larger distances.

The ID Innovations ID12 Reader is slightly more complex than the Parallax RFID, and operates at a faster baud rate – 9600 bps. All the ID Innovations Readers use the same communication protocol, and one can choose from three data output formats – ASCII, Weigand26 or Magnetic Track. The tags used in collaboration with the ID12 are EM4001 125 kHz tags, these tags encode the unique ID as ASCII hexadecimal values. Therefore, we will opt for the ASCII data output format.

The Data OutPut Format – ASCII
STX (ASCII 02)  DATA (10 ASCII)  CHECK SUM (2 ASCII)  CR (ASCII 13)  LF (ASCII 10)  ETX (ASCII 03)

The communication starts with a start-of-communication (STX) byte (ASCII 02) and ends with end-of-communication (ETX) byte (ASCII 03). The STX byte is immediately followed by the ten-byte tag ID (unique signature), a checksum, a carriage return (ASCII 13), a linefeed (ASCII 10) and then the ETX byte.

ID-12
ID-12

ID12 Pin Setup
ID12 Pin Setup



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 
/* RFID reader ID-12 for <a href="http://www.arduino.cc/playground/Code/ID12" target="_blank">Arduino</a> */ 
 
void setup() 
{
  // Open Serial Port
  Serial.begin(9600);
}
 
void loop () 
{
  byte i = 0;
  byte val = 0;
  byte code[6];
  byte checksum = 0;
  byte bytesread = 0;
  byte tempbyte = 0;
 
  if(Serial.available() > 0) 
  {
    // Check for the STX Header (02 ASCII Value)
    if((val = Serial.read()) == 2) 
    {                 
      bytesread = 0; 
      // Read the RFID 10 digit code & the 2 digit checksum
      while (bytesread < 12) 
      {                       
        if( Serial.available() > 0) 
        { 
          val = Serial.read();
          // Check for ETX | STX | CR | LF
          if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) 
          {  
            // Stop Reading - There is an Error.
            break;
          }
 
          // Convert Hex Tag ID
          if ((val >= '0') && (val <= '9')) 
         {
            val = val - '0';
         } 
         else if ((val >= 'A') && (val <= 'F')) 
         {
            val = 10 + val - 'A';
         }
 
          // Every two hex-digits, add byte to code:
          if (bytesread & 1 == 1) 
         {
            // make some space for this hex-digit by
            // shifting the previous hex-digit with 4 bits to the left:
            code[bytesread >> 1] = (val | (tempbyte << 4));
 
            if (bytesread >> 1 != 5) 
            {                
              // If we're at the checksum byte,
              // Calculate the checksum... (XOR) - Exclusive OR
              checksum ^= code[bytesread >> 1];      
            };
          } 
           else 
          {
            tempbyte = val;
          };
          // ready to read next digit
          bytesread++;                               
        } 
      } 
 
      // Print
      if (bytesread == 12) 
      {                          
        Serial.print("5-byte code: ");
        for (i=0; i<5; i++) 
        {
          if (code[i] < 16) Serial.print("0");
          Serial.print(code[i], HEX);
          Serial.print(" ");
        }
        Serial.println();
 
        Serial.print("Checksum: ");
        Serial.print(code[5], HEX);
        Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
        Serial.println();
      }
 
      bytesread = 0;
    }
  }
}
Unless otherwise stated, this code is released under the MIT License – Please use, change and share it.