ECE 110/Equipment/RFID Module

From PrattWiki
Revision as of 15:33, 29 July 2024 by EBortolomiol (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

The ID-12LA RFID reader will read and report values for RFID tags. The tag information will be delivered to a serial port one byte at a time. Recall that a byte is an 8-bit binary number (which can also be represented with a 2-character hexadecimal number).

For the tags in lab:

  • The first byte will always be 00000010 (dec: 2, hex:0x02) - STX, the "start of text" code in ASCII.
  • The next ten bytes will form a unique ID. Each of these bytes will represent the characters in a hexadecimal value and will thus be the characters 0-9 or A-F (dec: 48-57 or 65-70, hex: 0x30-0x39 or 0x41-0x46).
  • The next two bytes will form a checksum. Each of these bytes will also represent a hexadecimal value. More information on the checksum will be given below.
  • The fourteenth byte will always be 00001110 (dec: 13, hex: 0x0D) - CR, the "carriage return" code in ASCII.
  • The fifteenth byte will always be 00001100 (dec: 10, hex: 0x0A) - LF, the "line feed" code in ASCII.
  • The sixteenth and final byte will always be 00000011 (dec: 3, hex: 0x03) - ETX, the "end of text" code in ASCII.

Generally, we will only want to store the 12 bytes comprising the unique ID and checksum.

Leads

  • VCC: Supply voltage, typically 5 V
  • DATA: Data channel - this should be a serial receive (Rx) port and on the CX-Bot it is Serial1 Rx(pin 19)
  • GND: Ground

The CX-Bot has a header for RFID module; the DATA pin is connected to Serial1 Rx (pin 19).

Operation

When the ID-12LA detects a passive RFID tag in its vicinity, it will read it and queue up the information on a serial channel. The information starts with a byte that means "start of text" and ends with a carriage return, a line feed, and a byte that means "end of text." As a result, reading the information from the ID-12LA requires reading more than just the chip code and unique ID.

Depending on which Arduino and shield pairing is used, you can use different serial pins to connect the ID-12LA. On the CX-Bot shield, the RFID pins already have Serial1 connected to the data pin; the good news there is you can use Serial to interact with the Serial Monitor and Serial1 to interact with the ID-12LA. This will not always be the case with other Arduinos or shields.

To read the information from the ID-12LA, you will need to determine if there is information available at the serial port; if there is, you need to keep reading successive bytes until you have read all the information. The code below is an expanded version of a demonstration code from Instructables. Their code reads and prints the raw characters read by the ID-12LA. The code below will translate some "unprintable" characters or characters that skip lines (STX, CR, LF, and ETX) to clarify exactly what was read and will also present the byte in raw form, as a hexadecimal number, as a decimal number, and as a binary number.

Sample Code

Simple

The code below will read a tag one character at a time and print a table of what it sees. Note that it will look for certain unprintable characters that get sent from the tag and will interpret them.

// Based on https://www.instructables.com/Reading-RFID-Tags-with-an-Arduino/
// Expanded by Michael R. Gustafson II to include hex / bin / dec values and 
// replace unprintable characters

char val = 0; // variable to store the data from the serial port

void setup() {
  Serial.begin(9600); // connect to the serial port for the monitor
  Serial1.begin(9600); // connect to the serial port for the RFID reader
  Serial.println("");
  Serial.println("Char\tHex\tDec\tBin");
}

void loop () {
  if(Serial1.available() > 0) {
  val = Serial1.read();

    // Handle unprintable / line skipping characters
    switch(val) {
      case 0x2: Serial.print("STX"); break; // start of transmission
      case 0x3: Serial.print("ETX"); break; // end of transmission
      case 0xA: Serial.print("LF");  break; // line feed
      case 0xD: Serial.print("CR");  break; // carriage return
      default:  Serial.print(val);   break; // actual character
    }
  Serial.print("\t");
    
  Serial.print(val, HEX); Serial.print("\t");
  Serial.print(val, DEC); Serial.print("\t");
  Serial.print(val, BIN); Serial.println("");
  }
}

Simple with Storage

The following code is similar to the code above, except instead of printing each byte it stores the ones that are printable characters into a character array and then displays that at the end.

// Based on https://www.instructables.com/Reading-RFID-Tags-with-an-Arduino/
// Expanded by Michael R. Gustafson II to store code

char val = 0; // variable to store the data from the serial port
int len = 12;

void setup() {
  Serial.begin(9600); // connect to the serial port for the monitor
  Serial1.begin(9600); // connect to the serial port for the RFID reader
}

void loop () {
  char rfidData[len+1] = {};
  int get_more = 1;
  int i = 0;
  
  while(get_more == 1){
    if(Serial1.available() > 0) {
    val = Serial1.read();
  
      // Handle unprintable characters
      switch(val) {
        case 0x2: break;               // start of transmission - do not save
        case 0x3: get_more = 0; break; // end of transmission - done with code
        case 0xA: break;               // line feed - do not save
        case 0xD: break;               // carriage return - do not save
        default:  rfidData[i]=val; i+=1;  break; // actual character
      }
    }
  }
  Serial.println(rfidData);
}

Notes

References