1 /* Arduino Projects for Dummies
4 * Chapter 9: Building an RFID Tag Reader
5 * A system that Reads and matches RFID tag to trigger a relay
8 * Adapted from bildr.com
12 const int relayPin = 11; // The pin the relay is connected to
13 const int ledPin = 12; // The pin the indicaor LED is connected to
14 const int RFIDResetPin = 13; // This pin tells the reader to read again
15 const int relayOntime = 5000; // The time to leave the relay on, in ms.
17 // Make a list of your RFID tags here.
18 // If you don't know them, leave at least one in place as starter
19 // a starter ID and delete it later. Otherwise the code will break.
20 // (You always have to have something to compare the read tags to.)
22 char tag1[13] = "4B00DDBF9FB6"; // Your scanned tag ID goes here
23 char tag2[13] = "010203AABBCC"; // these are example Iag IDs only
24 char tag3[13] = "010203AABBDD"; // these are example Iag IDs only
28 Serial.begin(9600); // Start the serial connection to TX/RX data
29 pinMode(RFIDResetPin, OUTPUT); // Tells the reader to start again
30 pinMode(ledPin, OUTPUT); // Set the LED pin to output
31 pinMode(relayPin, OUTPUT); // Set the LED pin to output
32 digitalWrite(RFIDResetPin, HIGH); // Make it ready to read
33 Serial.println("Ready."); // Advise the consoe we are ready.
38 Serial.println("Looking for a tag..."); // Say what we are currently doing
39 char tagString[13]; // Create an array to hold the tag we are reading
40 int index = 0; // A utility counter to track where we are in tagstring[]
41 boolean reading = false; // Stores whether we have a reading
43 while(Serial.available()){ // If there is a serial connection...
45 int readByte = Serial.read(); // Read next available byte
46 if(readByte == 2) reading = true; // 2 indicates the begining ofa tag
47 if(readByte == 3) reading = false; // 3 indicates the end of tag
49 // If there is a reading and it's not the beginning or end, store it
50 if(reading && readByte != 2 && readByte != 10 && readByte != 13){
52 tagString[index] = readByte; // Store the tag at the index point
53 index ++; // increment where we are in the storage array
57 checkTag(tagString); // Check if it is a match
58 clearTag(tagString); // Clear the char array of all values
59 resetReader(); // Reset the RFID reader
63 void checkTag(char tag[]){
65 // Compare this tag to the stored tags
66 if(strlen(tag) == 0) return; // If there's nothing here, do nothing
68 if(compareTag(tag, tag1)){ // If read tag matches tag1, do this
72 else if(compareTag(tag, tag2)){ // If read tag matches tag2, do this
73 lightLED(); // You could have any function here!
75 else if(compareTag(tag, tag3)){ // If read tag matches tag3, do this
77 } else { // If it doesn't match the list of tags...
78 Serial.println("New tag found: ");
79 Serial.println(tag); // Print the tag number of this new tag
80 delay(5000); // Wait 5 seconds, so we can make note of it
85 boolean compareTag(char one[], char two[]){
86 // Compare our two chars to see whether the tag
87 // we just read is the same as the stored tag
89 if(strlen(one) == 0) return false; // If there's nothing here, do nothing
91 for(int i = 0; i < 12; i++){
92 if(one[i] != two[i]) return false; // If the two tags are are not the same, return no match
94 Serial.println("Valid tag found!");
95 return true; // Otherwise, return that there is a match!
99 digitalWrite(ledPin, HIGH); // Turn on the ledPin
100 delay(250); // Wait a moment
101 digitalWrite(ledPin, LOW); // Turn off the ledPin
105 digitalWrite(relayPin, HIGH); // Turn on the ledPin
106 delay(relayOntime); // Wait a moment
107 digitalWrite(relayPin, LOW); // Turn off the ledPin
110 void clearTag(char one[]){
111 // Clear the tag reading char array by filling it with ASCII 0
112 // If not null, it could indicate another tag read
113 for(int i = 0; i < strlen(one); i++){
119 // Toggle the reset pin so the RFID reader will read again
120 digitalWrite(RFIDResetPin, LOW);
121 digitalWrite(RFIDResetPin, HIGH);