├── README.md └── rfid └── rfid.ino /README.md: -------------------------------------------------------------------------------- 1 | #arduino-rfid 2 | 3 | A short example of how to read 125 kHz RFID tags using an Arduino and a RMD6300 RFID module 4 | 5 | See a video here on how it works in action [Youtube] (http://www.youtube.com/watch?v=u8YD3-BrHv0) 6 | 7 | ## Required hardware 8 | 9 | * [Arduino Uno](http://arduino.cc/en/Main/ArduinoBoardUno) (or other Arduino) 10 | * [125Khz RFID module RDM6300 - UART](http://imall.iteadstudio.com/im120618002.html) 11 | * Breadboard, cables, LEDs 12 | 13 | ## How to use 14 | 15 | * Simply plug the antenna, as well as the power, ground and TX pins of the RFID module to the Arduino (you can find the pins description in the [datasheet](http://cdn.boxtec.ch/pub/iteadstudio/DS_IM120618002_RDM6300.pdf)). The TX pin of the module should connect to the RX pin of the Arduino. 16 | * To use the LEDs as success/error indicators, plug two LEDs to the Arduino (in our code, we use pin 10 and 13) 17 | * Using the Arduino IDE, compile and upload the code to the Arduino. Note that you have to *unplug* the cable connecting to the RX pin of the Arduino to be able to upload the code. 18 | * Put one of your RFID tags close to the antenna and watch! You can use the serial monitor of the Arduino IDE (Ctrl+Shift+M) to see the messages we are printing, and note down the IDs of your tags if you don't know them. -------------------------------------------------------------------------------- /rfid/rfid.ino: -------------------------------------------------------------------------------- 1 | // define constants for pins 2 | int SUCCESS = 10; 3 | int ERROR = 13; 4 | 5 | // variables to keep state 6 | int readVal = 0; // individual character read from serial 7 | unsigned int readData[10]; // data read from serial 8 | int counter = -1; // counter to keep position in the buffer 9 | char tagId[11]; // final tag ID converted to a string 10 | 11 | char* authorizedTags[4]; // array to hold the list of authorized tags 12 | 13 | // fills the list of authorzied tags 14 | void initAuthorizedTags() { 15 | // add your own tag IDs here 16 | authorizedTags[0] = "0400680B85"; 17 | authorizedTags[1] = "0400063EB9"; 18 | authorizedTags[2] = "040004F3F5"; 19 | authorizedTags[3] = "04006813AB"; 20 | } 21 | 22 | void setup() { 23 | Serial.begin(9600); 24 | pinMode(SUCCESS, OUTPUT); 25 | pinMode(ERROR, OUTPUT); 26 | initAuthorizedTags(); 27 | } 28 | 29 | // check if the tag ID we just read is any of the authorized tags 30 | int checkTag() { 31 | int i; 32 | 33 | for (i = 0; i < 4; ++i) { 34 | if (strcmp(authorizedTags[i], tagId) == 0) { 35 | return 1; 36 | } 37 | } 38 | return 0; 39 | } 40 | 41 | // convert the int values read from serial to ASCII chars 42 | void parseTag() { 43 | int i; 44 | for (i = 0; i < 10; ++i) { 45 | tagId[i] = readData[i]; 46 | } 47 | tagId[10] = 0; 48 | } 49 | 50 | // once a whole tag is read, process it 51 | void processTag() { 52 | // convert id to a string 53 | parseTag(); 54 | 55 | // print it 56 | printTag(); 57 | 58 | // check if the tag is authorized 59 | if (checkTag() == 1) { 60 | tagSuccess(); // if so, perform an action (blink a led, open a door, etc...) 61 | } else { 62 | tagFailed(); // otherwise, inform user of failure 63 | } 64 | } 65 | 66 | void printTag() { 67 | Serial.print("Tag value: "); 68 | Serial.println(tagId); 69 | } 70 | 71 | // perform an action when an authorized tag was read 72 | void tagSuccess() { 73 | Serial.println("Tag authorized."); 74 | 75 | // here, we simply turn on the success LED for 2s 76 | digitalWrite(SUCCESS, HIGH); 77 | digitalWrite(ERROR, LOW); 78 | delay(2000); 79 | } 80 | 81 | // inform the user that the tag is not authorized 82 | void tagFailed() { 83 | Serial.println("Unauthorized access!"); 84 | 85 | digitalWrite(SUCCESS, LOW); 86 | digitalWrite(ERROR, HIGH); 87 | delay(2000); 88 | } 89 | 90 | // this function clears the rest of data on the serial, to prevent multiple scans 91 | void clearSerial() { 92 | while (Serial.read() >= 0) { 93 | ; // do nothing 94 | } 95 | } 96 | 97 | void loop() { 98 | // turn LEDs off 99 | digitalWrite(SUCCESS, LOW); 100 | digitalWrite(ERROR, LOW); 101 | 102 | if (Serial.available() > 0) { 103 | // read the incoming byte: 104 | readVal = Serial.read(); 105 | 106 | // a "2" signals the beginning of a tag 107 | if (readVal == 2) { 108 | counter = 0; // start reading 109 | } 110 | // a "3" signals the end of a tag 111 | else if (readVal == 3) { 112 | // process the tag we just read 113 | processTag(); 114 | 115 | // clear serial to prevent multiple reads 116 | clearSerial(); 117 | 118 | // reset reading state 119 | counter = -1; 120 | } 121 | // if we are in the middle of reading a tag 122 | else if (counter >= 0) { 123 | // save valuee 124 | readData[counter] = readVal; 125 | 126 | // increment counter 127 | ++counter; 128 | } 129 | } 130 | } 131 | --------------------------------------------------------------------------------