Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Code for the Innovations ID-12 RFID tag reader

This is a piece of code that lets you read out the ID-12 RFID tag reader (sparkfun.com) with an Arduino. The reader uses a 9600 baud serial connection with the Arduino. When uploading the sketch, you need to pull out the RX wire to the reader, otherwise it will disturb the uploading. (Alternatively, alter the sketch, use NewSoftSerial, and also change the pin you connect the ID12 with, and use separate pins for programming the Arduino and for talking to the ID12. Not as hard as it sounds, and has many benefits!)

I added (limited) ASCII/HEX conversion to make further processing of the tag codes (5-byte words) more efficient. I also added simple checksum verification in the example.

The sketch is scraped together from (or inspired by) multiple sources, see the code. Use the drawings from HC Gilje to wire up the ID-12 (see http://hcgilje.wordpress.com/resources/rfid_id12_tagreader/).

// RFID reader ID-12 for Arduino 
// Based on code by BARRAGAN <https://people.interaction-ivrea.it/h.barragan> 
// and code from HC Gilje - https://hcgilje.wordpress.com/resources/rfid_id12_tagreader/
// Modified for Arduino by djmatic
// Modified for ID-12 and checksum by Martijn The - https://www.martijnthe.nl/
//
// Use the drawings from HC Gilje to wire up the ID-12.
// Remark: disconnect the rx serial wire to the ID-12 when uploading the sketch


void setup() {
  Serial.begin(9600);                                 // connect to the serial port
}

void loop () {
  byte i = 0;
  byte val = 0;
  byte code[6];
  byte checksum = 0;
  byte bytesread = 0;
  byte tempbyte = 0;

  if(Serial.available() > 0) {
    if((val = Serial.read()) == 2) {                  // check for header 
      bytesread = 0; 
      while (bytesread < 12) {                        // read 10 digit code + 2 digit checksum
        if( Serial.available() > 0) { 
          val = Serial.read();
          if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading 
            break;                                    // stop reading
          }

          // Do Ascii/Hex conversion:
          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,
              checksum ^= code[bytesread >> 1];       // Calculate the checksum... (XOR)
            };
          } else {
            tempbyte = val;                           // Store the first hex digit first...
          };

          bytesread++;                                // ready to read next digit
        } 
      } 

      // Output to Serial:

      if (bytesread == 12) {                          // if 12 digit read is complete
        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;
    }
  }
}