My Cart

Review - RFID Modules for Arduino (I)October 18, 2012

[Spanish Version] by Arduteka

Hi friends!
Today we have a review of RFID modules for Arduino. They'll provide us a big number of possibilities  in our projects.
First of all, a small introduction to know what it means when we speak about RFID.

What is RFID?

RFID stands for Radio Frequency Identification, a technology that allows us to communicate two devices: a transceiver or RFID reader and a transponder or RFID tag.

To associate these devices, we can think about the passes that we can use in the bus, gym, our job... If we bring them without contact to a reader device, it will read them and deduct the cost of the trip, record the entry... We're sure you know what they are!

Nowadays, this technology is widely used and well-known. However, it was developed before 1960. It is thought that a similar one was used by the British Army in the Second World War to identify friendly planes.

We will be able to have two kinds of RFID tags: active and passive. An active tag needs a power supply while passive one doesn't. This type of unpowered tags allow us to  integrate them in books, animals, cards.... That's why the passive tags  are the most commonly used.

The RFID transceiver emits a low power radio frequency which creates an electromagnetic field using a coil-shaped antenna. This field will work as a signal that carries power which will be absorbed by the transponder's antenna. After that, this signal is transformed to an electric one which will power an integrated circuit. As result of this induction process, this circuit will provide us a stored information.

One disadvantage of using these passive tags is their  limited action range.  It is limited from two centimeters to no more than a meter, depending on our devices. But that is not a problem because they are perfect to a domotic integration! We can make a record of who opens the fridge! Or to limit its aperture two times per day. They are very helpful hacks if we are in a diet!

As in all Arduino projects, your imagination sets the limit.

How to use RFID with Arduino.

Although there are others, the main frequencies that we are going to work with our Arduino are 125 kHz (low frequency) and 13,56 mHz (high frequency). NFC Technology uses the last frequency to communicate so you can integrate your Arduino with mobile payment systems.

First, let's begin with the 125kHz RFID module for Arduino.

This unit has been designed to plug in the communication shield for Arduino. If we want to use it we'll need:

As you can see, we can buy read only or writable tags. A read only tag is manufactured with a serial number, that's why its use is limited to assign it to an object that we want to identify. A writable tag allows us to store some data about the analyzed object or user. They are commonly used in applications like food traceability or bus passes.

Programming these devices can be the main complexity, but with this tutorial you'll  overcome your fear of incorporating them in your projects!

In order to start with this technology, we are going to make a “Hello World” and we will complicate the applications in next tutorials. In short, we are going to perform a reading of a RFID tag that will turn on a green LED if everything goes correctly and a red one if we do anything wrong. It's a basic security hack that we can use in a future.

How to read a read-only 125 kHz RFID card EM4100  with our Arduino.

As we have seen, we need an Arduino, a communication shield and a 125 kHz RFID module. To load the program there are two options:  to remove the communication shield of the Arduino and program it or to disable the two jumpers of the shield when we are programming the Arduino. If you choose the second option, when it is programmed, you have to enable the two jumpers in XBEE position. If you do this, the RFID module will communicate with the Arduino and you will see it using the serial port. If you put the jumpers in USB position, it won't work, in other tutorial we will explain the functionality of the jumpers in USB mode.

Let's start with the communication between Arduino and RFID module.

The read-only RFID tag uses the EM4100 protocol to communicate, which gets its name from the internal chip manufacturer, EM Microelectronics.

These tags have a 64 bit capacity with an only 32 bit ID which is preprogrammed in the factory. We can see how the 64 bit are distributed in the following image:

So if we want to read an ID card number with version or manufacturer 06 and an identity number 001259E3, the 64 bit that it sends are:

Now let's see the program we will introduce in our Arduino. Then, we should analyze gradually all the program to understand it.

byte dato[5];  // Where we store the ID card
 int val = 0;   // Buffer for the data of the serial port

 void setup()
 {
   // Start the serial port at 19200 bps
   Serial.begin(19200);
   delay(3000);

   // Automatic reading mode - EM4102 decoding mode – Without pass
   // Command according Datasheet: FF 01 09 87 01 03 02 00 10 20 30 40 37
   Serial.write(0xFF);  //Header
   Serial.write(0x01);  //Reserved
   Serial.write(0x09);  //Length = Command(1) + Data(8)
   Serial.write(0x87);  //Command to send (0x87 Automatic reading mode)
   Serial.write(0x01);  //Data 1: Activate automatic reading
   Serial.write(0x03);  //Data 2: Mode – Parity decoded – Manchester RF/64
   Serial.write(0x02);  //Data 3: Number of blocks to read (2)
   Serial.write((byte)0x00);  //Data 4: Without pass
   Serial.write(0x10);  //Data 5: password byte 1
   Serial.write(0x20);  //Data 6: password byte 2
   Serial.write(0x30);  //Data 7: password byte 3
   Serial.write(0x40);  //Data 8: password byte 4
   Serial.write(0x37);  //Checksum

   delay(500);
   while(Serial.available()>0)
   {
     Serial.read();
   }
   Serial.println();
   Serial.println("RFID module is in automatic reading mode – Waiting tag...");
 }

 void loop()
 {
   //We store what is in the serial port in the variable val
   val = Serial.read();

   //If it isn't 0xFF, reread, this indicates the start of data from the tag
   while (val != 0xff)
   {
      val = Serial.read();
      delay(1000);
   }

   //We disregard the three first received byte until the ID TAG
   Serial.read();              // Reserved
   Serial.read();              // Length
   Serial.read();              // Command (it indicates the data of the tag)
   dato[0] = Serial.read();    // Read data 1
   dato[1] = Serial.read();    // Read data 2
   dato[2] = Serial.read();    // Read data 3
   dato[3] = Serial.read();    // Read data 4
   dato[4] = Serial.read();    // Read data 5

   // We identify the RFID tag
   Serial.print("Tag detected - ID: ");
   for (int i=0; i<5; i++)
   {
     if (dato[i] < 16) Serial.print("0");
     Serial.print(dato[i], HEX);
   }
   Serial.println();
 }

Let's go!!
First, we configure a 5-byte array which is the size of the the RFID card identity. In view of the fact that 8 bits are a byte and we store 10 words of 4 bits where 2 are to the version or manufacturer and 8 to the ID pre-programmed in the factory.

byte dato[5];

Then, we create a buffer where the data is stored using continuously the serial port which allows us to analyze the received data and to know if it is the moment to start reading the rest one.

In the setup, we have to configure the module's work speed. This can work from 9600bps to 115200bps. We are going to use 19200bps by default.

Serial.begin(19200);

Now that we can communicate with the RFID module, we have to configure the automatic reading mode. As we can see in the datasheet, it is only one of the multiples available commands of the RFID chip.

Once we know that the code we have to send if we want to enter in automatic reading mode is 0x87, we have to know how to send it. One more time, the datasheet gives us the solution.

Recall that the 0x prefix before a number indicates that it is a hexadecimal number.

According to the characteristics of this chip, all data sent from a host (Arduino) to a SM125 using UART must have the next structure:

First of all, we have to send 1 byte containing the Header, it is simply to send 0xFF to inform it that we are going to start.

Secondly, we have Reserved. It indicates us to put it to 0x01. It has no use.

Then, Length is the amount of data that we are going to send so that it knows where to stop. And besides, it is the addition between the data and the command to send. In our case: 1 of the command and 8 of the automatic reading mode, in conclusion 0x09.

Now we have to send it the Command that we want to interpret. As we have seen it is 0x87.

Once it knows what we are speaking about, we have to send it the Data of the automatic reading configuration which has 8 configuration bytes, 8 parameters which according to the datasheet are:

  • Data 1: 0x01 – Activate automatic reading.
  • Data 2: 0x03 – It suggests us to select this reading type if our mode is not in the list.
  • Data 3: 0x02 – It says that we should select 2 because it is the kind of reading that we have selected.
  • Data 4: 0x00 – We say it that we don't want password.
  • Data 5-8: Four random numbers because we have disabled the password.

Finally, we have to introduce the checksum. It is the addition of all the bytes that we have written before except the header 0xFF, 0x37 in our case.

This sum is for confirming that the data have been received without errors. If one is missing, the sum will be different.

Therefore, our configuration code of the module, inside the setup, would be:

Serial.write(0xFF);  //Header
Serial.write(0x01);  //Reserved
Serial.write(0x09);  //Length = Command(1) + Data(8)
Serial.write(0x87);  //Command to send (0x87 Automatic reading mode)
Serial.write(0x01);  //Data 1: Activate automatic reading
Serial.write(0x03);  //Data 2: Mode – Parity decoded – Manchester RF/64
Serial.write(0x02);  //Data 3: Number of blocks to read (2)
Serial.write((byte)0x00);  //Data 4: Without pass
Serial.write(0x10);  //Data 5: password byte 1
Serial.write(0x20);  //Data 6: password byte 2
Serial.write(0x30);  //Data 7: password byte 3
Serial.write(0x40);  //Data 8: password byte 4
Serial.write(0x37);  //Checksum

If we do a hexadecimal sum 01+09+87+01+03+02+00+10+20+30+40 we will have 0x137. But, for the checksum, we only use the last two numbers 0x37.

We have already configured our module to read the read-only RFID cards!!

Next, we put a half-second delay to prevent errors and filter the data which we don't want to show with a while. It makes the module is waiting until we receive a negative data through our serial port, then we print via serial that the module is configured.

 delay(500);
   while(Serial.available()>0)
   {
     Serial.read();
   }
   Serial.println();
   Serial.println("RFID module is in automatic reading mode – Waiting tag...");
 }

In the loop, the only thing that we are going to do is to wait a tag approaching to the interaction zone. Once the RFID tag is in the induction range, it will send us its identification with a structure similar to the explained before.

Let's see what sends us a 125 kHz read-only tag:

As you can see, the only difference between these data and the other one sent in the setup, is the section called  response that was called data.
In that response is where we will have all the information that we are going to need to know the ID card. It is a really important data if you want to do a program that allows you to make an access or other action only with this card!

So our loop will be:

1º We have to acquire a data using the serial port and store it in a variable.

val = Serial.read();

2º The stored data is analyzed to test if it is the beginning of the card's ID (0xFF).

while (val != 0xff)
{
val = Serial.read();
delay(1000);
}

This loop will be constantly implemented until it finds 0xFF. Once it detects this value, the program will continue its implementation.

Now that we are at the beginning of the data that we want to analyze, we have to save it in the array.

For this, we have to disregard 3 bytes (reserved, length and command) until it founds response. This is as easy as reading three times the serial port:

Serial.read();              // Reserved
Serial.read();              // Length
Serial.read();              // Command (it indicates the data of the tag)

Afterwards, we only have to store the 5 bytes that we want in our array.

dato[0] = Serial.read();    // Read data 1
dato[1] = Serial.read();    // Read data 2
dato[2] = Serial.read();    // Read data 3
dato[3] = Serial.read();    // Read data 4
dato[4] = Serial.read();    // Read data 5

Once we have the data stored, it has to send them using the serial port. With this, we will be able to watch them in the serial monitor of our Arduino IDE:

Serial.print("Tag detected - ID: ");
for (int i=0; i<5; i++)
{
if (dato[i] < 16) Serial.print("0");
Serial.print(dato[i], HEX);
}

As you can see, firstly, we tell it that if the acquired data is lower than 16, it will print a 0. This is made because when you print a value with that condition (like number 10 in decimal) the result will be like 0A and not A.

Then, we tell it to print what is in that position in the array and that this data is in hexadecimal.

Finally, we make a line break.

Serial.println();

With this, we have showed everything you need to have no doubt about how to read an 125 kHz read-only RFID card.

Once we have the ID Card, we can do a program that makes a specific task when the card is detected. In this example, we are going to turn on a LED, but we can adapt it to an access control project easily!!

/*
Programa para control de acceso con TAG RFID 125 kHZ
Almacenamos el ID de una etiqueta, y solo le permitimos
el acceso a ella.
www.arduteka.com
*/

 byte dato[5];  //Where we store the ID card
 int val = 0;   // Buffer for the data of the serial port
 byte ID_Etiqueta[5] = {0x52,0x00,0x1A,0xDA,0x2D}; //We store the ID Card
 int LED_OK = 11;
 int LED_NO = 12;

 void setup()
 {
   // Start the serial port at 19200 bps
   Serial.begin(19200);
   pinMode(LED_OK, OUTPUT);
   pinMode(LED_NO, OUTPUT);
   delay(3000);

   // Automatic reading mode - EM4102 decoding mode – Without pass
   // Command according Datasheet: FF 01 09 87 01 03 02 00 10 20 30 40 37
   Serial.write(0xFF);  //Header
   Serial.write(0x01);  //Reserved
   Serial.write(0x09);  //Length = Command(1) + Data(8)
   Serial.write(0x87);  //Command to send (0x87 Automatic reading mode)
   Serial.write(0x01);  //Data 1: Activate automatic reading
   Serial.write(0x03);  //Data 2: Mode – Parity decoded – Manchester RF/64
   Serial.write(0x02);  //Data 3: Number of blocks to read (2)
   Serial.write((byte)0x00);  //Data 4: Without pass
   Serial.write(0x10);  //Data 5: password byte 1
   Serial.write(0x20);  //Data 6: password byte 2
   Serial.write(0x30);  //Data 7: password byte 3
   Serial.write(0x40);  //Data 8: password byte 4
   Serial.write(0x37);  //Checksum
   delay(500);
while(Serial.available()>0)
   {
     Serial.read();
   }
   Serial.println();
   Serial.println("RFID module is in automatic reading mode – Waiting tag...");
 }

 void loop()
 {
   //We store what is in the serial port in the variable val
   val = Serial.read();

   //If it isn't 0xFF, reread, this indicates the start of data from the tag
   while (val != 0xff)
   {
      val = Serial.read();
      delay(1000);
   }

   //We disregard the three first received byte until the ID TAG
   Serial.read();              // Reserved
   Serial.read();              // Length
   Serial.read();              // Command (it indicates the data of the tag)
   dato[0] = Serial.read();    // Read data 1
   dato[1] = Serial.read();    // Read data 2
   dato[2] = Serial.read();    // Read data 3
   dato[3] = Serial.read();    // Read data 4
   dato[4] = Serial.read();    // Read data 5

   // We identify the RFID tag

   boolean etiqueta = true;

   Serial.print("Tag detected - ID: ");
   for (int i=0; i<5; i++)
   {
     if (dato[i] < 16) Serial.print("0");
     Serial.print(dato[i], HEX);
     if (dato[i] != ID_Etiqueta[i])
     {
       etiqueta = false;
     }
   }

   //We diregard between this stored card and the rest ones
      Serial.println();
   if (etiqueta)
   {
     Serial.println("Card accepted");
     digitalWrite(LED_OK, HIGH);
     delay(1000);
     digitalWrite(LED_OK, LOW);
   }
   else
   {
     Serial.println("Card denied");
     digitalWrite(LED_NO, HIGH);
     delay(1000);
     digitalWrite(LED_NO, LOW);
   }
 }

How to read an 125 kHz RFID card T5557  with Arduino.

The reading and writing cards use the T5557 data structure to communicate instead of  using EM4100. But the modus operandi is very similar.

They have a capacity of 330 bit assigned in 10 parts of 33 bit. There are available only 22,4 to store the data defined by the user. Which means that they have a structure of 7 blocks of 32 read and write bits. We can see it in the next image.

Let's see first how to do a reading of the content of a T5557 RFID tag:

byte block1_byte1 = 0x00;
   byte block1_byte2 = 0x00;
   byte block1_byte3 = 0x00;
   byte block1_byte4 = 0x00;

   byte block2_byte1 = 0x00;
   byte block2_byte2 = 0x00;
   byte block2_byte3 = 0x00;
   byte block2_byte4 = 0x00;

   byte block3_byte1 = 0x00;
   byte block3_byte2 = 0x00;
   byte block3_byte3 = 0x00;
   byte block3_byte4 = 0x00;

   byte block4_byte1 = 0x00;
   byte block4_byte2 = 0x00;
   byte block4_byte3 = 0x00;
   byte block4_byte4 = 0x00;

   byte block5_byte1 = 0x00;
   byte block5_byte2 = 0x00;
   byte block5_byte3 = 0x00;
   byte block5_byte4 = 0x00;

   byte block6_byte1 = 0x00;
   byte block6_byte2 = 0x00;
   byte block6_byte3 = 0x00;
   byte block6_byte4 = 0x00;

   byte block7_byte1 = 0x00;
   byte block7_byte2 = 0x00;
   byte block7_byte3 = 0x00;
   byte block7_byte4 = 0x00;

   int val = 0;

   void setup(){
   // Start serial port 19200 bps
   Serial.begin(19200);

   // Automatic reading mode without password

   Serial.write(0xFF);
   Serial.write(0x01);
   Serial.write(0x09);
   Serial.write(0x87);
   Serial.write(0x01);
   Serial.write(0x01);
   Serial.write(0x07);
   Serial.write((byte)0x00);
   Serial.write(0x10);
   Serial.write(0x20);
   Serial.write(0x30);
   Serial.write(0x40);
   Serial.write(0x3A);

   delay(500);
   Serial.flush();
   Serial.println();
   Serial.println();
   Serial.println("RFID module in automatic reading mode");
   }

   void loop(){

   val = Serial.read();
   while (val != 0xff){
      Serial.println("Waiting card");
      val = Serial.read();
      delay(1000);
   }

   Serial.read();    //Reserved
   Serial.read();    //Lenght
   Serial.read();    //Command

   block1_byte1 = Serial.read();    // Read block 1 byte 1
   block1_byte2 = Serial.read();    // Read block 1 byte 2
   block1_byte3 = Serial.read();    // Read block 1 byte 3
   block1_byte4 = Serial.read();    // Read block 1 byte 4

   block2_byte1 = Serial.read();    // Read block 2 byte 1
   block2_byte2 = Serial.read();    // Read block 2 byte 2
   block2_byte3 = Serial.read();    // Read block 2 byte 3
   block2_byte4 = Serial.read();    // Read block 2 byte 4

   block3_byte1 = Serial.read();    // Read block 3 byte 1
   block3_byte2 = Serial.read();    // Read block 3 byte 2
   block3_byte3 = Serial.read();    // Read block 3 byte 3
   block3_byte4 = Serial.read();    // Read block 3 byte 4

   block4_byte1 = Serial.read();    // Read block 4 byte 1
   block4_byte2 = Serial.read();    // Read block 4 byte 2
   block4_byte3 = Serial.read();    // Read block 4 byte 3
   block4_byte4 = Serial.read();    // Read block 4 byte 4

   block5_byte1 = Serial.read();    // Read block 5 byte 1
   block5_byte2 = Serial.read();    // Read block 5 byte 2
   block5_byte3 = Serial.read();    // Read block 5 byte 3
   block5_byte4 = Serial.read();    // Read block 5 byte 4

   block6_byte1 = Serial.read();    // Read block 6 byte 1
   block6_byte2 = Serial.read();    // Read block 6 byte 2
   block6_byte3 = Serial.read();    // Read block 6 byte 3
   block6_byte4 = Serial.read();    // Read block 6 byte 4

   block7_byte1 = Serial.read();    // Read block 7 byte 1
   block7_byte2 = Serial.read();    // Read block 7 byte 2
   block7_byte3 = Serial.read();    // Read block 7 byte 3
   block7_byte4 = Serial.read();    // Read block 7 byte 4

   // Print the card data
   Serial.println();
   Serial.println("T5557 card – Reading data: ");
   Serial.println("---------------------------------------");
   Serial.print("-- Block 1 -- | "); Serial.print(block1_byte1, HEX);
   Serial.print(" | ");  Serial.print(block1_byte2, HEX);
   Serial.print(" | ");  Serial.print(block1_byte3, HEX);
   Serial.print(" | ");  Serial.print(block1_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 2 -- | ");  Serial.print(block2_byte1, HEX);
   Serial.print(" | ");  Serial.print(block2_byte2, HEX);
   Serial.print(" | ");  Serial.print(block2_byte3, HEX);
   Serial.print(" | ");  Serial.print(block2_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 3 -- | ");  Serial.print(block3_byte1, HEX);
   Serial.print(" | ");  Serial.print(block3_byte2, HEX);
   Serial.print(" | ");  Serial.print(block3_byte3, HEX);
   Serial.print(" | ");  Serial.print(block3_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 4 -- | ");  Serial.print(block4_byte1, HEX);
   Serial.print(" | ");  Serial.print(block4_byte2, HEX);
   Serial.print(" | ");  Serial.print(block4_byte3, HEX);
   Serial.print(" | ");  Serial.print(block4_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 5 -- | ");  Serial.print(block5_byte1, HEX);
   Serial.print(" | ");  Serial.print(block5_byte2, HEX);
   Serial.print(" | ");  Serial.print(block5_byte3, HEX);
   Serial.print(" | ");  Serial.print(block5_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 6 -- | ");  Serial.print(block6_byte1, HEX);
   Serial.print(" | ");  Serial.print(block6_byte2, HEX);
   Serial.print(" | ");  Serial.print(block6_byte3, HEX);
   Serial.print(" | ");  Serial.print(block6_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 7 -- | ");  Serial.print(block7_byte1, HEX);
   Serial.print(" | ");  Serial.print(block7_byte2, HEX);
   Serial.print(" | ");  Serial.print(block7_byte3, HEX);
   Serial.print(" | ");  Serial.print(block7_byte4, HEX);
   Serial.println(" |");
   Serial.println("---------------------------------------");

   Serial.println();

   }

First of all, we create a set of variables to store the data that we will read of our RFID tag. Because of the number of data blocks (seven) we have to create 7 blocks with a variable for each byte that is read. It would be:

byte block1_byte1 = 0x00;
byte block1_byte2 = 0x00;
byte block1_byte3 = 0x00;
byte block1_byte4 = 0x00;

byte block2_byte1 = 0x00;
byte block2_byte2 = 0x00;
byte block2_byte3 = 0x00;
byte block2_byte4 = 0x00;

byte block3_byte1 = 0x00;
byte block3_byte2 = 0x00;
byte block3_byte3 = 0x00;
byte block3_byte4 = 0x00;

byte block4_byte1 = 0x00;
byte block4_byte2 = 0x00;
byte block4_byte3 = 0x00;
byte block4_byte4 = 0x00;

byte block5_byte1 = 0x00;
byte block5_byte2 = 0x00;
byte block5_byte3 = 0x00;
byte block5_byte4 = 0x00;

byte block6_byte1 = 0x00;
byte block6_byte2 = 0x00;
byte block6_byte3 = 0x00;
byte block6_byte4 = 0x00;

byte block7_byte1 = 0x00;
byte block7_byte2 = 0x00;
byte block7_byte3 = 0x00;
byte block7_byte4 = 0x00;

We make, like in the program before, a variable that we will use as a buffer to store in it all that we read in the serial port.

int val = 0;

In the Setup, we start establishing the communication speed of our Arduino and the RFID module. As in the previous tests, it is 19200 bps.

Serial.begin(19200);

As we can see in the datasheet, the code will start configuring the module to be understood by these cards:

   Serial.write(0xFF);           //Header
   Serial.write(0x01);           //Reserved
   Serial.write(0x09);           //Lenght
   Serial.write(0x87);           //Automatic reading
   Serial.write(0x01);           //Enable automatic reading
   Serial.write(0x01);           //Track mode - Manchester RF/64
   Serial.write(0x07);           //Reading blocks
   Serial.write((byte)0x00);     //Without pass
   Serial.write(0x10);           //Random numbers
   Serial.write(0x20);           //Random numbers
   Serial.write(0x30);           //Random numbers
   Serial.write(0x40);           //Random numbers
   Serial.write(0x3A);           //Checksum

With this, we have got the RFID module ready to read the T5557 cards.

The next step, after  displaying the message “all right” via the serial port, is to set the loop, like in the example before.

   val = Serial.read();
   while (val != 0xff){
      Serial.println("Esperando tarjeta");
      val = Serial.read();
      delay(1000);
   }

Once the string has detected a reading, we have to analyze it and read what is inside: the 7 stored data blocks. We have to disregard 3 readings before starting to store the byte in the variables:

Serial.read();    // Reserved
   Serial.read();    // Length
   Serial.read();    // Command

   block1_byte1 = Serial.read();    // Read block 1 byte 1
   block1_byte2 = Serial.read();    // Read block 1 byte 2
   block1_byte3 = Serial.read();    // Read block 1 byte 3
   block1_byte4 = Serial.read();    // Read block 1 byte 4

   block2_byte1 = Serial.read();    // Read block 2 byte 1
   block2_byte2 = Serial.read();    // Read block 2 byte 2
   block2_byte3 = Serial.read();    // Read block 2 byte 3
   block2_byte4 = Serial.read();    // Read block 2 byte 4

   block3_byte1 = Serial.read();    // Read block 3 byte 1
   block3_byte2 = Serial.read();    // Read block 3 byte 2
   block3_byte3 = Serial.read();    // Read block 3 byte 3
   block3_byte4 = Serial.read();    // Read block 3 byte 4

   block4_byte1 = Serial.read();    // Read block 4 byte 1
   block4_byte2 = Serial.read();    // Read block 4 byte 2
   block4_byte3 = Serial.read();    // Read block 4 byte 3
   block4_byte4 = Serial.read();    // Read block 4 byte 4

   block5_byte1 = Serial.read();    // Read block 5 byte 1
   block5_byte2 = Serial.read();    // Read block 5 byte 2
   block5_byte3 = Serial.read();    // Read block 5 byte 3
   block5_byte4 = Serial.read();    // Read block 5 byte 4

   block6_byte1 = Serial.read();    // Read block 6 byte 1
   block6_byte2 = Serial.read();    // Read block 6 byte 2
   block6_byte3 = Serial.read();    // Read block 6 byte 3
   block6_byte4 = Serial.read();    // Read block 6 byte 4

   block7_byte1 = Serial.read();    // Read block 7 byte 1
   block7_byte2 = Serial.read();    // Read block 7 byte 2
   block7_byte3 = Serial.read();    // Read block 7 byte 3
   block7_byte4 = Serial.read();    // Read block 7 byte 4

Now, we send the variables to our Arduino serial monitor and we will be able to analyze the data of the RFID tag.

Serial.println("T5557 card – Reading data: ");
   Serial.println("---------------------------------------");
   Serial.print("-- Block 1 -- | "); Serial.print(block1_byte1, HEX);
   Serial.print(" | ");  Serial.print(block1_byte2, HEX);
   Serial.print(" | ");  Serial.print(block1_byte3, HEX);
   Serial.print(" | ");  Serial.print(block1_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 2 -- | ");  Serial.print(block2_byte1, HEX);
   Serial.print(" | ");  Serial.print(block2_byte2, HEX);
   Serial.print(" | ");  Serial.print(block2_byte3, HEX);
   Serial.print(" | ");  Serial.print(block2_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 3 -- | ");  Serial.print(block3_byte1, HEX);
   Serial.print(" | ");  Serial.print(block3_byte2, HEX);
   Serial.print(" | ");  Serial.print(block3_byte3, HEX);
   Serial.print(" | ");  Serial.print(block3_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 4 -- | ");  Serial.print(block4_byte1, HEX);
   Serial.print(" | ");  Serial.print(block4_byte2, HEX);
   Serial.print(" | ");  Serial.print(block4_byte3, HEX);
   Serial.print(" | ");  Serial.print(block4_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 5 -- | ");  Serial.print(block5_byte1, HEX);
   Serial.print(" | ");  Serial.print(block5_byte2, HEX);
   Serial.print(" | ");  Serial.print(block5_byte3, HEX);
   Serial.print(" | ");  Serial.print(block5_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 6 -- | ");  Serial.print(block6_byte1, HEX);
   Serial.print(" | ");  Serial.print(block6_byte2, HEX);
   Serial.print(" | ");  Serial.print(block6_byte3, HEX);
   Serial.print(" | ");  Serial.print(block6_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 7 -- | ");  Serial.print(block7_byte1, HEX);
   Serial.print(" | ");  Serial.print(block7_byte2, HEX);
   Serial.print(" | ");  Serial.print(block7_byte3, HEX);
   Serial.print(" | ");  Serial.print(block7_byte4, HEX);
   Serial.println(" |");
   Serial.println("---------------------------------------");

With this structure, we'll get something similar to the following image:

As we can see, this card has data in blocks 1 and 3.

How to write an 125 kHz RFID card T5557  with Arduino.

The program is very similar to the previous one but we will only add the function of writing and a small counter to write it only once.

Here you have the complete program:

byte block1_byte1 = 0x00;
   byte block1_byte2 = 0x00;
   byte block1_byte3 = 0x00;
   byte block1_byte4 = 0x00;

   byte block2_byte1 = 0x00;
   byte block2_byte2 = 0x00;
   byte block2_byte3 = 0x00;
   byte block2_byte4 = 0x00;

   byte block3_byte1 = 0x00;
   byte block3_byte2 = 0x00;
   byte block3_byte3 = 0x00;
   byte block3_byte4 = 0x00;

   byte block4_byte1 = 0x00;
   byte block4_byte2 = 0x00;
   byte block4_byte3 = 0x00;
   byte block4_byte4 = 0x00;

   byte block5_byte1 = 0x00;
   byte block5_byte2 = 0x00;
   byte block5_byte3 = 0x00;
   byte block5_byte4 = 0x00;

   byte block6_byte1 = 0x00;
   byte block6_byte2 = 0x00;
   byte block6_byte3 = 0x00;
   byte block6_byte4 = 0x00;

   byte block7_byte1 = 0x00;
   byte block7_byte2 = 0x00;
   byte block7_byte3 = 0x00;
   byte block7_byte4 = 0x00;

   int val = 0;
   boolean count = true;

    void setup()
 {
   // Start the serial port 19200 bps
   Serial.begin(19200);
   delay(2000);

   // Automatic reading mode
  Serial.write(0xFF);
  Serial.write(0x01);
  Serial.write(0x09);
  Serial.write(0x87);
  Serial.write(0x01);
  Serial.write(0x01);
  Serial.write(0x07);
  Serial.write((byte)0x00);
  Serial.write(0x10);
  Serial.write(0x20);
  Serial.write(0x30);
  Serial.write(0x40);
  Serial.write(0x3A);

  //We disregard some configuration bit that are not used
  for (int x=0; x<256; x++)
  {Serial.read();}

   delay(500);
   Serial.flush();
   Serial.println();
   Serial.println();
   Serial.println("RFID module is ready");
 }

  void loop()
 {

   for (int x=0; x<128; x++)
   {Serial.read();}

   //We store what is in the serial port in the variable val
   val = Serial.read();

   //If it isn't 0xFF, reread, this indicates the beginning of data from the tag
   while (val != 0xff)
   {
      Serial.println("Waiting tag");
      val = Serial.read();
      delay(1000);
   }

   Serial.println("Card detected, please don't remove it...");
   Serial.read();    //Reserved
   Serial.read();    //Lenght
   Serial.read();    //Command

   block1_byte1 = Serial.read();    // Read block 1 byte 1
   block1_byte2 = Serial.read();    // Read block 1 byte 2
   block1_byte3 = Serial.read();    // Read block 1 byte 3
   block1_byte4 = Serial.read();    // Read block 1 byte 4

   block2_byte1 = Serial.read();    // Read block 2 byte 1
   block2_byte2 = Serial.read();    // Read block 2 byte 2
   block2_byte3 = Serial.read();    // Read block 2 byte 3
   block2_byte4 = Serial.read();    // Read block 2 byte 4

   block3_byte1 = Serial.read();    // Read block 3 byte 1
   block3_byte2 = Serial.read();    // Read block 3 byte 2
   block3_byte3 = Serial.read();    // Read block 3 byte 3
   block3_byte4 = Serial.read();    // Read block 3 byte 4

   block4_byte1 = Serial.read();    // Read block 4 byte 1
   block4_byte2 = Serial.read();    // Read block 4 byte 2
   block4_byte3 = Serial.read();    // Read block 4 byte 3
   block4_byte4 = Serial.read();    // Read block 4 byte 4

   block5_byte1 = Serial.read();    // Read block 5 byte 1
   block5_byte2 = Serial.read();    // Read block 5 byte 2
   block5_byte3 = Serial.read();    // Read block 5 byte 3
   block5_byte4 = Serial.read();    // Read block 5 byte 4

   block6_byte1 = Serial.read();    // Read block 6 byte 1
   block6_byte2 = Serial.read();    // Read block 6 byte 2
   block6_byte3 = Serial.read();    // Read block 6 byte 3
   block6_byte4 = Serial.read();    // Read block 6 byte 4

   block7_byte1 = Serial.read();    // Read block 7 byte 1
   block7_byte2 = Serial.read();    // Read block 7 byte 2
   block7_byte3 = Serial.read();    // Read block 7 byte 3
   block7_byte4 = Serial.read();    // Read block 7 byte 4

   // We print the code of the card
   Serial.println();
   Serial.println("T5557 card – Reading data: ");
   Serial.println("---------------------------------------");
   Serial.print("-- Block 1 -- | "); Serial.print(block1_byte1, HEX);
   Serial.print(" | ");  Serial.print(block1_byte2, HEX);
   Serial.print(" | ");  Serial.print(block1_byte3, HEX);
   Serial.print(" | ");  Serial.print(block1_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 2 -- | ");  Serial.print(block2_byte1, HEX);
   Serial.print(" | ");  Serial.print(block2_byte2, HEX);
   Serial.print(" | ");  Serial.print(block2_byte3, HEX);
   Serial.print(" | ");  Serial.print(block2_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 3 -- | ");  Serial.print(block3_byte1, HEX);
   Serial.print(" | ");  Serial.print(block3_byte2, HEX);
   Serial.print(" | ");  Serial.print(block3_byte3, HEX);
   Serial.print(" | ");  Serial.print(block3_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 4 -- | ");  Serial.print(block4_byte1, HEX);
   Serial.print(" | ");  Serial.print(block4_byte2, HEX);
   Serial.print(" | ");  Serial.print(block4_byte3, HEX);
   Serial.print(" | ");  Serial.print(block4_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 5 -- | ");  Serial.print(block5_byte1, HEX);
   Serial.print(" | ");  Serial.print(block5_byte2, HEX);
   Serial.print(" | ");  Serial.print(block5_byte3, HEX);
   Serial.print(" | ");  Serial.print(block5_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 6 -- | ");  Serial.print(block6_byte1, HEX);
   Serial.print(" | ");  Serial.print(block6_byte2, HEX);
   Serial.print(" | ");  Serial.print(block6_byte3, HEX);
   Serial.print(" | ");  Serial.print(block6_byte4, HEX);
   Serial.println(" |");
   Serial.print("-- Block 7 -- | ");  Serial.print(block7_byte1, HEX);
   Serial.print(" | ");  Serial.print(block7_byte2, HEX);
   Serial.print(" | ");  Serial.print(block7_byte3, HEX);
   Serial.print(" | ");  Serial.print(block7_byte4, HEX);
   Serial.println(" |");
   Serial.println("---------------------------------------");
   delay(3000);

if (count == true)
{
//We configure it like a writer
   Serial.println("Writing...");
   byte dato1 = 0x66;
   byte dato2 = 0x66;
   byte dato3 = 0x66;
   byte dato4 = 0x66;
   byte bloque = 0x02;
   byte checksum = 0x01 + 0x06 + 0x20 + bloque + dato1 + dato2 + dato3 + dato4;
   Serial.write(0xFF);
   Serial.write(0x01);
   Serial.write(0x06);
   Serial.write(0x20);
   Serial.write(bloque);
   Serial.write(dato1);
   Serial.write(dato2);
   Serial.write(dato3);
   Serial.write(dato4);
   Serial.write(checksum);
   delay(1000);

   count = false;
   Serial.println("The card has been properly recorded");
 }
 else {
   Serial.println("The card has already been recorded,restart to record another card");
 }
 }

Let's go to analyze those parts that we are interested in and which differ from the reading program.
We have a new boolean variable count (true or false). It will allow us to write the card.

boolean count = true;

If we analyze the end of the program, we will see the part where the writing is made.

if (count == true)
{
//Configuramos como grabador
   Serial.println("Writing...");
   byte dato1 = 0x66;
   byte dato2 = 0x66;
   byte dato3 = 0x66;
   byte dato4 = 0x66;
   byte bloque = 0x02;
   byte checksum = 0x01 + 0x06 + 0x20 + bloque + dato1 + dato2 + dato3 + dato4;
   Serial.write(0xFF);
   Serial.write(0x01);
   Serial.write(0x06);
   Serial.write(0x20);
   Serial.write(bloque);
   Serial.write(dato1);
   Serial.write(dato2);
   Serial.write(dato3);
   Serial.write(dato4);
   Serial.write(checksum);
   delay(1000);

   count = false;
   Serial.println("The card has been properly recorded");
 }
 else {
   Serial.println("The card has already been recorded,restart to record another card");
 }

At the beginning we have created four variables where we store the 4 bytes that we want and one for the block where we want to store it.

It is VERY IMPORTANT to use as writing blocks from 2 to 6 both inclusive. If the rest varies, the card may become unusable.

The configuration structure is very similar to the automatic mode. We send it the writing command 0x06, then the block and the data and finally,  the addition of all the bytes  except 0xFF.

Now that you know how to use the 125 kHz RFID module, we want to see your projects!!!

I hope it has been helpful and you enjoy it.

See you soon!!

Source: Arduteka

Leave a Reply