> For the complete documentation index, see [llms.txt](https://docs.launchboard.app/cxd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.launchboard.app/cxd/internet-of-things-project/references-for-wiring-and-coding/rfid-reader.md).

# RFID Reader

An [RFID reader](https://www.sparkfun.com/products/13198) can be used to read ID numbers (or other data) stored in RFID tags, labels, and chips. RFID stands for "[radio-frequency identification](https://en.wikipedia.org/wiki/Radio-frequency_identification)" – radio waves of a specific frequency are used to read the data from a distance.

RFID is used extensively by industries and stores to keep track of inventory. Some companies give their employees RFID cards for access to buildings and other purposes. Some people have their pets embedded with a RFID microchip to help identify the pet if it gets lost and is found by someone.

Near-Field Communication (NFC) is a type of RFID. For example, the Apple Pay and Android Pay features in certain smartphones use NFC.

The RFID reader kit comes with an RFID reader (the black reader module has pins that plug into to the female pins on the reader board) plus two RFID cards, which look blank but actually have a tag embedded in them that stores a unique ID number.

When an RFID card is brought within about 2 inches of the reader, it will be automatically identified. The RFID reader has a buzzer that beeps and a small green LED that lights up to verify that the card has been identified.

![RFID Reader Kit](https://576155698-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LDgX-RdB-DPZBPuXzJ1%2F-LDgX75Qrx69yS4tZ2_Z%2F-LDgXCDP3PC90uv0oxQI%2Frfid-card-reader-kit.jpg?generation=1527608039463837\&alt=media)

The RFID reader board has 8 different pin holes, but only 3 of these have to be connected. The easiest way to connect the RFID reader is to use a [3-wire JST connector](https://www.sparkfun.com/products/9915). Connect the 3 wires (black, red, blue) to the corresponding pin holes, and then plug the male JST connector into different numbered rows on a breadboard. Then use jumper wires to connect the rows to the Photon. To make it easier to remember which wire is which, use corresponding black, red, and blue jumper wires to match the JST wires.

| RFID Reader         | Photon Pin |
| ------------------- | ---------- |
| VCC - Power (Black) | 3.3V       |
| TX - Data (Red)     | RX         |
| GND - Ground (Blue) | GND        |

The RFID reader will send the card ID data to the Photon board over a serial data connection using the Photon RX pin.

**NOTE:** For serial data connections, the RX pin of one device connects to the TX pin of the other device. (RX = receive, TX = transmit)

**FYI:** The RFID reader also has a Mini-USB port, which can be used to connect it directly to a computer (instead of wiring it to a Photon or other microcontroller device). However, you will **not** use this port. (Also this Mini-USB port is different from the Micro-USB cable included with your Photon kit.)

## Code for RFID Reader

### Library

The RFID reader does **not** require any special code library. (It does use a serial data connection, but serial data functions are **built-in** to the Particle firmware on your Photon device.)

### Global Variables

Each RFID card has a code consisting of 16 characters (combination of numbers and letters): 12-character unique ID code + 4 special characters (that are not unique). When we read the RFID card, we only want to keep the unique ID code.

In the global variables, you should declare an array variable of **13** characters, which will be used to store the unique ID code from the RFID card. (The array will include 1 extra character at the end, which will always be zero. This is needed for converting the character array to a String.) The example below declares an array variable called "RFID" (but you could use a different variable name).

You will probably want to also declare a String variable that will simply be a text version of the RFID (the array is separate pieces of data, while the String is the same data stored as one piece). This String variable will be easier to use for purposes such as comparing the ID to a set of known IDs, sending the ID to your web app, etc. The example below declares a String variable called "idCard" (but you could use a different variable name).

```java
char RFID[13]; // stores 12 character RFID code + 1 empty character
String idCard; // text string version of RFID code
```

### setup( ) function

You need to include a statement within the `setup()` function to start a serial data connection (which will activate both the RX and TX pins on the Photon). This will allow the Photon to receive data from the RFID reader. The RFID reader communicates at a baud rate of 9600 bps.

```java
Serial1.begin(9600);
```

### Read RFID Card

Code for reading an RFID card should probably be placed within its own custom function, which can then be called within the `loop()` function.

Inside the `readRFID()` function, you will have to insert additional code to do something with the ID after it has been read (such as compare the ID to a set of known IDs, send the ID to your web app as an event notification, etc.).

```java
void loop() {
    readRFID();
    // can add other code
    
}

// CUSTOM FUNCTION - checks for RFID card data
void readRFID() {
    int readByte;
    int i = 0;

    // make sure entire RFID (16 characters) is available before reading data
    if (Serial1.available() == 16) {
        // while data is still available in serial port
        while (Serial1.available()) {
            // read (and remove) next available byte (one character) of data
            readByte = Serial1.read();
            // if not special character, then add to RFID
            if (readByte != 2 && readByte!= 13 && readByte != 10 && readByte != 3) {
                RFID[i] = readByte;
                i++;
            }
        }
    idCard = String(RFID);
    // insert code here to do something with idCard

  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.launchboard.app/cxd/internet-of-things-project/references-for-wiring-and-coding/rfid-reader.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
