/* Arduino Projects for Dummies * by Brock Craft * * Chapter 9: Building an RFID Tag Reader * A system that Reads and matches RFID tag to trigger a relay * * v0.1 30.04.2013 * Adapted from bildr.com */ const int relayPin = 11; // The pin the relay is connected to const int ledPin = 12; // The pin the indicaor LED is connected to const int RFIDResetPin = 13; // This pin tells the reader to read again const int relayOntime = 5000; // The time to leave the relay on, in ms. // Make a list of your RFID tags here. // If you don't know them, leave at least one in place as starter // a starter ID and delete it later. Otherwise the code will break. // (You always have to have something to compare the read tags to.) char tag1[13] = "4B00DDBF9FB6"; // Your scanned tag ID goes here char tag2[13] = "010203AABBCC"; // these are example Iag IDs only char tag3[13] = "010203AABBDD"; // these are example Iag IDs only // etc. for more tags void setup(){ Serial.begin(9600); // Start the serial connection to TX/RX data pinMode(RFIDResetPin, OUTPUT); // Tells the reader to start again pinMode(ledPin, OUTPUT); // Set the LED pin to output pinMode(relayPin, OUTPUT); // Set the LED pin to output digitalWrite(RFIDResetPin, HIGH); // Make it ready to read Serial.println("Ready."); // Advise the consoe we are ready. } void loop(){ Serial.println("Looking for a tag..."); // Say what we are currently doing char tagString[13]; // Create an array to hold the tag we are reading int index = 0; // A utility counter to track where we are in tagstring[] boolean reading = false; // Stores whether we have a reading while(Serial.available()){ // If there is a serial connection... int readByte = Serial.read(); // Read next available byte if(readByte == 2) reading = true; // 2 indicates the begining ofa tag if(readByte == 3) reading = false; // 3 indicates the end of tag // If there is a reading and it's not the beginning or end, store it if(reading && readByte != 2 && readByte != 10 && readByte != 13){ tagString[index] = readByte; // Store the tag at the index point index ++; // increment where we are in the storage array } } checkTag(tagString); // Check if it is a match clearTag(tagString); // Clear the char array of all values resetReader(); // Reset the RFID reader } void checkTag(char tag[]){ // Compare this tag to the stored tags if(strlen(tag) == 0) return; // If there's nothing here, do nothing if(compareTag(tag, tag1)){ // If read tag matches tag1, do this lightLED(); triggerRelay(); } else if(compareTag(tag, tag2)){ // If read tag matches tag2, do this lightLED(); // You could have any function here! } else if(compareTag(tag, tag3)){ // If read tag matches tag3, do this lightLED(); } else { // If it doesn't match the list of tags... Serial.println("New tag found: "); Serial.println(tag); // Print the tag number of this new tag delay(5000); // Wait 5 seconds, so we can make note of it } } boolean compareTag(char one[], char two[]){ // Compare our two chars to see whether the tag // we just read is the same as the stored tag if(strlen(one) == 0) return false; // If there's nothing here, do nothing for(int i = 0; i < 12; i++){ if(one[i] != two[i]) return false; // If the two tags are are not the same, return no match } Serial.println("Valid tag found!"); return true; // Otherwise, return that there is a match! } void lightLED(){ digitalWrite(ledPin, HIGH); // Turn on the ledPin delay(250); // Wait a moment digitalWrite(ledPin, LOW); // Turn off the ledPin } void triggerRelay(){ digitalWrite(relayPin, HIGH); // Turn on the ledPin delay(relayOntime); // Wait a moment digitalWrite(relayPin, LOW); // Turn off the ledPin } void clearTag(char one[]){ // Clear the tag reading char array by filling it with ASCII 0 // If not null, it could indicate another tag read for(int i = 0; i < strlen(one); i++){ one[i] = 0; } } void resetReader(){ // Toggle the reset pin so the RFID reader will read again digitalWrite(RFIDResetPin, LOW); digitalWrite(RFIDResetPin, HIGH); delay(150); }