miguelbalboa/rfid

repository·master·Indexed 25 days ago

https://github.com/miguelbalboa/rfid

An Arduino library for interacting with MFRC522-based RFID readers via SPI. It supports reading and writing MIFARE Classic (1k, 4k, Mini) and MIFARE Ultralight cards. The library provides functionality for firmware self-checks, UID reading, and sector authentication. Compatible with Arduino IDE 1.6+, Teensy, and ESP8266. Note: The library is currently in a feature freeze; for advanced development, RFID_MFRC522v2 is recommended.

Tokens
4.9K
Snippets
4
Records
28
Agent score
85%

What's inside miguelbalboa-rfid

  1. Overview of MFRC522 RFID Library

    master

    This is an Arduino library for MFRC522 and other RFID RC522 based modules. It allows you to read and write different types of Radio-Frequency IDentification (RFID) cards using an RC522 reader connected via the Serial Peripheral Interface (SPI).

    Note on Development Status: The library is in a feature freeze. No new functions or APIs are being added; updates are limited to bug fixes, typos, or documentation. For advanced development, consider using the RFID_MFRC522v2 library.

  2. Understand MFRC522 feature limitations

    master

    The MFRC522 library and hardware have the following limitations:

    • No Card Simulation: The reader cannot act as a card.
    • No Mobile Communication: You cannot communicate with mobile phones using this hardware.
    • No Peer-to-Peer: Peer-to-peer communication is not supported.
    • Feature Upgrades: If you require NFC or more advanced features, consider using a chip like the PN532.
  3. Compatible Boards

    master

    The library primarily targets Arduino boards. However, it is compatible with the following boards (though you may need to change pins and not all examples will be available):

    • Teensy
    • ESP8266 (via Arduino IDE board plugin)

    Note for Advanced Users: If your microcontroller supports multiple SPI interfaces, this library only uses the default (first) SPI of the Arduino framework.

  4. Compatible IDEs and Compilers

    master

    To use this library, ensure your environment meets the following requirements:

    • Arduino IDE: Version 1.6 or newer is required. Older versions are not supported and will cause compiler errors.
    • Custom Compilers: If you are using your own compiler instead of the Arduino IDE, you must enable c++11 support.
  5. Use MFRC522Extended for ISO-14443-4 (RATS) support

    master
    The MFRC522Extended class extends the standard MFRC522 library to provide support for ISO-14443-4 PICC cards, specifically enabling Request for Answer To Select (RATS) functionality. It provides enhanced data structures for handling Answer To Select (ATS) information and specialized communication methods for ISO/IEC 14443-4 cards.
  6. Initialize the MFRC522 class

    master

    You can initialize the MFRC522 object using one of three constructors depending on your hardware wiring:

    1. Default: Uses default pins.
    2. Reset Pin only: Specify only the resetPowerDownPin.
    3. Full Configuration: Specify both chipSelectPin and resetPowerDownPin.

    After instantiation, you must call PCD_Init() to begin communication with the module.

  7. Fix unknown Firmware Version (0x12 or random values)

    master

    If the library reports an unknown firmware version (e.g., 0x12), try the following:

    1. Initialization Delay: Some boards require more time to stabilize after initialization. Add a small delay immediately after calling PCD_Init():
    mfrc522.PCD_Init();
    delay(4);
    1. Connection/Power: Intermittent reporting of random firmware versions is often caused by poor connections or an unstable power source.
    2. Hardware Authenticity: If the firmware version is permanently reported as unknown, the hardware may be a counterfeit or defective.
  8. Verify tag/card compatibility

    master

    If your tag or card is not being detected, verify the following:

    1. Frequency: Ensure the tag is 13.56 MHz (Mifare Type A). This library does not support 125 kHz animal RFID tags.
    2. Type: NFC tokens are not officially supported (though some may work).
    3. Distance: The distance between the antenna and the token must be less than 1cm.
    4. Security/Protocol: Newer Mifare cards (e.g., DESFire, Ultralight) may fail due to missing authentication requirements. Check the security or protocol documentation for details.
    5. Encryption: If you can only read the card UID, the card is likely encrypted (e.g., public transport or university cards). This library cannot bypass such encryption.
  9. Troubleshoot MFRC522 communication failures

    master

    If you encounter WARNING: Communication failure, is the MFRC522 properly connected? or receive no input from the reader, check the following:

    1. Physical Connections: Verify pin settings in your code against the physical wiring. Check for cold solder joints on the pin headers.
    2. Power Supply: Ensure the power supply is stable. Adding a capacitor between 3.3V and GND can help. Most breakouts work at 3.3V; while some are 5V tolerant, SPI communication specifically requires 3.3V. Use a level shifter if necessary.
    3. Signal Integrity: SPI is sensitive to long connections and prototyping boards. Use shorter wires and soldered connections where possible.
    4. Hardware Defects: Some MFRC522 breakouts may have soldering issues on the board itself.
  10. Resolve tag/card timeouts and detection issues

    master

    If you experience timeouts or intermittent tag detection, try these steps:

    1. Physical Positioning: Try the other side of the antenna or decrease the distance between the MFRC522 and the tag.
    2. Increase Antenna Gain: You can increase the antenna gain using the following method:
    mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
    1. Power: Ensure you are using a high-quality power supply.
    2. Hardware Quality: Low-quality components (common in some inexpensive breakouts) can affect detection. Specifically, some boards may require replacing L1 and L2 inductors with 2.2uH inductors with higher operating current, or replacing C4 and C5 with 33pf capacitors to tune the matching circuit.
  11. Memory Layout of MIFARE Ultralight C (MF0ICU2)

    master

    The MIFARE Ultralight C chip has 192 bytes of memory, organized into 48 pages of 4 bytes each.

    Page Map:

    • Pages 0 + 1: Used for the 7-byte UID.
    • Page 2: Contains the UID check digit, one byte of manufacturer internal data, and the lock bytes.
    • Page 3: OTP (One Time Programmable) bits. Once set to 1, they cannot be reverted to 0.
    • Pages 4-39: Read/write pages, unless blocked by lock bytes in Page 2.
    • Page 40: Lock bytes.
    • Page 41: 16-bit one-way counter.
    • Pages 42-43: Authentication configuration.
    • Pages 44-47: Authentication key.
  12. Memory Layout of MIFARE Ultralight (MF0ICU1)

    master

    The MIFARE Ultralight chip has 64 bytes of memory, organized into 16 pages of 4 bytes each.

    Page Map:

    • Pages 0 + 1: Used for the 7-byte UID.
    • Page 2: Contains the UID check digit, one byte of manufacturer internal data, and the lock bytes.
    • Page 3: OTP (One Time Programmable) bits. Once set to 1, they cannot be reverted to 0.
    • Pages 4-15: Read/write pages, unless they have been blocked by the lock bytes defined in Page 2.