# Example 43: RFID Card (MFRC522)

## Experiment Overview

MFRC522 is a 13.56MHz SPI RFID reader for MIFARE cards. FastBee includes the driver in the release baseline.

> **Note**: This feature requires `FASTBEE_ENABLE_RFID=1` and active `rfid`.

## Hardware Wiring

| Board Label | GPIO Pin | Connected Device |
|-------------|----------|------------------|
| SDA(SS) | GPIO5 | MFRC522 SDA (Chip Select) |
| SCK | GPIO18 | MFRC522 SCK |
| MOSI | GPIO23 | MFRC522 MOSI |
| MISO | GPIO19 | MFRC522 MISO |
| RST | GPIO27 | MFRC522 RST (Reset) |

> MFRC522 requires 3.3V power supply; do NOT connect to 5V!

## JSON Configuration Example

```json
{
  "peripherals": [
    {
      "id": "rfid_01",
      "name": "RFID Reader",
      "type": 38,
      "enabled": false,
      "pins": [5, 18, 23, 19, 27],
      "params": {
        "category": "rfid"
      }
    }
  ]
}
```

## Peripheral Execution Linkage

### Scenario 1: Card Swipe Unlock (Event Trigger)

**Feature**: When RFID card is detected, open relay for 3 seconds then close

#### Web UI Configuration Steps

**Step 1: Ensure Relay Peripheral is Configured**

* Peripheral ID: `relay_01`
* Type: GPIO Output

**Step 2: Create Rule**

1. Click **Peripheral Execution** in the left menu
2. Click **<i class="fas fa-plus"></i> New Rule** button
3. Fill in basic configuration:
   * **Rule Name**: `RFID Unlock`
   * **Report Data**: ✅ Enabled
   * **Enable**: ✅ Enabled

**Step 3: Configure Trigger (Event Trigger)**

1. Click **Add Trigger** button
2. Fill in trigger configuration:

   | Field | Value | Description |
   |-------|-------|-------------|
   | **Trigger Type** | Select **Event Trigger** | RFID card swipe event |
   | **Event Type** | `120` | RFID card detected |
   | **Event Source** | Select `rfid_01` | RFID reader |

**Step 4: Configure Actions (3 actions required)**

**Action 1: Open Relay**

1. Click **Add Action** button
2. Fill in:
   * **Action Type**: Select **HIGH**
   * **Target Peripheral**: Select `relay_01`

**Action 2: Delay 3 Seconds**

1. Click **Add Action** button again
2. Fill in:
   * **Action Type**: Select **Delay**
   * **Delay Time**: `3000` (3000ms = 3 seconds)

**Action 3: Close Relay**

1. Click **Add Action** button a third time

2. Fill in:
   * **Action Type**: Select **LOW**
   * **Target Peripheral**: Select `relay_01`

3. Click **Save** button

***

### Scenario 2: Card Removal Event (Event Trigger)

**Feature**: Turn off LED when RFID card is removed

**Configuration Steps**:

1. Create rule, name: `RFID Card Removed`
2. Trigger configuration:
   * **Trigger Type**: Select **Event Trigger**
   * **Event Type**: `121` (RFID card removed)
   * **Event Source**: Select `rfid_01`
3. Action configuration:
   * **Action Type**: Select **HIGH** (LED off)
   * **Target Peripheral**: Select `led_d1`
4. Click **Save**

### RFID Specifications

| Feature | Description |
|---------|-------------|
| Operating Frequency | 13.56MHz |
| Supported Cards | Mifare series IC cards |
| Read Distance | 3-5cm |
| UID Format | 4-byte hexadecimal (e.g., A1B2C3D4) |
| Polling Rate | Internal 200ms polling |

## Notes

1. **Firmware State**: Confirm `rfid` is compiled and active
2. **Power Supply**: MFRC522 must use 3.3V; 5V will damage the chip
3. **Read Distance**: Typical card reading distance is 3-5cm
4. **UID Format**: Card UID is 4-byte hexadecimal, e.g., `A1B2C3D4`
5. **SPI Pins**: Pin order is SS, SCK, MOSI, MISO, RST
6. **Polling Rate**: Driver internally polls for cards every 200ms
