Ingredients:
Preparation Time: 30 minutes
Setting up the hardware is very easy, just plug the XBee shield with the RFID module to Arduino. The jumpers in the XBee shield have to be set to XBEE position. Now you can program the Arduino and communicate it with the RFID module using the serial port (Serial.read(), Serial.print()...).
Setting up the hardware is very easy, just plug the module to Raspberry Pi using the Raspberry Pi to Arduino shields connection bridge as shown in the picture.
In this part we show an example of reading EM4100 cards. We use the Auto Read mode. The device is waiting all the time, when a card is detected, we read its code, blink the LED and print the code on the standard output.
Command |
0x87 - Set Auto Mode: EM4102 Mode - Parity decoded - Manchester RF/64 |
FF 01 09 87 01 03 02 00 10 20 30 40 37 |
Arduino code:
/* * RFID 125 kHz Module * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Marcos Yarza & Luis Martin */ // var int led = 13; byte data_1 = 0x00; byte data_2 = 0x00; byte data_3 = 0x00; byte data_4 = 0x00; byte data_5 = 0x00; int val = 0; void setup(){ // Start serial port 19200 bps Serial.begin(19200); pinMode(led, OUTPUT); delay(500); // Setting Auto Read Mode - EM4102 Decoded Mode - No password // command: FF 01 09 87 01 03 02 00 10 20 30 40 37 Serial.write(0xFF); Serial.write(0x01); Serial.write(0x09); Serial.write(0x87); Serial.write(0x01); Serial.write(0x03); Serial.write(0x02); Serial.write((byte)0x00); Serial.write(0x10); Serial.write(0x20); Serial.write(0x30); Serial.write(0x40); Serial.write(0x37); delay(500); while (Serial.available() >0) { Serial.read(); } Serial.println(); Serial.println("RFID module started in Auto Read Mode"); } void loop(){ val = Serial.read(); while (val != 0xff){ Serial.println("Waiting card"); val = Serial.read(); delay(1000); } // Serial.read(); // we read ff Serial.read(); // we read 01 Serial.read(); // we read 06 Serial.read(); // we read 10 data_1 = Serial.read(); // we read data 1 data_2 = Serial.read(); // we read data 2 data_3 = Serial.read(); // we read data 3 data_4 = Serial.read(); // we read data 4 data_5 = Serial.read(); // we read data 5 Serial.read(); // we read checksum // Led blink for(int i = 0;i<4;i++){ digitalWrite(led,HIGH); delay(500); digitalWrite(led,LOW); delay(500); } // Printing the code of the card Serial.println(); Serial.print("EM4100 card found - Code: "); writeByte(data_1); writeByte(data_2); writeByte(data_3); writeByte(data_4); writeByte(data_5); Serial.println(); Serial.println(); } //Write a byte (hex) in ASCII void writeByte(byte data){ int aux_1 = 0; int aux_2 = 0; aux_1=data/16; aux_2=data%16; if (aux_1<10){ Serial.write(aux_1 + 48); } else{ Serial.write(aux_1+55); } if (aux_2<10){ Serial.write(aux_2 + 48); } else{ Serial.write(aux_2 + 55); } Serial.print(" "); }
Raspberry Pi code:
/* * RFID 125 kHz Module * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Marcos Yarza & Luis Martin */ //Include ArduPi library #include "arduPi.h" int led = 13; byte data_1 = 0x00; byte data_2 = 0x00; byte data_3 = 0x00; byte data_4 = 0x00; byte data_5 = 0x00; int val = 0; void setup(){ // Start serial port 19200 bps Serial.begin(19200); pinMode(led, OUTPUT); delay(500); // Setting Auto Read Mode - EM4102 Decoded Mode - No password // command: FF 01 09 87 01 03 02 00 10 20 30 40 37 Serial.print(0xFF,BYTE); Serial.print(0x01,BYTE); Serial.print(0x09,BYTE); Serial.print(0x87,BYTE); Serial.print(0x01,BYTE); Serial.print(0x03,BYTE); Serial.print(0x02,BYTE); Serial.print(0x00,BYTE); Serial.print(0x10,BYTE); Serial.print(0x20,BYTE); Serial.print(0x30,BYTE); Serial.print(0x40,BYTE); Serial.print(0x37,BYTE); delay(500); Serial.flush(); printf("\n"); printf("RFID module started in Auto Read Mode\n"); } void loop(){ printf("Waiting card...\n"); val = Serial.read(); while (val != 0xff){ val = Serial.read(); delay(1000); } // Serial.read(); // we read ff Serial.read(); // we read 01 Serial.read(); // we read 06 Serial.read(); // we read 10 data_1 = Serial.read(); // we read data 1 data_2 = Serial.read(); // we read data 2 data_3 = Serial.read(); // we read data 3 data_4 = Serial.read(); // we read data 4 data_5 = Serial.read(); // we read data 5 Serial.read(); // we read checksum // Led blink for(int i = 0;i < 4;i++){ digitalWrite(led,HIGH); delay(500); digitalWrite(led,LOW); delay(500); } // Printing the code of the card printf("\n"); printf("EM4100 card found - Code: "); printf("%x",data_1); printf("%x",data_2); printf("%x",data_3); printf("%x",data_4); printf("%x",data_5); printf("\n\n"); } int main (){ setup(); while(1){ loop(); } return (0); }
Here is the USB output using the Arduino IDE serial port terminal
The next example of use of the RFID module is the reading of T5557 rewritable cards.
Command |
0x87 - Set Auto Mode: Byte track mode - Parity decoded - Manchester RF/64, 7 blocks, without password. |
FF 01 09 87 01 01 07 00 10 20 30 40 3A |
Arduino code:
/* * RFID 125 kHz Module * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Marcos Yarza & Luis Martin */ // var int led = 13; 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); pinMode(led, OUTPUT); delay(500); // Setting Auto Read Mode - T5557 7 blocks 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); while (Serial.available() >0) { Serial.read(); } Serial.println(); Serial.println(); Serial.println("RFID module started in Auto Read Mode"); } void loop(){ val = Serial.read(); while (val != 0xff){ Serial.println("Waiting card"); val = Serial.read(); delay(1000); } // Serial.read(); // we read ff Serial.read(); // we read 01 Serial.read(); // we read 1d Serial.read(); // we read 10 block1_byte1 = Serial.read(); // we read block 1 byte 1 block1_byte2 = Serial.read(); // we read block 1 byte 2 block1_byte3 = Serial.read(); // we read block 1 byte 3 block1_byte4 = Serial.read(); // we read block 1 byte 4 block2_byte1 = Serial.read(); // we read block 2 byte 1 block2_byte2 = Serial.read(); // we read block 2 byte 2 block2_byte3 = Serial.read(); // we read block 2 byte 3 block2_byte4 = Serial.read(); // we read block 2 byte 4 block3_byte1 = Serial.read(); // we read block 3 byte 1 block3_byte2 = Serial.read(); // we read block 3 byte 2 block3_byte3 = Serial.read(); // we read block 3 byte 3 block3_byte4 = Serial.read(); // we read block 3 byte 4 block4_byte1 = Serial.read(); // we read block 4 byte 1 block4_byte2 = Serial.read(); // we read block 4 byte 2 block4_byte3 = Serial.read(); // we read block 4 byte 3 block4_byte4 = Serial.read(); // we read block 4 byte 4 block5_byte1 = Serial.read(); // we read block 5 byte 1 block5_byte2 = Serial.read(); // we read block 5 byte 2 block5_byte3 = Serial.read(); // we read block 5 byte 3 block5_byte4 = Serial.read(); // we read block 5 byte 4 block6_byte1 = Serial.read(); // we read block 6 byte 1 block6_byte2 = Serial.read(); // we read block 6 byte 2 block6_byte3 = Serial.read(); // we read block 6 byte 3 block6_byte4 = Serial.read(); // we read block 6 byte 4 block7_byte1 = Serial.read(); // we read block 7 byte 1 block7_byte2 = Serial.read(); // we read block 7 byte 2 block7_byte3 = Serial.read(); // we read block 7 byte 3 block7_byte4 = Serial.read(); // we read block 7 byte 4 Serial.read(); // we read checksum // Led blink for(int i = 0;i<4;i++){ digitalWrite(led,HIGH); delay(500); digitalWrite(led,LOW); delay(500); } // Printing the code of the card Serial.println(); Serial.println("T5557 card found - Data read: "); Serial.println("---------------------------------------"); Serial.print("-- Block 1 -- | "); writeByte(block1_byte1); Serial.print(" | "); writeByte(block1_byte2); Serial.print(" | "); writeByte(block1_byte3); Serial.print(" | "); writeByte(block1_byte4); Serial.println(" |"); Serial.print("-- Block 2 -- | "); writeByte(block2_byte1); Serial.print(" | "); writeByte(block2_byte2); Serial.print(" | "); writeByte(block2_byte3); Serial.print(" | "); writeByte(block2_byte4); Serial.println(" |"); Serial.print("-- Block 3 -- | "); writeByte(block3_byte1); Serial.print(" | "); writeByte(block3_byte2); Serial.print(" | "); writeByte(block3_byte3); Serial.print(" | "); writeByte(block3_byte4); Serial.println(" |"); Serial.print("-- Block 4 -- | "); writeByte(block4_byte1); Serial.print(" | "); writeByte(block4_byte2); Serial.print(" | "); writeByte(block4_byte3); Serial.print(" | "); writeByte(block4_byte4); Serial.println(" |"); Serial.print("-- Block 5 -- | "); writeByte(block5_byte1); Serial.print(" | "); writeByte(block5_byte2); Serial.print(" | "); writeByte(block5_byte3); Serial.print(" | "); writeByte(block5_byte4); Serial.println(" |"); Serial.print("-- Block 6 -- | "); writeByte(block6_byte1); Serial.print(" | "); writeByte(block6_byte2); Serial.print(" | "); writeByte(block6_byte3); Serial.print(" | "); writeByte(block6_byte4); Serial.println(" |"); Serial.print("-- Block 7 -- | "); writeByte(block7_byte1); Serial.print(" | "); writeByte(block7_byte2); Serial.print(" | "); writeByte(block7_byte3); Serial.print(" | "); writeByte(block7_byte4); Serial.println(" |"); Serial.println("---------------------------------------"); Serial.println(); } //Write a byte (hex) in ASCII void writeByte(byte data){ int aux_1 = 0; int aux_2 = 0; aux_1=data/16; aux_2=data%16; if (aux_1<10){ Serial.write(aux_1 + 48); } else{ Serial.write(aux_1+55); } if (aux_2<10){ Serial.write(aux_2 + 48); } else{ Serial.write(aux_2 + 55); } Serial.print(" "); }
/* * RFID 125 kHz Module * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Marcos Yarza & Luis Martin */ //Include ArduPi library #include "arduPi.h" void writeByte(byte data); int led = 13; 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); pinMode(led, OUTPUT); delay(500); // Setting Auto Read Mode - T5557 7 blocks without password Serial.print(0xFF,BYTE); Serial.print(0x01,BYTE); Serial.print(0x09,BYTE); Serial.print(0x87,BYTE); Serial.print(0x01,BYTE); Serial.print(0x01,BYTE); Serial.print(0x07,BYTE); Serial.print(0x00,BYTE); Serial.print(0x10,BYTE); Serial.print(0x20,BYTE); Serial.print(0x30,BYTE); Serial.print(0x40,BYTE); Serial.print(0x3A,BYTE); delay(200); Serial.flush(); printf("RFID module started in Auto Read Mode\n"); } void loop(){ val = Serial.read(); while (val != 0xff){ val = Serial.read(); delay(200); } // Serial.read(); // we read ff Serial.read(); // we read 01 Serial.read(); // we read 1d Serial.read(); // we read 10 block1_byte1 = Serial.read(); // we read block 1 byte 1 block1_byte2 = Serial.read(); // we read block 1 byte 2 block1_byte3 = Serial.read(); // we read block 1 byte 3 block1_byte4 = Serial.read(); // we read block 1 byte 4 block2_byte1 = Serial.read(); // we read block 2 byte 1 block2_byte2 = Serial.read(); // we read block 2 byte 2 block2_byte3 = Serial.read(); // we read block 2 byte 3 block2_byte4 = Serial.read(); // we read block 2 byte 4 block3_byte1 = Serial.read(); // we read block 3 byte 1 block3_byte2 = Serial.read(); // we read block 3 byte 2 block3_byte3 = Serial.read(); // we read block 3 byte 3 block3_byte4 = Serial.read(); // we read block 3 byte 4 block4_byte1 = Serial.read(); // we read block 4 byte 1 block4_byte2 = Serial.read(); // we read block 4 byte 2 block4_byte3 = Serial.read(); // we read block 4 byte 3 block4_byte4 = Serial.read(); // we read block 4 byte 4 block5_byte1 = Serial.read(); // we read block 5 byte 1 block5_byte2 = Serial.read(); // we read block 5 byte 2 block5_byte3 = Serial.read(); // we read block 5 byte 3 block5_byte4 = Serial.read(); // we read block 5 byte 4 block6_byte1 = Serial.read(); // we read block 6 byte 1 block6_byte2 = Serial.read(); // we read block 6 byte 2 block6_byte3 = Serial.read(); // we read block 6 byte 3 block6_byte4 = Serial.read(); // we read block 6 byte 4 block7_byte1 = Serial.read(); // we read block 7 byte 1 block7_byte2 = Serial.read(); // we read block 7 byte 2 block7_byte3 = Serial.read(); // we read block 7 byte 3 block7_byte4 = Serial.read(); // we read block 7 byte 4 Serial.read(); // we read checksum // Led blink for(int i = 0;i < 4;i++){ digitalWrite(led,HIGH); delay(200); digitalWrite(led,LOW); delay(200); } // Printing the code of the card printf("\n"); printf("T5557 card found - Data read: \n"); printf("---------------------------------------\n"); printf("-- Block 1 -- | "); writeByte(block1_byte1); printf(" | "); writeByte(block1_byte2); printf(" | "); writeByte(block1_byte3); printf(" | "); writeByte(block1_byte4); printf(" |\n"); printf("-- Block 2 -- | "); writeByte(block2_byte1); printf(" | "); writeByte(block2_byte2); printf(" | "); writeByte(block2_byte3); printf(" | "); writeByte(block2_byte4); printf(" |\n"); printf("-- Block 3 -- | "); writeByte(block3_byte1); printf(" | "); writeByte(block3_byte2); printf(" | "); writeByte(block3_byte3); printf(" | "); writeByte(block3_byte4); printf(" |\n"); printf("-- Block 4 -- | "); writeByte(block4_byte1); printf(" | "); writeByte(block4_byte2); printf(" | "); writeByte(block4_byte3); printf(" | "); writeByte(block4_byte4); printf(" |\n"); printf("-- Block 5 -- | "); writeByte(block5_byte1); printf(" | "); writeByte(block5_byte2); printf(" | "); writeByte(block5_byte3); printf(" | "); writeByte(block5_byte4); printf(" |\n"); printf("-- Block 6 -- | "); writeByte(block6_byte1); printf(" | "); writeByte(block6_byte2); printf(" | "); writeByte(block6_byte3); printf(" | "); writeByte(block6_byte4); printf(" |\n"); printf("-- Block 7 -- | "); writeByte(block7_byte1); printf(" | "); writeByte(block7_byte2); printf(" | "); writeByte(block7_byte3); printf(" | "); writeByte(block7_byte4); printf(" |\n"); printf("---------------------------------------\n"); printf("Waiting card...\n"); } //Write a byte (hex) in ASCII void writeByte(byte data){ int aux_1 = 0; int aux_2 = 0; aux_1=data/16; aux_2=data%16; if (aux_1 < 10){ printf("%x",aux_1 + 48); } else{ printf("%x",aux_1+55); } if (aux_2 < 10){ printf("%x",aux_2 + 48); } else{ printf("%x",aux_2 + 55); } printf(" "); } int main (){ setup(); while(1){ loop(); } return (0); }
Serial output (Arduino Serial Monitor)
As you can see, Arduino reads 7 blocks of memory (4 bytes each):
Block 1: 52 58 8B 45
Block 2: 02 02 02 02
Block 3: 03 03 03 03
Block 4: 04 04 04 04
Block 5: 05 05 05 05
Block 6: 06 06 06 06
Block 7: 00 00 00 00
The next example for this module is the writing/reading of RFID cards. We program the device to read a T5557 card, then it writes blocks 2, 3, 4, 5 and 6 of the card with an incremental value each time.
Command |
0x10 - Read Tag: Byte track Mode - Manchester RF/64, 7 Blocks |
0x40 - Configure Tag programming parameters: |
WG = 50 (0x32) |
SG = 100 0x64) |
ONE = 90 (0x5A) |
ZERO = 30 (0x1E) |
PADF = 96 (0x60) |
0x20 - Write Tag |
Warning!!!
Don't write block 0, 1 or 7 if you are not an advanced user!!!
Arduino code:
/* * RFID 125 kHz Module * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Marcos Yarza & Luis Martin */ // var int led = 13; 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; byte valueToStore = 0x00; void setup(){ // Start serial port 19200 bps Serial.begin(19200); pinMode(led, OUTPUT); delay(500); // Configure Tag programming parameters // Command: FF 01 06 40 32 64 5A 1E 60 B5 Serial.write(0xFF); Serial.write(0x01); Serial.write(0x06); Serial.write(0x40); Serial.write(0x32); Serial.write(0x64); Serial.write(0x5A); Serial.write(0x1E); Serial.write(0x60); Serial.write(0xB5); delay(500); Serial.flush(); } void loop(){ // Read the card: Byte track Mode - Manchester RF/64 7 blocks readTag(0x01,0x07); digitalWrite(led,HIGH); Serial.println("Waiting card to read"); Serial.flush(); delay(5000); digitalWrite(led,LOW); getTag(); // Add 1 to the value to store in card if (valueToStore == 0xFF){ valueToStore = 0x00; } else { valueToStore++; } delay(5000); // Write card Serial.println("Waiting card to write ..."); Serial.println("Put the card on the reader..."); digitalWrite(led,HIGH); delay(5000); Serial.print("Writing: "); writeByte(valueToStore); Serial.println(); // We write blocks 2, 3, 4, 5 and 6 // Warning !!! // don't write block 1 or 7 if you are not an advanced user!!! writeTag(0x02,valueToStore,valueToStore,valueToStore,valueToStore); writeTag(0x03,valueToStore,valueToStore,valueToStore,valueToStore); writeTag(0x04,valueToStore,valueToStore,valueToStore,valueToStore); writeTag(0x05,valueToStore,valueToStore,valueToStore,valueToStore); writeTag(0x06,valueToStore,valueToStore,valueToStore,valueToStore); Serial.println(); Serial.println("Card ready!!!"); delay(5000); } // Command for read a Tag void readTag(byte modeRead, byte blocks){ // Read command: FF 01 03 10 02 02 18 byte checksum = 0x01 + 0x03 + 0x10 + modeRead + blocks; Serial.write(0xFF); Serial.write(0x01); Serial.write(0x03); Serial.write(0x10); Serial.write(modeRead); Serial.write(blocks); Serial.write(checksum); delay(200); while (Serial.available() > 0){ Serial.read(); } } //Write a byte (hex) in ASCII void writeByte(byte data){ int aux_1 = 0; int aux_2 = 0; aux_1=data/16; aux_2=data%16; if (aux_1<10){ Serial.write(aux_1 + 48); } else{ Serial.write(aux_1+55); } if (aux_2<10){ Serial.write(aux_2 + 48); } else{ Serial.write(aux_2 + 55); } Serial.print(" "); } void writeTag(byte block, byte data0, byte data1, byte data2, byte data3){ byte checksum = 0x01 + 0x06 + 0x20 + block + data0 + data1 + data2 + data3; Serial.write(0xFF); Serial.write(0x01); Serial.write(0x06); Serial.write(0x20); Serial.write(block); Serial.write(data0); Serial.write(data1); Serial.write(data2); Serial.write(data3); Serial.write(checksum); delay(2000); while (Serial.available() > 0){ Serial.read(); } } // Reading T5557 tags 7 blocks void getTag(){ val = Serial.read(); // we read ff if (val > 0){ Serial.read(); // we read 01 Serial.read(); // we read 0d Serial.read(); // we read 10 block1_byte1 = Serial.read(); // we read block 1 byte 1 block1_byte2 = Serial.read(); // we read block 1 byte 2 block1_byte3 = Serial.read(); // we read block 1 byte 3 block1_byte4 = Serial.read(); // we read block 1 byte 4 block2_byte1 = Serial.read(); // we read block 2 byte 1 block2_byte2 = Serial.read(); // we read block 2 byte 2 block2_byte3 = Serial.read(); // we read block 2 byte 3 block2_byte4 = Serial.read(); // we read block 2 byte 4 block3_byte1 = Serial.read(); // we read block 3 byte 1 block3_byte2 = Serial.read(); // we read block 3 byte 2 block3_byte3 = Serial.read(); // we read block 3 byte 3 block3_byte4 = Serial.read(); // we read block 3 byte 4 block4_byte1 = Serial.read(); // we read block 4 byte 1 block4_byte2 = Serial.read(); // we read block 4 byte 2 block4_byte3 = Serial.read(); // we read block 4 byte 3 block4_byte4 = Serial.read(); // we read block 4 byte 4 block5_byte1 = Serial.read(); // we read block 5 byte 1 block5_byte2 = Serial.read(); // we read block 5 byte 2 block5_byte3 = Serial.read(); // we read block 5 byte 3 block5_byte4 = Serial.read(); // we read block 5 byte 4 block6_byte1 = Serial.read(); // we read block 6 byte 1 block6_byte2 = Serial.read(); // we read block 6 byte 2 block6_byte3 = Serial.read(); // we read block 6 byte 3 block6_byte4 = Serial.read(); // we read block 6 byte 4 block7_byte1 = Serial.read(); // we read block 7 byte 1 block7_byte2 = Serial.read(); // we read block 7 byte 2 block7_byte3 = Serial.read(); // we read block 7 byte 3 block7_byte4 = Serial.read(); // we read block 7 byte 4 Serial.read(); // we read checksum // Led blink for(int i = 0;i<4;i++){ digitalWrite(led,HIGH); delay(500); digitalWrite(led,LOW); delay(500); } // Printing the code of the card Serial.println(); Serial.println("T5557 card found - Data read: "); Serial.println("---------------------------------------"); Serial.print("-- Block 1 -- | "); writeByte(block1_byte1); Serial.print(" | "); writeByte(block1_byte2); Serial.print(" | "); writeByte(block1_byte3); Serial.print(" | "); writeByte(block1_byte4); Serial.println(" |"); Serial.print("-- Block 2 -- | "); writeByte(block2_byte1); Serial.print(" | "); writeByte(block2_byte2); Serial.print(" | "); writeByte(block2_byte3); Serial.print(" | "); writeByte(block2_byte4); Serial.println(" |"); Serial.print("-- Block 3 -- | "); writeByte(block3_byte1); Serial.print(" | "); writeByte(block3_byte2); Serial.print(" | "); writeByte(block3_byte3); Serial.print(" | "); writeByte(block3_byte4); Serial.println(" |"); Serial.print("-- Block 4 -- | "); writeByte(block4_byte1); Serial.print(" | "); writeByte(block4_byte2); Serial.print(" | "); writeByte(block4_byte3); Serial.print(" | "); writeByte(block4_byte4); Serial.println(" |"); Serial.print("-- Block 5 -- | "); writeByte(block5_byte1); Serial.print(" | "); writeByte(block5_byte2); Serial.print(" | "); writeByte(block5_byte3); Serial.print(" | "); writeByte(block5_byte4); Serial.println(" |"); Serial.print("-- Block 6 -- | "); writeByte(block6_byte1); Serial.print(" | "); writeByte(block6_byte2); Serial.print(" | "); writeByte(block6_byte3); Serial.print(" | "); writeByte(block6_byte4); Serial.println(" |"); Serial.print("-- Block 7 -- | "); writeByte(block7_byte1); Serial.print(" | "); writeByte(block7_byte2); Serial.print(" | "); writeByte(block7_byte3); Serial.print(" | "); writeByte(block7_byte4); Serial.println(" |"); Serial.println("---------------------------------------"); Serial.println(); } else Serial.println("No card"); }
//Include ArduPi library #include "arduPi.h" void writeByte(byte data); void readTag(byte modeRead, byte blocks); void writeTag(byte block, byte data0, byte data1, byte data2, byte data3); void getTag(); int led = 13; 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; byte valueToStore = 0x00; void setup(){ // Start serial port 19200 bps Serial.begin(19200); pinMode(led, OUTPUT); delay(500); // Configure Tag programming parameters // Command: FF 01 06 40 32 64 5A 1E 60 B5 Serial.print(0xFF,BYTE); Serial.print(0x01,BYTE); Serial.print(0x06,BYTE); Serial.print(0x40,BYTE); Serial.print(0x32,BYTE); Serial.print(0x64,BYTE); Serial.print(0x5A,BYTE); Serial.print(0x1E,BYTE); Serial.print(0x60,BYTE); Serial.print(0xB5,BYTE); delay(500); Serial.flush(); } void loop(){ // Read the card: Byte track Mode - Manchester RF/64 7 blocks readTag(0x01,0x07); digitalWrite(led,HIGH); printf("Waiting card to read\n"); Serial.flush(); delay(5000); digitalWrite(led,LOW); getTag(); // Add 1 to the value to store in card if (valueToStore == 0xFF){ valueToStore = 0x00; } else { valueToStore++; } delay(5000); // Write card printf("Waiting card to write ..."); printf("Put the card on the reader...\n"); digitalWrite(led,HIGH); delay(5000); printf("Writing: "); writeByte(valueToStore); printf("\n"); // We write blocks 2, 3, 4, 5 and 6 // Warning !!! // don't write block 1 or 7 if you are not an advanced user!!! writeTag(0x02,valueToStore,valueToStore,valueToStore,valueToStore); writeTag(0x03,valueToStore,valueToStore,valueToStore,valueToStore); writeTag(0x04,valueToStore,valueToStore,valueToStore,valueToStore); writeTag(0x05,valueToStore,valueToStore,valueToStore,valueToStore); writeTag(0x06,valueToStore,valueToStore,valueToStore,valueToStore); printf("\n"); Serial.println("Card ready!!!"); delay(5000); } // Command for read a Tag void readTag(byte modeRead, byte blocks){ // Read command: FF 01 03 10 02 02 18 byte checksum = 0x01 + 0x03 + 0x10 + modeRead + blocks; Serial.print(0xFF,BYTE); Serial.print(0x01,BYTE); Serial.print(0x03,BYTE); Serial.print(0x10,BYTE); Serial.print(modeRead,BYTE); Serial.print(blocks,BYTE); Serial.print(checksum,BYTE); delay(200); while (Serial.available() > 0){ Serial.read(); } } // Write a byte (hex) in ASCII void writeByte(byte data){ int aux_1 = 0; int aux_2 = 0; aux_1=data/16; aux_2=data%16; if (aux_1 < 10){ printf("%x",aux_1 + 48,BYTE); } else{ printf("%x",aux_1+55,BYTE); } if (aux_2 < 10){ printf("%x",aux_2 + 48,BYTE); } else{ printf("%x",aux_2 + 55,BYTE); } printf(" "); } void writeTag(byte block, byte data0, byte data1, byte data2, byte data3){ byte checksum = 0x01 + 0x06 + 0x20 + block + data0 + data1 + data2 + data3; Serial.print(0xFF,BYTE); Serial.print(0x01,BYTE); Serial.print(0x06,BYTE); Serial.print(0x20,BYTE); Serial.print(block,BYTE); Serial.print(data0,BYTE); Serial.print(data1,BYTE); Serial.print(data2,BYTE); Serial.print(data3,BYTE); Serial.print(checksum,BYTE); delay(2000); while (Serial.available() > 0){ Serial.read(); } } // Reading T5557 tags 7 blocks void getTag(){ val = Serial.read(); // we read ff if (val > 0){ Serial.read(); // we read 01 Serial.read(); // we read 0d Serial.read(); // we read 10 block1_byte1 = Serial.read(); // we read block 1 byte 1 block1_byte2 = Serial.read(); // we read block 1 byte 2 block1_byte3 = Serial.read(); // we read block 1 byte 3 block1_byte4 = Serial.read(); // we read block 1 byte 4 block2_byte1 = Serial.read(); // we read block 2 byte 1 block2_byte2 = Serial.read(); // we read block 2 byte 2 block2_byte3 = Serial.read(); // we read block 2 byte 3 block2_byte4 = Serial.read(); // we read block 2 byte 4 block3_byte1 = Serial.read(); // we read block 3 byte 1 block3_byte2 = Serial.read(); // we read block 3 byte 2 block3_byte3 = Serial.read(); // we read block 3 byte 3 block3_byte4 = Serial.read(); // we read block 3 byte 4 block4_byte1 = Serial.read(); // we read block 4 byte 1 block4_byte2 = Serial.read(); // we read block 4 byte 2 block4_byte3 = Serial.read(); // we read block 4 byte 3 block4_byte4 = Serial.read(); // we read block 4 byte 4 block5_byte1 = Serial.read(); // we read block 5 byte 1 block5_byte2 = Serial.read(); // we read block 5 byte 2 block5_byte3 = Serial.read(); // we read block 5 byte 3 block5_byte4 = Serial.read(); // we read block 5 byte 4 block6_byte1 = Serial.read(); // we read block 6 byte 1 block6_byte2 = Serial.read(); // we read block 6 byte 2 block6_byte3 = Serial.read(); // we read block 6 byte 3 block6_byte4 = Serial.read(); // we read block 6 byte 4 block7_byte1 = Serial.read(); // we read block 7 byte 1 block7_byte2 = Serial.read(); // we read block 7 byte 2 block7_byte3 = Serial.read(); // we read block 7 byte 3 block7_byte4 = Serial.read(); // we read block 7 byte 4 Serial.read(); // we read checksum // Led blink for(int i = 0;i < 4;i++){ digitalWrite(led,HIGH); delay(500); digitalWrite(led,LOW); delay(500); } // Printing the code of the card printf("\n"); printf("T5557 card found - Data read: \n"); printf("---------------------------------------\n"); printf("-- Block 1 -- | "); writeByte(block1_byte1); printf(" | "); writeByte(block1_byte2); printf(" | "); writeByte(block1_byte3); printf(" | "); writeByte(block1_byte4); printf(" |\n"); printf("-- Block 2 -- | "); writeByte(block2_byte1); printf(" | "); writeByte(block2_byte2); printf(" | "); writeByte(block2_byte3); printf(" | "); writeByte(block2_byte4); printf(" |\n"); printf("-- Block 3 -- | "); writeByte(block3_byte1); printf(" | "); writeByte(block3_byte2); printf(" | "); writeByte(block3_byte3); printf(" | "); writeByte(block3_byte4); printf(" |\n"); printf("-- Block 4 -- | "); writeByte(block4_byte1); printf(" | "); writeByte(block4_byte2); printf(" | "); writeByte(block4_byte3); printf(" | "); writeByte(block4_byte4); printf(" |\n"); printf("-- Block 5 -- | "); writeByte(block5_byte1); printf(" | "); writeByte(block5_byte2); printf(" | "); writeByte(block5_byte3); printf(" | "); writeByte(block5_byte4); printf(" |\n"); printf("-- Block 6 -- | "); writeByte(block6_byte1); printf(" | "); writeByte(block6_byte2); printf(" | "); writeByte(block6_byte3); printf(" | "); writeByte(block6_byte4); printf(" |\n"); printf("-- Block 7 -- | "); writeByte(block7_byte1); printf(" | "); writeByte(block7_byte2); printf(" | "); writeByte(block7_byte3); printf(" | "); writeByte(block7_byte4); printf(" |\n"); printf("---------------------------------------\n"); } else printf("No card\n"); } int main (){ setup(); while(1){ loop(); } return (0); }
Serial monitor output
Warning!!!
Once password is enabled, if the block 7 data is unknown then it is impossible to access the card again unless with the correct 4 byte password.
For activating the password, we'll program the block 0, we have to be careful programming this block otherwise the tag may not be accessed again.
First of all, we have to discover the password (4 bytes), so we have to read the block 7 in the card (see the above example).
Password for our card: |
00 00 00 00 |
Once we know the password of the card we can activate it using this command
Command for activating the password in the card |
FF 01 0A 23 00 00 14 80 F0 XX XX XX XX CHECKSUM |
XX XX XX XX => Password |
For writing data using password we'll use command 0x23
Write with pass command |
FF 01 0A 23 BLOCK DATA0 DATA1 DATA2 DATA3 XX XX XX XX CHECKSUM |
BLOCK: Block to write (0x02, 0x03,0x04, 0x05, 0x06 or 0x07) |
DATA0, DATA1, DATA2, DATA3: Data bytes to store (4 bytes) |
XX XX XX XX: Password (4 bytes) |
For reading data in a protected card, we'll use command 0x13
Read with pass command |
FF 01 07 13 01 BLOCKS XX XX XX XX CHECKSUM |
BLOCKS: Blocks to read (1-7) |
XX XX XX XX: Pasword |
We also could use Auto read mode)
Just sending this command you can deactivate the password in the card.
FF 01 0A 23 00 00 14 80 E0 XX XX XX XX CHECKSUM |
XX XX XX XX => Password |
An advantage of our RFID module is that it has got a sleep mode that let us save power hen we don't use it. Go to the sleep mode is very easy, just sending a command. When the RFID is in sleeping mode and we want to wake up it we need to provide it a HIGH (5V) pulse to the reset pin. We'll use one of the Arduino/Raspberry Pi outputs to control the reset signal. For making the connection we'll use a cable from output 8 to RST pin (look picture).
Connection |
Arduino: pin 8 |
RFID module: RST (pin 5) |
Command to go to sleep mode: |
FF 01 01 60 62 |
Arduino code:
/* * RFID 125 kHz Module * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Marcos Yarza & Luis Martin */ // var int led = 13; int wake_pin = 8; byte data_1 = 0x00; byte data_2 = 0x00; byte data_3 = 0x00; byte data_4 = 0x00; byte data_5 = 0x00; int val = 0; void setup(){ // Start serial port 19200 bps Serial.begin(19200); pinMode(led, OUTPUT); pinMode(wake_pin, OUTPUT); delay(500); Serial.flush(); Serial.println(); Serial.println("RFID module started in Auto Read Mode"); } void loop(){ // Setting Auto Read Mode - EM4102 Decoded Mode - No password // command: FF 01 09 87 01 03 02 00 10 20 30 40 37 Serial.write(0xFF); Serial.write(0x01); Serial.write(0x09); Serial.write(0x87); Serial.write(0x01); Serial.write(0x03); Serial.write(0x02); Serial.write((byte)0x00); Serial.write(0x10); Serial.write(0x20); Serial.write(0x30); Serial.write(0x40); Serial.write(0x37); delay(500); while (Serial.available() >0) { Serial.read(); } digitalWrite(led, HIGH); digitalWrite(wake_pin, LOW); // wait for Tag val = Serial.read(); while (val != 0xff){ Serial.println("Waiting card"); val = Serial.read(); delay(1000); } // Serial.read(); // we read ff Serial.read(); // we read 01 Serial.read(); // we read 06 Serial.read(); // we read 10 data_1 = Serial.read(); // we read data 1 data_2 = Serial.read(); // we read data 2 data_3 = Serial.read(); // we read data 3 data_4 = Serial.read(); // we read data 4 data_5 = Serial.read(); // we read data 5 Serial.read(); // we read checksum // Printing the code of the card Serial.println(); Serial.print("EM4100 card found - Code: "); writeByte(data_1); writeByte(data_2); writeByte(data_3); writeByte(data_4); writeByte(data_5); Serial.println(); Serial.println(); digitalWrite(led,LOW); // go to sleep mode for 25 seconds Serial.println("Go to sleep mode for 25s ..."); // Sleep command Serial.write(0xFF); Serial.write(0x01); Serial.write(0x01); Serial.write(0x60); Serial.write(0x62); // wait for 25 sec delay(25000); // wake up digitalWrite(wake_pin,HIGH); delay(500); digitalWrite(wake_pin,LOW); } //Write a byte (hex) in ASCII void writeByte(byte data){ int aux_1 = 0; int aux_2 = 0; aux_1=data/16; aux_2=data%16; if (aux_1<10){ Serial.write(aux_1 + 48); } else{ Serial.write(aux_1+55); } if (aux_2<10){ Serial.write(aux_2 + 48); } else{ Serial.write(aux_2 + 55); } Serial.print(" "); }
/* * RFID 125 kHz Module * * Copyright (C) Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * a * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * * Version: 2.0 * Design: David Gascón * Implementation: Marcos Yarza & Luis Martin */ // var #include "arduPi.h" int led = 4; int wake_pin = 9; byte data_1 = 0x00; byte data_2 = 0x00; byte data_3 = 0x00; byte data_4 = 0x00; byte data_5 = 0x00; int val = 0; void setup(){ // Start serial port 19200 bps Serial.begin(19200); pinMode(led, OUTPUT); pinMode(wake_pin, OUTPUT); delay(500); Serial.flush(); printf("RFID module started in Auto Read Mode\n"); printf("Waiting card...\n"); } void loop(){ // Setting Auto Read Mode - EM4102 Decoded Mode - No password // command: FF 01 09 87 01 03 02 00 10 20 30 40 37 Serial.print(0xFF,BYTE); Serial.print(0x01,BYTE); Serial.print(0x09,BYTE); Serial.print(0x87,BYTE); Serial.print(0x01,BYTE); Serial.print(0x03,BYTE); Serial.print(0x02,BYTE); Serial.print(0x00,BYTE); Serial.print(0x10,BYTE); Serial.print(0x20,BYTE); Serial.print(0x30,BYTE); Serial.print(0x40,BYTE); Serial.print(0x37,BYTE); delay(500); while (Serial.available() >0) { Serial.read(); } digitalWrite(led, HIGH); digitalWrite(wake_pin, LOW); // wait for Tag val = Serial.read(); while (val != 0xff){ val = Serial.read(); delay(1000); } // Serial.read(); // we read ff Serial.read(); // we read 01 Serial.read(); // we read 06 Serial.read(); // we read 10 data_1 = Serial.read(); // we read data 1 data_2 = Serial.read(); // we read data 2 data_3 = Serial.read(); // we read data 3 data_4 = Serial.read(); // we read data 4 data_5 = Serial.read(); // we read data 5 Serial.read(); // we read checksum // Printing the code of the card printf("\nEM4100 card found - Code: \n"); printf("%x",data_1); printf("%x",data_2); printf("%x",data_3); printf("%x",data_4); printf("%x",data_5); printf("\n\n"); digitalWrite(led,LOW); // go to sleep mode for 25 seconds printf("Go to sleep mode for 25s ...\n"); // Sleep command Serial.print(0xFF,BYTE); Serial.print(0x01,BYTE); Serial.print(0x01,BYTE); Serial.print(0x60,BYTE); Serial.print(0x62,BYTE); // wait for 25 sec delay(25000); // wake up digitalWrite(wake_pin,HIGH); delay(500); digitalWrite(wake_pin,LOW); printf("Waiting card\n"); } int main(){ setup(); while(1){ loop(); } return 0; }
Here's an explanatory video, which shows the whole process developed in this tutorial:
Download Fritzing Arduino RFID 125kHz
RFID 125kHz Module for Arduino can be connected to Arduino using a XBee shield and will communicate it using the Arduino serial port (UART).
You can download our Fritzing libraries from this area.
If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.
Know more at:
Get the Starter Kits at: