Integrating ESP32 with NFC: A Hands-On Tutorial 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) is a short-range wireless technology that enables secure data exchange between devices. With the ESP32, you can integrate NFC functionality into your IoT projects, allowing you to read from and write to NFC tags. This guide dives into the practical aspects of working with NFC tags using the ESP32, including hardware setupZigbee 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., libraries, and example code.

Table of Contents🔗

Introduction to NFC and ESP32🔗

NFC operates at 13.56 MHz and is commonly used for contactless payments, access control, and data sharing. While 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, you can easily add it using external modules like the PN532Adding NFC to ESP32: Hardware Options (PN532, RC522) and WiringAdding NFC to ESP32: Hardware Options (PN532, RC522) and WiringDiscover how NFC transforms your ESP32 projects with PN532 or RC522 modules. Follow our guide for secure contactless interactions and proper wiring setups. or RC522Adding NFC to ESP32: Hardware Options (PN532, RC522) and WiringAdding NFC to ESP32: Hardware Options (PN532, RC522) and WiringDiscover how NFC transforms your ESP32 projects with PN532 or RC522 modules. Follow our guide for secure contactless interactions and proper wiring setups.. These modules communicate 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 UART interfaces.

Hardware Setup🔗

To get started, you’ll need:

Wiring the PN532 Module

The PN532 modulePeer-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 popular choice for NFC projects. Here’s how to connect it to the ESP32 via SPI:

ESP32 PinPN532 Pin
3.3VVCC
GNDGND
GPIO23MOSI
GPIO19MISO
GPIO18SCK
GPIO5SS/CS

Example initialization for PN532Adding NFC to ESP32: Hardware Options (PN532, RC522) and WiringAdding NFC to ESP32: Hardware Options (PN532, RC522) and WiringDiscover how NFC transforms your ESP32 projects with PN532 or RC522 modules. Follow our guide for secure contactless interactions and proper wiring setups.:

#include <SPI.h>
#include <PN532_SPI.h>
PN532_SPI pn532(SPI, 5); // CS on GPIO5
void setup() {
  pn532.begin();
  uint32_t version = pn532.getFirmwareVersion();
  if (!version) {
    Serial.println("PN532 not detected!");
    while (1);
  }
  pn532.SAMConfig();
}

Wiring the RC522 Module

The RC522 is a more cost-effective option. Here’s the 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.:

ESP32 PinRC522 Pin
3.3V3.3V
GNDGND
GPIO23MOSI
GPIO19MISO
GPIO18SCK
GPIO5SDA
GPIO22RST

Installing Required Libraries🔗

Install these libraries via Arduino IDE or PlatformIO:

PlatformIO platformio.ini snippet:

lib_deps =
  espressif/esp32_nfc @ ^1.0.0
  miguelbalboa/MFRC522 @ ^1.4.10

Reading NFC Tags🔗

NFC Data Exchange FormatPeer-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. (NDEF) is the standard for storing data like URLs, text, or Wi-Fi credentials. Use the NDEF_Message class to decode tags.

Code to read an NDEFPeer-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. URL:

#include <NDEF_Message.h>
#include <NDEF_Record.h>
void loop() {
  if (pn532.tagPresent()) {
    NDEF_Message message;
    if (message.decode(pn532)) {
      for (int i = 0; i < message.getRecordCount(); i++) {
        NDEF_Record record = message.getRecord(i);
        if (record.isUrl()) {
          String url = record.getPayloadString();
          Serial.println("URL: " + url);
        }
      }
    }
  }
  delay(1000);
}

TroubleshootingConnecting 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. Tip: Ensure tags are within 5 cm of the antenna. Use pn532Adding NFC to ESP32: Hardware Options (PN532, RC522) and WiringAdding NFC to ESP32: Hardware Options (PN532, RC522) and WiringDiscover how NFC transforms your ESP32 projects with PN532 or RC522 modules. Follow our guide for secure contactless interactions and proper wiring setups..setPassiveActivationRetries(0xFE) to increase detection 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..

Writing NFC Tags🔗

Write NDEF messagesPeer-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. to blank tags using ndef_message and ndef_record classes.

Example: Writing Wi-FiArquitetura 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. credentials:

NDEF_Message msg;
NDEF_Record wifiRecord;
wifiRecord.setType("W"); // Wi-Fi type for Android NFC
String payload = "W:T:WPA;S:MyWiFi;P:secret123;;";
wifiRecord.setPayload((uint8_t*)payload.c_str(), payload.length());
msg.addRecord(wifiRecord);
if (pn532.write(msg)) {
  Serial.println("Write success!");
}

Real-World Use: Embedding setup URLs in tags for IoT devicesConnecting 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. to trigger Wi-Fi provisioning.

Security Considerations🔗

While NFC is short-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., data can still be intercepted. Implement:

HMACNFC 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. Example:

#include <mbedtls/md.h>
String data = "SENSOR_DATA=25.6";
uint8_t hmac[32];
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
                key, key_len,
                (uint8_t*)data.c_str(), data.length(),
                hmac);
// Append hmac to data before writing

Use Cases🔗

1. Smart Home: Unlock doors by tapping an NFCNFC 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. tag.

2. Asset Tracking: Tag equipment with maintenance logs.

3. Retail: Product info tags for instant access via smartphone.

Practical Examples🔗

1. Access Control: Use NFCNFC 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. tags to authenticate users for door access.

2. Smart Posters: Store URLs on NFCNFC 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. tags for interactive advertisements.

3. Device Pairing: Share Wi-FiArquitetura 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. credentials via NFC for quick setup.

By integrating NFC with the ESP32, you can unlock a wide range of possibilities for secure, short-range communication in your IoT projectsConnecting 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.. Whether you’re building a smart home system or an industrial monitoring solution, NFC adds a layer of convenience and security.

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