FRIENDz

FAILURE IS THE SIGN OF SUCCESS!!

Reading RFID Tags using Arduino

RFID Connected with Arduino
A tag consists of a start character followed by a 10-digit tag and is terminated by an end character. The sketch waits for a complete tag message to be available and displays the tag if it is valid. The tag is received as ASCII digits (see Recipe 4.4 for more on receiving ASCII digits). You may want to convert this into a number if you want to store or compare the values received. To do this, change the last few lines as follows:
if( Serial.read() == endByte) // check for the correct end character
{
tag[bytesread] = 0; // terminate the string
long tagValue = atol(tag); // convert the ASCII tag to a long integer

Serial.print("RFID tag is: ");
Serial.println(tagValue);
}
RFID stands for radio frequency identification, and as the name implies, it is sensitive to radio frequencies and can be prone to interference. The code in this recipe’s Solution will only use code of the correct length that contains the correct start and end bits, which should eliminate most errors. But you can make the code more resilient by reading the tag more than once and only using the data if it’s the same each time. (RFID
readers such as the Parallax will repeat the code while a valid card is near the reader.) To do this, add the following lines to the last few lines in the preceding code snippet:
if( Serial.read() == endByte) // check for the correct end character
{
tag[bytesread] = 0; // terminate the string
long tagValue = atol(tag); // convert the ASCII tag to a long integer
if (tagValue == lastTagValue)
{
Serial.print("RFID tag is: ");
Serial.println(tagValue);
lasTagValue = tagValue;
}
}


RFID sketch Displays the value read from an RFID tag

const int startByte = 10; // ASCII line feed precedes each tag
const int endByte = 13; // ASCII carriage return terminates each tag
const int tagLength = 10; // the number of digits in tag
const int totalLength = tagLength + 2; //tag length + start and end bytes
char tag[tagLength + 1]; // holds the tag and a terminating null
int bytesread = 0;
void setup()
{
Serial.begin(2400); // set this to the baud rate of your RFID reader
pinMode(2,OUTPUT); // connected to the RFID ENABLE pin
digitalWrite(2, LOW); // enable the RFID reader
}
void loop()
{
if(Serial.available() >= totalLength) // check if there's enough data
{
if(Serial.read() == startByte)
{
bytesread = 0; // start of tag so reset count to 0
while(bytesread < tagLength) // read 10 digit code
{
int val = Serial.read();
if((val == startByte)||(val == endByte)) // check for end of code
break;
tag[bytesread] = val;
bytesread = bytesread + 1; // ready to read next digit
}
if( Serial.read() == endByte) // check for the correct end character
{
tag[bytesread] = 0; // terminate the string
Serial.print("RFID tag is: ");
Serial.println(tag);
}
}
}
}

No comments:

Post a Comment