Parallax RFID Card Reader Library (Contribution)

I wrote a library for the Parallax RFID Card Reader (http://www.parallax.com/StoreSearchResults/tabid/768/txtSearch/28140/List/0/SortField/4/ProductID/114/Default.aspx). I'm releasing it for all to benefit.

The library code is Creative Commons licensed and freely available at: git://github.com/wojnosystems/cw-rfid-reader.git.

I've provided examples in the .h file, reproduced here for convenience.

EXAMPLE USAGE:

#include <CWRFID.h>

// HARDWARE SETUP:
// Connect the RFID card reader's "VCC" pin to 5V on the Arduino board
// Connect the RFID card reader's "/ENABLE" pin to the Digital (PWM) 2 pin (digital 2)
// Connect the RFID card reader's "SOUT" pin to the RX1 pin under (Communication) (You MUST have an Arduino Mega)
// Connect the RFID card reader's "GND" pin to the "GND" pin on the Arduino

// Sets which pin we'll use to enable and disable our RFID device. In this case, it's digital pin #2
#define RFIDEN_PIN 2
// Helper function: enableds the RFID device using the helper defines given in CWRFID.h
// You do not have to use them, but it helps make the code below a bit more readable
#define ENABLE_RFID() CW_RFID_ENABLE_PIN(RFIDEN_PIN)
#define DISABLE_RFID() CW_RFID_DISABLE_PIN(RFIDEN_PIN)
void setup() {
  // Configure the Arduino IDE console (assumes Arduino 2560 Mega) so we can see IDs that are read
  Serial.begin(9600);
  
  // Configure the RFID enable output
  pinMode(RFIDEN_PIN,OUTPUT);
  // Turn off the reader at start (no need to waste power until we're ready)
  DISABLE_RFID();
}

void loop() {
  // This is our storage location for the ID that we're going to read
  int buffer[10];
  
  // output of the RFID call: 0 = OK, -1 = read error (try again), -3 = timeout
  int rfid_status = 0;
  
  // Turn on the radio
  ENABLE_RFID();
  
  // Ask the user to present his/her RFID key
  Serial.println("Please present your RFID key");
  
  /// Read the RFID Key Reader's serial data. Store the ID (if available) in "buffer",
  ///  wait 10 seconds before timing out
  ///  use RX2 as the input
  rfid_status = cw_rfid_p28140_read( buffer, 10000, &Serial1 );
  switch( rfid_status ) {
    // Success!
    case 0:
      // Write the ID to the console, separate each integer with a colon (:)
      // The ID is exactly 10 integer digits
      for( int i = 0; i < 10; ++i ) {
        Serial.print( (unsigned int)buffer[i] );
        // Add a colon separator, except for the last character, add a newline.
        if( i != 9 ) { Serial.print( ":" ); } else { Serial.print("\n"); }
      }
      // Turn off the radio
      DISABLE_RFID();
      // wait 5 seconds. You could, at this point, cross reference this ID
      // with a database of known keys to see if this person has access
      // If they do, let them in or do something. I'm just waiting here just because
      delay(5000);
      // Do stuff
      break;
    case -3:
      // This means they didn't hold up a key
      // Turn off the radio
      DISABLE_RFID();
      Serial.println( "Timeout reached" );
      // Do something else. The user is clearly not paying attention or doesn't want to present
      // an RFID key to us
      delay(5000);
      break;
    case -1:
      // Sometimes, the RFID reader will encounter noise. This will generate keys with spurious IDs
      // The library filters these out for you and will indicate a -1 when you should ignore the key
      // This is also caused by moving the RFID tag away from the reader before it's had a chance to read
      // the ID two times in a row. This takes about 1 second
      Serial.println( "Unable to read the key, please try again" );
      // Turn off the radio
      DISABLE_RFID();
      // Wait for the noise to dissipate
      delay(1000);
      break;
    default:
      // You shouldn't be able to read this state. Assume any values other than those
      // specified above are error conditions
      Serial.println("UNKNOWN ERROR");
  }
}

If you have a working RFID reader and keys, you will see key ID's such as:
30:48:52:33:48:30:48:48:42:56.

Installing:

  • Open Terminal.app
  • cd ~/Documents/Arduino/libraries (you may need to create the libraries folder)
  • git clone git://github.com/wojnosystems/cw-rfid-reader.git CWRFID
  • Restart the Arduino IDE (if already running)

Happy DIY'ing.

Thanks for sharing !