Step 1: Connecting The RFID Module To Arduino
Step 1: Connecting The RFID Module To Arduino
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()...).
/*
* RFID module from Libelium for Arduino
* Basic program, just Read EM1000 cards
*
* Copyright (C) 2008 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.
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 0.1
Author: Marcos Yarza
// var
int led = 13;
byte data_1 =
byte data_2 =
byte data_3 =
byte data_4 =
byte data_5 =
int val = 0;
0x00;
0x00;
0x00;
0x00;
0x00;
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();
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();
//
Serial.read();
// we
Serial.read();
// we
Serial.read();
// we
data_1 = Serial.read();
data_2 = Serial.read();
data_3 = Serial.read();
data_4 = Serial.read();
data_5 = Serial.read();
Serial.read();
// we
we read ff
read 01
read 06
read 10
// we read
// we read
// we read
// we read
// we read
read checksum
data
data
data
data
data
// Led blink
for(int i = 0;i<4;i++){
digitalWrite(led,HIGH);
delay(500);
digitalWrite(led,LOW);
delay(500);
}
1
2
3
4
5
aux_1=data/16;
aux_2=data%16;
if (aux_1<10){
Serial.print(aux_1 + 48,BYTE);
}
else{
Serial.print(aux_1+55,BYTE);
}
if (aux_2<10){
Serial.print(aux_2 + 48,BYTE);
}
else{
Serial.print(aux_2 + 55,BYTE);
}
Serial.print(" ");
}
Here is the USB output using the Arduino IDE serial port terminal
/*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
// var
int led = 13;
byte block1_byte1
byte block1_byte2
byte block1_byte3
byte block1_byte4
=
=
=
=
0x00;
0x00;
0x00;
0x00;
byte
byte
byte
byte
block2_byte1
block2_byte2
block2_byte3
block2_byte4
=
=
=
=
0x00;
0x00;
0x00;
0x00;
byte
byte
byte
byte
block3_byte1
block3_byte2
block3_byte3
block3_byte4
=
=
=
=
0x00;
0x00;
0x00;
0x00;
block5_byte1
block5_byte2
block5_byte3
block5_byte4
=
=
=
=
0x00;
0x00;
0x00;
0x00;
byte
byte
byte
byte
block6_byte1
block6_byte2
block6_byte3
block6_byte4
=
=
=
=
0x00;
0x00;
0x00;
0x00;
byte
byte
byte
byte
block7_byte1
block7_byte2
block7_byte3
block7_byte4
=
=
=
=
0x00;
0x00;
0x00;
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(500);
Serial.flush();
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
block1_byte2
block1_byte3
block1_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
1
1
1
1
byte
byte
byte
byte
1
2
3
4
block2_byte1
block2_byte2
block2_byte3
block2_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
2
2
2
2
byte
byte
byte
byte
1
2
3
4
block3_byte1
block3_byte2
block3_byte3
block3_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
3
3
3
3
byte
byte
byte
byte
1
2
3
4
block4_byte1
block4_byte2
block4_byte3
block4_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
4
4
4
4
byte
byte
byte
byte
1
2
3
4
block5_byte1
block5_byte2
block5_byte3
block5_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
5
5
5
5
byte
byte
byte
byte
1
2
3
4
block6_byte1
block6_byte2
block6_byte3
block6_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
6
6
6
6
byte
byte
byte
byte
1
2
3
4
block7_byte1
block7_byte2
block7_byte3
block7_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
7
7
7
7
byte
byte
byte
byte
1
2
3
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.print(aux_1 + 48,BYTE);
}
else{
Serial.print(aux_1+55,BYTE);
}
if (aux_2<10){
Serial.print(aux_2 + 48,BYTE);
}
else{
Serial.print(aux_2 + 55,BYTE);
}
Serial.print(" ");
}
As
you
can
Block
Block
Block
Block
Block
Block
Block 7: 00 00 00 00
see,
1:
2:
3:
4:
5:
6:
Arduino
reads
52
02
03
04
05
06
blocks
of
58
02
03
04
05
06
memory
(4
8B
02
03
04
05
06
bytes
each):
45
02
03
04
05
06
Block 0 - It should not be programmed with arbitrary values otherwise the tag may not be accessed
again.
Block 1 - This block can be used for Byte Track method. What is programmed in this block should be
traced by SM125 (typically 52 58 8B 45).
Block 7 - This block can be used as general purpose data block or can be used as password. If
password is enabled, access to the tag requires the password that matches block7 content. Once password
is enabled and if the block7 data is unknown then it is impossible to access the tag again unless with the
correct 4 byte password.
Arduino code:
/*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
// var
int led = 13;
byte
byte
byte
byte
block1_byte1
block1_byte2
block1_byte3
block1_byte4
=
=
=
=
0x00;
0x00;
0x00;
0x00;
byte
byte
byte
byte
block2_byte1
block2_byte2
block2_byte3
block2_byte4
=
=
=
=
0x00;
0x00;
0x00;
0x00;
block4_byte1
block4_byte2
block4_byte3
block4_byte4
=
=
=
=
0x00;
0x00;
0x00;
0x00;
byte
byte
byte
byte
block5_byte1
block5_byte2
block5_byte3
block5_byte4
=
=
=
=
0x00;
0x00;
0x00;
0x00;
byte
byte
byte
byte
block6_byte1
block6_byte2
block6_byte3
block6_byte4
=
=
=
=
0x00;
0x00;
0x00;
0x00;
byte
byte
byte
byte
block7_byte1
block7_byte2
block7_byte3
block7_byte4
=
=
=
=
0x00;
0x00;
0x00;
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);
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.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);
}
// 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.print(aux_1 + 48,BYTE);
}
else{
Serial.print(aux_1+55,BYTE);
}
if (aux_2<10){
Serial.print(aux_2 + 48,BYTE);
}
else{
Serial.print(aux_2 + 55,BYTE);
}
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.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);
}
// Reading T5557 tags 7 blocks
void getTag(){
val = Serial.read();
if (val > 0){
Serial.read();
Serial.read();
Serial.read();
// we read ff
// we read 01
// we read 0d
// we read 10
block1_byte1
block1_byte2
block1_byte3
block1_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
1
1
1
1
byte
byte
byte
byte
1
2
3
4
block2_byte1
block2_byte2
block2_byte3
block2_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
2
2
2
2
byte
byte
byte
byte
1
2
3
4
block3_byte1
block3_byte2
block3_byte3
block3_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
3
3
3
3
byte
byte
byte
byte
1
2
3
4
block4_byte1
block4_byte2
block4_byte3
block4_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
4
4
4
4
byte
byte
byte
byte
1
2
3
4
block5_byte1
block5_byte2
block5_byte3
block5_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
5
5
5
5
byte
byte
byte
byte
1
2
3
4
block6_byte1
block6_byte2
block6_byte3
block6_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
6
6
6
6
byte
byte
byte
byte
1
2
3
4
block7_byte1
block7_byte2
block7_byte3
block7_byte4
=
=
=
=
Serial.read();
Serial.read();
Serial.read();
Serial.read();
//
//
//
//
we
we
we
we
read
read
read
read
block
block
block
block
7
7
7
7
byte
byte
byte
byte
1
2
3
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");
}
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.
Activate
password
in
the
card
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
Writing
data
in
For writing data using password we'll use command 0x23
protected
card
protected
card
the
card
Connection
Arduino: pin 8
XBee shield: RST (pin 5)
Command to go to sleep mode:
FF 01 01 60 62
Arduino code:
/*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
// var
int led = 13;
int wake_pin = 8;
byte data_1 =
byte data_2 =
byte data_3 =
byte data_4 =
byte data_5 =
int val = 0;
0x00;
0x00;
0x00;
0x00;
0x00;
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.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);
Serial.flush();
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();
Serial.read();
Serial.read();
Serial.read();
data_1 = Serial.read();
data_2 = Serial.read();
data_3 = Serial.read();
data_4 = Serial.read();
data_5 = Serial.read();
Serial.read();
//
//
//
//
//
//
//
//
//
//
we
we
we
we
we
we
we
we
we
we
read
read
read
read
read
read
read
read
read
read
ff
01
06
10
data 1
data 2
data 3
data 4
data 5
checksum
// 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.print(aux_1 + 48,BYTE);
}
else{
Serial.print(aux_1+55,BYTE);
}
if (aux_2<10){
Serial.print(aux_2 + 48,BYTE);
}
else{
Serial.print(aux_2 + 55,BYTE);
}
Serial.print(" ");
}