Arduino: Read RFID from Sparkfun D-12


/ Published in: Processing
Save to your folder(s)

I was looking for a simple way to link my Sparkfun D-12 to a Mac OSX using the Arduino IDE. I found this awesome little script (see below).


Copy this code and paste it in your HTML
  1. // RFID reader ID-12 for Arduino
  2. // Based on code by BARRAGAN <http://people.interaction-ivrea.it/h.barragan>
  3. // and code from HC Gilje - http://hcgilje.wordpress.com/resources/rfid_id12_tagreader/
  4. // Modified for Arudino by djmatic
  5. // Modified for ID-12 and checksum by Martijn The - http://www.martijnthe.nl/
  6. //
  7. // Use the drawings from HC Gilje to wire up the ID-12.
  8. // Remark: disconnect the rx serial wire to the ID-12 when uploading the sketch
  9.  
  10.  
  11. void setup() {
  12. Serial.begin(9600); // connect to the serial port
  13. }
  14.  
  15. void loop () {
  16. byte i = 0;
  17. byte val = 0;
  18. byte code[6];
  19. byte checksum = 0;
  20. byte bytesread = 0;
  21. byte tempbyte = 0;
  22.  
  23. if(Serial.available() > 0) {
  24. if((val = Serial.read()) == 2) { // check for header
  25. bytesread = 0;
  26. while (bytesread < 12) { // read 10 digit code + 2 digit checksum
  27. if( Serial.available() > 0) {
  28. val = Serial.read();
  29. if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
  30. break; // stop reading
  31. }
  32.  
  33. // Do Ascii/Hex conversion:
  34. if ((val >= '0') && (val <= '9')) {
  35. val = val - '0';
  36. } else if ((val >= 'A') && (val <= 'F')) {
  37. val = 10 + val - 'A';
  38. }
  39.  
  40. // Every two hex-digits, add byte to code:
  41. if (bytesread & 1 == 1) {
  42. // make some space for this hex-digit by
  43. // shifting the previous hex-digit with 4 bits to the left:
  44. code[bytesread >> 1] = (val | (tempbyte << 4));
  45.  
  46. if (bytesread >> 1 != 5) { // If we're at the checksum byte,
  47. checksum ^= code[bytesread >> 1]; // Calculate the checksum... (XOR)
  48. };
  49. } else {
  50. tempbyte = val; // Store the first hex digit first...
  51. };
  52.  
  53. bytesread++; // ready to read next digit
  54. }
  55. }
  56.  
  57. // Output to Serial:
  58.  
  59. if (bytesread == 12) { // if 12 digit read is complete
  60. Serial.print("5-byte code: ");
  61. for (i=0; i<5; i++) {
  62. if (code[i] < 16) Serial.print("0");
  63. Serial.print(code[i], HEX);
  64. Serial.print(" ");
  65. }
  66. Serial.println();
  67.  
  68. Serial.print("Checksum: ");
  69. Serial.print(code[5], HEX);
  70. Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
  71. Serial.println();
  72. }
  73.  
  74. bytesread = 0;
  75. }
  76. }
  77. }

URL: http://playground.arduino.cc/Code/ID12

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.