How to use the PN532 RFID reader with Arduino

  • The PN532 allows reading, writing and emulation of NFC tags.
  • Supports multiple interfaces: I2C, SPI and UART.
  • It is ideal for security, home automation and access control projects.

pn532

The PN532 RFID reader is one of the most popular modules versatile y advanced available to work with NFC (Near Field Communication) technology. This small device allows read, Record and even emulate NFC tags, making it a must-have tool for projects to maximise security and your enjoyment., domótica o automation.

Connecting and configuring this module with an Arduino board can be easier than it seems. Below, we will teach you not only how to make the physical connections with the Arduino, but also how to install the Libraries necessary, understand the modes of communication and use examples coding practice. Get ready to discover the potential of this module in your projects.

What is the PN532 RFID module?

The PN532 module is a widely used NFC controller, which enables communication between NFC devices and other items such as RFID cards, compatible mobile phones or even acting as an NFC tag. Among its performance include:

  • Compatibility: It can operate with cards and devices compatible with the MIFARE standard.
  • Flexibility of communication: Allows connections via SPI, I2C and UART (HSU).
  • Multi-mode support: Includes reading/writing capabilities, emulation of card and communication Peer to peer.
  • Ease of integration: Compatible with Arduino boards and similar microcontrollers.

Necessary materials

To start working with the PN532 module, you need the following components:

  • An Arduino board (for example, Arduino UNO).
  • The PN532 NFC module.
  • Compatible NFC cards or RFID key fobs.
  • Dupont or similar connecting cables.
  • A computer with the Arduino IDE installed.

Physical connections

pn532 pinout and Arduino connection

The PN532 module can be connected to the Arduino using different interfaces. communication: I2C, SPI or UART. Below we explain how to make the connections for each case:

Connection via I2C

I2C communication is simple and only requires a few pins:

  • 5V from module to pin 5V from the Arduino.
  • GND from module to pin GND from the Arduino.
  • SDA from module to pin A4 from the Arduino.
  • SCL from module to pin A5 from the Arduino.

Connection by SPI

To use SPI, you need to connect these pins:

  • MOTION from module to pin 11 from the Arduino.
  • MISO from module to pin 12 from the Arduino.
  • SCK pins can be used from module to pin 13 from the Arduino.
  • SS from module to pin 10 from the Arduino.

Connection by UART

If you prefer the UART (also known as HSU), you need to make the following connections:

  • TX from module to pin RX from the Arduino (pin 0).
  • RX from module to pin TX from the Arduino (pin 1).

Setting up Arduino IDE and installing the library

To work with the PN532, you must install the suitable library. Follow these steps:

  1. Open the Arduino IDE and go to Sketch > Include Library > Manage Libraries.
  2. Search for “PN532” in the search bar.
  3. Select the library Adafruit PN532 and click Install.

Operating modes

The PN532 supports several modes of operation, including:

  • Reader/Writer: It allows read y write data on NFC cards.
  • Card Emulation: It works like a NFC tag.
  • Peer-to-Peer Communication: Exchange data with other NFC devices.

Example code

Once everything is set up, you can upload a code basic To read the UID of compatible NFC cards:

#include #include #include #define IRQ_PIN 2 #define RESET_PIN 3 Adafruit_PN532 nfc(IRQ_PIN, RESET_PIN); void setup() { Serial.begin(115200); nfc.begin(); uint32_t version = nfc.getFirmwareVersion(); if (!version) { Serial.println("PN532 not detected"); while (1); } nfc.SAMConfig(); Serial.println("Waiting for card..."); } void loop() { uint8_t success; uint8_t uid[7]; uint8_t uidLength; success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); if (success) { Serial.print("UID detected: "); for (int i = 0; i < uidLength; i++) { Serial.print(uid[i], HEX); Serial.print(" "); } Serial.println(); } }

Tests and applications

Once the code is uploaded, connect the Arduino to the computer and open the serial monitor. When you bring an NFC card close to the module, you should see its UID printed to the console. This simple experiment opens a world of possibilities, such as:

  • Temperature control access with unique identification.
  • Automation of processes based on NFC cards.
  • Creation of payment systems in DIY projects.

The PN532 reader offers a wide range of functionalities, from read NFC cards to emulate them. Thanks to its flexibility y usability, is ideal for both beginners and experts. Get ready to explore the world of wireless communication with this fantastic module.