Writing to 13.56mhz RFID tags

Hello everyone, I am using an Andurino Uno, with a Sparkfun RFID evaluation shield 13.56mhz SparkFun RFID Evaluation Shield - 13.56MHz - DEV-10406 - SparkFun Electronics and a Sparkfun RFID Module - SM130 Mifare RFID Module - SM130 MIFARE® (13.56 MHz) - SEN-10126 - SparkFun Electronics. I have successfully used a modified code to read RFID tags in Andurino 1.0 but I cannot find any example codes for writing to RFID tags. If anyone knows of such code and how to do it, I would be most grateful. I'm a noob at all this electronics stuff so it's really hard to get my head around, if someone could show me working code that can read and write tags I will happily reward you via PayPal, many thanks. Also, I have posted the working read code below.

/*
  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

  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/newsoftserial/

  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.
  
  06/04/2013 - Modified for compatibility with Arudino 1.0. Seb Madgwick.

*/
#include <SoftwareSerial.h>

SoftwareSerial rfid(7, ;
SoftwareSerial xbee(10, 9);

//Prototypes
void check_for_notag(void);
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[11];

//INIT
void setup()
{
  Serial.begin(9600);
  Serial.println("Start");

  // set the data rate for the SoftwareSerial ports
  xbee.begin(9600);
  rfid.begin(19200);
  delay(10);
  halt();
}

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

void check_for_notag()
{
  seek();
  delay(10);
  parse();
  set_flag();

  if(flag = 1){
    seek();
    delay(10);
    parse();
  }
}

void halt()
{
 //Halt tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)147);
  rfid.write((uint8_t)148);
}

void parse()
{
  while(rfid.available()){
    if(rfid.read() == 255){
      for(int i=1;i<11;i++){
        Str1= rfid.read();
      }
    }
  }
}

void print_serial()
{
  if(flag == 1){
    //print to serial port
    Serial.print(Str1[8], HEX);
    Serial.print(Str1[7], HEX);
    Serial.print(Str1[6], HEX);
    Serial.print(Str1[5], HEX);
    Serial.println();
    //print to XBee module
    xbee.print(Str1[8], HEX);
    xbee.print(Str1[7], HEX);
    xbee.print(Str1[6], HEX);
    xbee.print(Str1[5], HEX);
    xbee.println();
    delay(100);
    //check_for_notag();
  }
}

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

void seek()
{
  //search for RFID tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)130);
  rfid.write((uint8_t)131);
  delay(10);
}

void set_flag()
{
  if(Str1[2] == 6){
    flag++;
  }
  if(Str1[2] == 2){
    flag = 0;
  }
}

Many thanks.

void parse()
{
  while(rfid.available()){
    if(rfid.read() == 255){
      for(int i=1;i<11;i++){
        Str1= rfid.read();
      }
    }
  }
}

If there is at least one byte to read, and that byte is 255, read the other 10 bytes, too. Bad idea. Really bad.

And, of course, storing all of those bytes that way is bad, too.

Now you know why we expect you to use code tags.

I keep re-reading your post but my lack of knowledge in this area really shows. So basically what your saying is that the sample code I am using is making the job of reading more difficult that necessary? And what exactly is a code tag? Many thanks.

You should probably start with the datasheet and understand what the current program does, exactly:

http://www.sonmicro.com/en/downloads/Mifare/ds_SM130.pdf

Then you can use the datasheet to determine what commands and data to send to the reader/writer to write data. I suspect you will need to send one of these commands:
0x89 Write Block
0x8A Write Value
0x8B Write 4 Byte Block
0x8C Write Key
0x8D Increment
0x8E Decrement

Okay thanks for the advice, I will do as you say and try to understand the current program and will post the results here if I have any success.

So basically what your saying is that the sample code I am using is making the job of reading more difficult that necessary?

No. What it does is assume that the tag data, all 11 bytes, arrives all at once. Not a serial device in the world can do that. That is not how serial data is handled.

If that code works at all, it is because other things are prevent it from reading from the serial buffer until the tag data has slowly dribbled in. All those (unnecessary) delay()s, for instance.

  if(flag = 1){

Assigning a value to flag seems an unlikely thing to do here...

And what exactly is a code tag? Many thanks.

Read the how to use this forum sticky post. Ittells you what code tags are. Then go and modify your posts to include them.

Perhaps this SM130 library will help: http://forum.arduino.cc/index.php/topic,37086.0.html

Does anyone have a working copy of the SM130 library? The link is down.

According to his web site:

My Arduino RFID libraries for the SM130 and SL018/SL030 RFID readers can be found on Github.

I've not actually checked there though.

This link is still live:
http://marcboon.com/rfiduino/code/SM130v1.zip

Here are the latest version of the library. Click on the "ZIP" button to download the library as a .zip file.

There is also a library for the StrongLink SL018:

Buy the SL018 here for $17:

johnwasser:
You should probably start with the datasheet and understand what the current program does, exactly:

http://www.sonmicro.com/en/downloads/Mifare/ds_SM130.pdf

Then you can use the datasheet to determine what commands and data to send to the reader/writer to write data. I suspect you will need to send one of these commands:
0x89 Write Block
0x8A Write Value
0x8B Write 4 Byte Block
0x8C Write Key
0x8D Increment
0x8E Decrement

Well I read the .PDF fully although I didn't fully understand what I am supposed to do with the knowledge. I also downloaded all the liberies above and although they successfully uploaded to my Andurino setup, none of them stimulated my setup to respond. I also tried looking at the code to see if I could transfer it but it just made my head hurt.

The only idea I did have what I thought might wield results was to send the commands to the reader/writer as you suggested, I am guessing I was doing it wrong as when I was using the working reader code, my setup did not give me any response. Below is a picture.

I typed in what is in the picture, plus other commands with no joy.

Thanks for all the help so far, it is much appreciated.

stephen_wolf:
I also downloaded all the liberies above and although they successfully uploaded to my Andurino setup, none of them stimulated my setup to respond.

Did you try the sm130demo sketch that comes with the SM130 library? Looks like you send it a '?' (via Serial Monitor) and it will echo back the list of commands it understands:
Commands:
D - Debug on/off
A - Antenna on/off
H - Halt tag
S - Seek tag
R - Read sector
W - Write string
V - Version
Q - Sleep
X - Reset

johnwasser:

stephen_wolf:
I also downloaded all the liberies above and although they successfully uploaded to my Andurino setup, none of them stimulated my setup to respond.

Did you try the sm130demo sketch that comes with the SM130 library? Looks like you send it a '?' (via Serial Monitor) and it will echo back the list of commands it understands:
Commands:
D - Debug on/off
A - Antenna on/off
H - Halt tag
S - Seek tag
R - Read sector
W - Write string
V - Version
Q - Sleep
X - Reset

Yes I did try that but none of the commands worked, perhaps because although using the correct RFID module, it is meant for a different board. Am I correct in thinking that the working read code should respond to the commands in serial monitor without any additional code?

The original code which I posted above, is it possible to reason that the commands do not get a response from serial monitor because the commands needed to be programmed into the sketch? It was my understanding that the SM130 module would respond automatically to these commands with no programming. Many thanks.

It is not clear what code you are running on the arduino. Is it the demo code? If so do you get any response with the ? command?
You only need send the single letter not the whole thing for example
H - Halt tag
Just type H in the serial monitor window and hit enter.

Grumpy_Mike:
It is not clear what code you are running on the arduino. Is it the demo code? If so do you get any response with the ? command?
You only need send the single letter not the whole thing for example
H - Halt tag
Just type H in the serial monitor window and hit enter.

Hi Mike, yes I am using the sample code but when I give it the single letter commands it gives no response, I have tried them all.

EDIT: I am referring to a different code than you, the one I use which works is the RFID Eval 13.56MHz Shield example sketch v10, which also does not respond to any of it's commands.

You will have to post the code you are using or provide a link if you want help with it.

Grumpy_Mike:
You will have to post the code you are using or provide a link if you want help with it.

This is the code I am using:

/*
  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

  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/newsoftserial/

  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.
  
  06/04/2013 - Modified for compatibility with Arudino 1.0. Seb Madgwick.

*/
#include <SoftwareSerial.h>

SoftwareSerial rfid(7, ;
SoftwareSerial xbee(10, 9);

//Prototypes
void check_for_notag(void);
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[11];

//INIT
void setup()
{
  Serial.begin(9600);
  Serial.println("Start");

  // set the data rate for the SoftwareSerial ports
  xbee.begin(9600);
  rfid.begin(19200);
  delay(10);
  halt();
}

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

void check_for_notag()
{
  seek();
  delay(10);
  parse();
  set_flag();

  if(flag = 1){
    seek();
    delay(10);
    parse();
  }
}

void halt()
{
 //Halt tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)147);
  rfid.write((uint8_t)148);
}

void parse()
{
  while(rfid.available()){
    if(rfid.read() == 255){
      for(int i=1;i<11;i++){
        Str1= rfid.read();
      }
    }
  }
}

void print_serial()
{
  if(flag == 1){
    //print to serial port
    Serial.print(Str1[8], HEX);
    Serial.print(Str1[7], HEX);
    Serial.print(Str1[6], HEX);
    Serial.print(Str1[5], HEX);
    Serial.println();
    //print to XBee module
    xbee.print(Str1[8], HEX);
    xbee.print(Str1[7], HEX);
    xbee.print(Str1[6], HEX);
    xbee.print(Str1[5], HEX);
    xbee.println();
    delay(100);
    //check_for_notag();
  }
}

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

void seek()
{
  //search for RFID tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)130);
  rfid.write((uint8_t)131);
  delay(10);
}

void set_flag()
{
  if(Str1[2] == 6){
    flag++;
  }
  if(Str1[2] == 2){
    flag = 0;
  }
}

I am still using the same code which I mentioned in the first post as it's the only one which works with my Sparkfun RFID module and evaluation board. These are the commands that should interact with this setup and the code above:

0x89 Write Block
0x8A Write Value
0x8B Write 4 Byte Block
0x8C Write Key
0x8D Increment
0x8E Decrement

However none of the commands work and I have tried multiple different ways of typing the commands into the serial monitor.
Many thanks.

You can not send numbers like 0x89 from the serial monitor. That is a single 8 bit value expressed as a hex number. When you type that into ther serial monitor you are actually sending four ASCII digits to the arduino.
You need to have code on the PC that sends that single byte to the arduino.