NFC Essentials: ESP32, PN532 & RC522 Integration Guide

Near Field CommunicationNFC Security: Implementing Encryption and Tamper DetectionNFC Security: Implementing Encryption and Tamper DetectionLearn how to secure your ESP32 NFC projects with AES encryption, HMAC validation, and tamper detection techniques for robust wireless security. (NFC) transforms ESP32 projects into secure, contactless interaction hubs. Whether you’re building smart locks, inventory trackers, or payment systems, integrating NFC requires the right hardware and wiring. This article will guide you through two popular NFC modules-the PN532Peer-to-Peer NFC Communication Between ESP32 DevicesPeer-to-Peer NFC Communication Between ESP32 DevicesDiscover how to set up NFC P2P communication on ESP32 devices. Our tutorial covers hardware, software integration, and practical security measures. and RC522-exploring their features, wiring options, and practical considerations for ESP32 integrationZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsDiscover how ZGP enables battery-free IoT devices through energy harvesting with ESP32 integrations, supporting smart home and industrial applications..

Table of Contents🔗

1. Introduction to NFC and Its Use CasesZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsDiscover how ZGP enables battery-free IoT devices through energy harvesting with ESP32 integrations, supporting smart home and industrial applications.

2. Hardware Options for NFC on ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips.

3. WiringUsing Quectel BC66/BG96 Modules with ESP32 for NB-IoT ConnectivityUsing Quectel BC66/BG96 Modules with ESP32 for NB-IoT ConnectivityExplore our detailed tutorial on integrating Quectel BC66/BG96 with ESP32 for low-power, reliable NB-IoT connectivity. Learn hardware setup and AT commands. NFC Modules to ESP32

4. Code Examples: Basic Tag Reading

5. Best PracticesZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsDiscover how ZGP enables battery-free IoT devices through energy harvesting with ESP32 integrations, supporting smart home and industrial applications. for NFC Integration

6. Practical Considerations and DebuggingConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies. Tips

Introduction to NFC and Its Use Cases🔗

NFC operates at 13.56 MHz and allows communication within a rangeQuick Comparison: Range, power consumption, costs, and complexity of each technologyQuick Comparison: Range, power consumption, costs, and complexity of each technologyDiscover the ideal wireless solution for your ESP32 IoT project by analyzing range, power, cost, and complexity. Optimize connectivity now. of up to 10 cm. It is widely used in:

The ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. does not have native NFC support, but NFC functionality can be added using external modules like the PN532 or RC522.

Hardware Options for NFC on ESP32🔗

PN532 NFC Module

The PN532Peer-to-Peer NFC Communication Between ESP32 DevicesPeer-to-Peer NFC Communication Between ESP32 DevicesDiscover how to set up NFC P2P communication on ESP32 devices. Our tutorial covers hardware, software integration, and practical security measures. is a versatile NFC module that supports:

It communicates with the ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. via SPI, I2C, or UARTInterfacing ESP32 with Zigbee3.0 Devices (Xiaomi, Philips Hue)Interfacing ESP32 with Zigbee3.0 Devices (Xiaomi, Philips Hue)Unlock seamless smart home integration by following our detailed guide on bridging ESP32 with external Zigbee modules for reliable IoT solutions., making it highly flexible for different project requirements.

Key Features:

Use Cases:

RC522 NFC Module

The RC522 is a costQuick Comparison: Range, power consumption, costs, and complexity of each technologyQuick Comparison: Range, power consumption, costs, and complexity of each technologyDiscover the ideal wireless solution for your ESP32 IoT project by analyzing range, power, cost, and complexity. Optimize connectivity now.-effective NFC module that supports:

It communicates with the ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. primarily via SPIInterfacing ESP32 with Zigbee3.0 Devices (Xiaomi, Philips Hue)Interfacing ESP32 with Zigbee3.0 Devices (Xiaomi, Philips Hue)Unlock seamless smart home integration by following our detailed guide on bridging ESP32 with external Zigbee modules for reliable IoT solutions..

Key Features:

Use Cases:

  • Budget-friendly projects.
  • Applications requiring simple tag reading/writing.

Wiring NFC Modules to ESP32🔗

Wiring PN532 to ESP32

The PN532 can be connected to the ESP32 via SPI, I2C, or UART. Below is an example of SPI wiringUsing Quectel BC66/BG96 Modules with ESP32 for NB-IoT ConnectivityUsing Quectel BC66/BG96 Modules with ESP32 for NB-IoT ConnectivityExplore our detailed tutorial on integrating Quectel BC66/BG96 with ESP32 for low-power, reliable NB-IoT connectivity. Learn hardware setup and AT commands.:

PN532 PinESP32 Pin
VCC3.3V
GNDGND
SCLKGPIO 18
MOSIGPIO 23
MISOGPIO 19
SSGPIO 5
IRQNot connected (optional)

Example Code for SPI Communication:

#include <SPI.h>
#include <Adafruit_PN532.h>
#define PN532_SS   5
#define PN532_MOSI 23
#define PN532_MISO 19
#define PN532_SCK  18
Adafruit_PN532 nfc(PN532_SS, PN532_MOSI, PN532_MISO, PN532_SCK);
void setup() {
  Serial.begin(115200);
  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();
  if (!versiondata) {
    Serial.println("PN532 not found");
    while (1); // halt
  }
  // Configure the PN532 as RFID/NFC reader
  nfc.SAMConfig();
  Serial.println("PN532 is ready");
}
void loop() {
  // Check for NFC tags and print their UID
  uint8_t success;
  uint8_t uid[7];
  uint8_t uidLength;
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  if (success) {
    Serial.print("Found a tag with UID: ");
    for(uint8_t i = 0; i < uidLength; i++) {
      Serial.print(uid[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
    delay(1000);
  }
}

Wiring RC522 to ESP32

The RC522 is typically connected to the ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. via SPI:

RC522 PinESP32 Pin
VCC3.3V
GNDGND
SDAGPIO 5
SCKGPIO 18
MOSIGPIO 23
MISOGPIO 19
RSTGPIO 22

Example Code for SPI Communication:

#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN    4    // Reset pin for RC522
#define SS_PIN     5    // Chip Select pin
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
  Serial.begin(115200);
  SPI.begin();
  rfid.PCD_Init();  // Initialize the RC522 module
  Serial.println("RC522 NFC Module Initialized");
}
void loop() {
  // Look for new NFC cards
  if ( ! rfid.PICC_IsNewCardPresent() || ! rfid.PICC_ReadCardSerial() ) {
    delay(50);
    return;
  }
  Serial.print("Card UID: ");
  for (byte i = 0; i < rfid.uid.size; i++) {
    Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(rfid.uid.uidByte[i], HEX);
  }
  Serial.println();
  rfid.PICC_HaltA(); // Stop reading
  delay(1000);
}

Code Examples: Basic Tag Reading🔗

Extend the PN532Peer-to-Peer NFC Communication Between ESP32 DevicesPeer-to-Peer NFC Communication Between ESP32 DevicesDiscover how to set up NFC P2P communication on ESP32 devices. Our tutorial covers hardware, software integration, and practical security measures. example to trigger an action (e.g., GPIO control):

// Add to setup():
pinMode(2, OUTPUT); // Built-in LED
// Add to loop() after UID detection:
if (uid[0] == 0xDE && uid[1] == 0xAD) { // Replace with your tag's UID
  digitalWrite(2, HIGH); // Authorized tag
} else {
  digitalWrite(2, LOW); // Unauthorized
}

Best Practices for NFC Integration🔗

1. Power ManagementArquitetura ESP32: SoC dual-core, subsistemas RF integradosArquitetura ESP32: SoC dual-core, subsistemas RF integradosDiscover the ESP32’s dual-core prowess and integrated RF subsystems for efficient, innovative IoT applications—from smart homes to industrial sensors.:

2. Security:

3. Antenna Placement:

4. Error HandlingConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies.:

uint8_t retries = 0;
while (retries < 3 && !nfc.readPassiveTargetID(...)) {
  retries++;
  delay(200);
}

Libraries to Explore:

Practical Considerations and Debugging Tips🔗

  • Interface Selection:

If your project requires additional features like tag emulation or you plan to use different modes, the PN532Peer-to-Peer NFC Communication Between ESP32 DevicesPeer-to-Peer NFC Communication Between ESP32 DevicesDiscover how to set up NFC P2P communication on ESP32 devices. Our tutorial covers hardware, software integration, and practical security measures. offers more versatility. For simple tag read/write tasks, the RC522 is a reliable and cost-effective choice.

  • Voltage Levels and Power:

Verify the operating voltage of your NFC module. Most modules are designed for 3.3V or 5V logic. The ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. typically operates at 3.3V, so level shifting might be necessary if your module requirements differ.

Loose connections or incorrect pin assignments can lead to erratic behaviour. Always double-check your wiringUsing Quectel BC66/BG96 Modules with ESP32 for NB-IoT ConnectivityUsing Quectel BC66/BG96 Modules with ESP32 for NB-IoT ConnectivityExplore our detailed tutorial on integrating Quectel BC66/BG96 with ESP32 for low-power, reliable NB-IoT connectivity. Learn hardware setup and AT commands. diagram against the module datasheet.

  • Library Versions:

Stick with reputable libraries like Adafruit PN532 and MFRC522 for robust community support and debuggingConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies. resources. Keeping your libraries up-to-date can save you from common pitfalls.

By carefully selecting your NFC module and following proper wiring and debugging practices, you’ll be well on your way to integrating NFC capability into your ESP32-powered IoT solutionsConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies..

Happy hacking!

Author: Marcelo V. Souza - Engenheiro de Sistemas e Entusiasta em IoT e Desenvolvimento de Software, com foco em inovação tecnológica.

References🔗

Share article

Related Articles