Code for using the Arduino with the Parallax RFID reader/writer

This page is specifically for help with interfacing the Arduino with the newer, black read/write RFID modules from Parallax. If you need help with the blue, read-only module, please visit its page.

To learn more about the module itself, click here.

For the sample code below, the wiring should be set up as:

VCC -> +5V (arduino)
SIN -> digital pin 6 (this is the txPin)
SOUT -> digital pin 8 (this is the rxPin)
GND -> GND (arduino)

Here are three example codes. The first is for reading data from the older EM4100 RFID tags and displaying the value in the serial monitor.

//Code to read data from Parallax RFID reader/writer 28440 via Arduino
//Program reads data from the old EM4100-based tags and prints their value in the serial monitor.

//Writen by vgrhcp, uberdude, sebflippers and sixeyes

#include <SoftwareSerial.h>
#define txPin 6
#define rxPin 8
#define RFID_LEGACY 0x0F


SoftwareSerial mySerial(rxPin, txPin);
int  val = 0; 
char code[11];     //Note this is 11 for the extra null char?
int bytesread = 0;

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);

  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(txPin, OUTPUT);     //pin 6
  pinMode(rxPin, INPUT);      //pin 8

  Serial.println("RFID Read/Write Test");
}

void loop()
{
  mySerial.print("!RW");
  mySerial.write(byte(RFID_LEGACY));

  //mySerial.print(32, BYTE);

  if(mySerial.available() > 0) {          // if data available from reader 

    if((val = mySerial.read()) == 10) {   // check for header 
      bytesread = 0; 
      while(bytesread<10) {              // read 10 digit code 
        if( mySerial.available() > 0) { 
          val = mySerial.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                       // stop reading 
          } 
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit  
        } 
      } 

      if(bytesread == 10) {              // if 10 digit read is complete 
        Serial.print("TAG code is: ");   // possibly a good TAG 
        Serial.println(code);            // print the TAG code 
      } 
      bytesread = 0;

      delay(500);                       // wait for a 1/2 second 
    } 
  } 
}

The following code reads the newer EM4x50 tags and displays them in the serial monitor.


#include <SoftwareSerial.h>
#define RFID_READ 0x01
#define txPin 6
#define rxPin 8

SoftwareSerial mySerial(rxPin, txPin);
int val;
int runs = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("RFID Read/Write Test");
  mySerial.begin(9600);
  pinMode(txPin, OUTPUT);    
  pinMode(rxPin, INPUT);      
}

void suppressAll()                                //suppresses the "null result" from being printed if no RFID tag is present
{
    if(mySerial.available() > 0)
    { mySerial.read();
      suppressAll();
    }
}

void loop()
{
 int val;
  mySerial.print("!RW");
  mySerial.write(byte(RFID_READ));
  mySerial.write(byte(32));

  if(mySerial.available() > 0)
  {      
    val = mySerial.read();                        //The mySerial.read() procedure is called, but the result is not printed because I don't want the "error message: 1" cluttering up the serial monitor
      if (val != 1)                                   //If the error code is anything other than 1, then the RFID tag was not read correctly and any data collected is meaningless. In this case since we don't care about the resultant values they can be suppressed
       {suppressAll();}                              
  }      


 if(mySerial.available() > 0) {      
    val = mySerial.read();
    Serial.print("1st:");
    Serial.println(val, HEX);
    }

if(mySerial.available() > 0) {        
    val = mySerial.read();
    Serial.print("2nd:");
    Serial.println(val, HEX);
    }

if(mySerial.available() > 0) {      
    val = mySerial.read();
    Serial.print("3rd:");
    Serial.println(val, HEX);
    }

if(mySerial.available() > 0) {          
    val = mySerial.read();
    Serial.print("4th:");
    Serial.println(val, HEX);
    Serial.println("-----------------");
    }  

delay(750);
}



The following code is for writing 4 bytes of data (as defined by first, second, third, and fourth) to a specific address (as defined by whichSpace). Upon successful write operation, a confirmation is printed in the serial monitor.

//Code to write data to Parallax RFID reader/writer 28440 from Arduino
//Program writes to one of the 29 user-defined addresses (3-31) as define by whichSpace
//The four bytes to be written are defined by first, second, third, and fourth
//Coded by vgrhcp, adapted to arduino 1.0 by sebflippers

 #include <SoftwareSerial.h>
 #define RFID_WRITE 0x02
 #define txPin 6
 #define rxPin 8

 #define whichSpace 4

 #define first 1                 // first, second, thrid, and fourth are four arbitrary values which will be written to the RFID tag at address whichSpace
 #define second 26
 #define third 3
 #define fourth 27

SoftwareSerial mySerial(rxPin, txPin);

void setup()
{
  Serial.begin(9600);
  Serial.println("RFID Write Test");
  mySerial.begin(9600);
  pinMode(txPin, OUTPUT);     
  pinMode(rxPin, INPUT);      
}


void suppressAll()                                      //Keeps error code & the "write confirmation" codes from being printed in the serial monitor       
{
    if(mySerial.available() > 0)
    { mySerial.read();
      suppressAll();
    }
} 

 void loop()
{
  int val;

  mySerial.print("!RW");
  mySerial.write(byte(RFID_WRITE));
  mySerial.write(byte(whichSpace));
  mySerial.write(byte(first));
  mySerial.write(byte(second));
  mySerial.write(byte(third));
  mySerial.write(byte(fourth));

if(mySerial.available() > 0) {        
    val = mySerial.read();
    if (val == 1)                                        //If data was written successfully
      { Serial.println("Data written succesfully!");
        suppressAll();
      }
    else suppressAll();                                  //If an error occured during writing, discard all data recieved from the RFID writer
    }
delay(250);
}

Share