Difference between revisions of "ECE 110/Equipment/RFID Module"

From PrattWiki
Jump to navigation Jump to search
(Sample Code)
(References)
Line 66: Line 66:
  
 
== References ==
 
== References ==
 +
* [https://www.asciitable.com/ ASCII Codes] from AsciiTable.com
 +
* [https://blog.pepperl-fuchs.com/en/2022/how-to-decode-rfid-tags/ How to Decode RFID Tags] from PEPPERL+FUCHS

Revision as of 04:39, 19 August 2022

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 two bytes will form the chip code. Each of these bytes will represent 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 ten bytes will form a unique ID. Each of these bytes will also represent a hexadecimal value.
  • 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 chip code and the unique ID.

Leads

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

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

// 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;
    case 0x3: Serial.print("ETX"); break;
    case 0xA: Serial.print("LF");  break;
    case 0xD: Serial.print("CR");  break;
    default:  Serial.print(val);   break;
  }
  Serial.print("\t");
    
  Serial.print(val, HEX); Serial.print("\t");
  Serial.print(val, DEC); Serial.print("\t");
  Serial.print(val, BIN); Serial.println("");
  }
}

Notes

References