SM130 RFID: Code to read Ultralite Tag IDs

I’ve been playing with the SM130 RFID reader and breakout/antenna board, and have been using the provided [example code to read the IDs of Mifare Classic tags. It doesn’t work with Mifare Ultralite tags, however. Classic tags can support authentication, but only have 4 bytes in their ID. Ultralite tags don’t support authentication, so they have a longer ID - 7 bytes - to make them slightly harder to clone.

Here is my modified version of Aaron Weiss’ original code. I’ve eliminated the XBee lines since I’m not using one. I also added a number of additional print statements to help with troubleshooting, but have commented them out.

Don P.

/*
  RFID Eval 13.56MHz Shield example sketch v10
  
  Aaron Weiss, aaron at sparkfun dot com
  OSHW license: http://freedomdefined.org/OSHW

  works with 13.56MHz MiFare 1k tags
  
  ** Modified to read both Classic & Ultralight tags by Don Pancoe, http://donpancoe.com **

  Based on hardware v13:
  D7 -> RFID RX
  D8 -> RFID TX
  D9 -> XBee TX
  D10 -> XBee RX
  
  Note: RFID Reset attached to D13 (aka status LED)
  
  Note: be sure include the SoftwareSerial lib, http://arduiniana.org/libraries/SoftwareSerial/
  
  Usage: Sketch prints 'Start' and waits for a tag. When a tag is in range, the shield reads the tag,
  blinks the 'Found' LED and prints the serial number of the tag to the serial port
  and the XBee port. 

*/
#include <SoftwareSerial.h>

SoftwareSerial rfid(7, 8);

//Prototypes
void halt(void);
void parse(void);
void print_serial(void);
void read_serial(void);
void seek(void);
void set_flag(void);

//Global var
int flag = 0;
int Str1[13];
int msb;

//INIT
void setup() {
  Serial.begin(9600);
  while(!Serial) {
    ;
  }
  Serial.println("Start");
  
  // set the data rate for the SoftwareSerial ports
  rfid.begin(19200);
  delay(10);
  halt();
}

//MAIN
void loop() {
  read_serial();
}

void halt() {
 //Halt tag,  FF 00 01 93 94, "halt tag"
  rfid.write(byte(255));
  rfid.write(byte(0));
  rfid.write(byte(1));
  rfid.write(byte(147));
  rfid.write(byte(148));
}

void parse() {
  while(rfid.available()){
    if(rfid.read() == 255){
      Str1[0] = 255;
      for(int i=1;i<13;i++){
        Str1[i]= rfid.read();
        //Serial.print(Str1[i], HEX); Serial.print(" ");
        if (i == 2 && Str1[i] == 2) {
          break;
        }
      }
      //Serial.print("end parse, flag = ");
    }
  }
}

void print_serial() {
  if(flag == 1){
    //print to serial port
    for (int i=msb; i>4; i--) {
      if (Str1[i] < 16) {
        Serial.print("0");
      }
      Serial.print(Str1[i], HEX); Serial.print(" ");
    }
    //Serial.print("end print_serial");
    Serial.println();
    delay(100);
  }
}

void read_serial() {
  seek();
  delay(10);
  parse();
  set_flag();
  print_serial();
  delay(100);
}

void seek() {
  //search for RFID tag, FF 00 01 82 83, "seek for tag"
  rfid.write(byte(255));
  rfid.write(byte(0));
  rfid.write(byte(1));
  rfid.write(byte(130));
  rfid.write(byte(131)); 
  delay(10);
}

void set_flag() {
  if(Str1[2] == 9){
    flag++;
    msb = 11;
  }
  if(Str1[2] == 6){
    flag++;
    msb = 8;
  }
  if(Str1[2] == 2){
    flag = 0;
  }
  //Serial.println(flag);
}

](http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/ID/RFID_Eval_13_56MHz_v10.pde)