Example Code for Arduino-Read and Write MIFARE Classic S50 NFC Card with UART
Last revision 2026/01/25
This demo runs on the Arduino MEGA2560 platform. Download this demo to learn how to read data on card and read data through serial ports. Read the data on the card to see if the write is successful.
Hardware Preparation
- DFRduino UNO R3 or DFRobot Mega 2560 V3.0 x 1
- Gravity: UART & I2C NFC Module x 1
- Gravity 4P sensor wire (or Dupont wires) x 1
- USB to TTL Converter x 1
Software Preparation
Wiring Diagram
- UART interface: turn the switch to the right. The UART interface is mainly used to communicate with the PC through the USB to serial port convert to read, write or copy various NFC smart tags/cards, but it can also communicate with various 3.3V/5V controllers by programming. The connection diagram is based on the Arduino Mega2560. Note that the module's D/T and USB to serial port module's RXD, C/R and TXD are connected respectively.


Other Preparation Work
- According to the first connection diagram, connect UNO to the NFC module through UART, and turn the switch to UART.
- Download and install the DFRobot_PN532 library.
Sample Code
/*!
@flie read_S50_uart.ino
@Copyright [DFRobot](https://www.dfrobot.com), 2016
@Copyright GNU Lesser General Public License
@version V1.0
@date 07/03/2019
@brief This demo runs on the Arduino MEGA2560 platform.
Download this demo to learn how to read data on card and read data through serial ports.
Read the data on the card to see if the write is successful.
This demo and related libraries are for DFRobot Gravity: I2C&UART NFC Module
Product(CH): https://www.dfrobot.com.cn/goods-2029.html
Product(EN): https://www.dfrobot.com/product-1905.html
*/
#include <DFRobot_PN532.h>
#define BLOCK_SIZE 16
//Initialize MEGA2560
DFRobot_PN532_UART nfc;
struct card NFCcard ;
void setup() {
Serial.begin(115200);
while (!nfc.begin(&Serial3)) {
Serial.println("initial failure");
delay (1000);
}
Serial.println("Waiting for a card......");
}
uint8_t dataWrite[BLOCK_SIZE] = {"DFRobot NFC"}; /*Write data page */
uint8_t dataRead[16] = {0};
void loop() {
//Scan, write and read NFC card every 2s
//Print all what is to be written and read
if (nfc.scan() == true) {
NFCcard = nfc.getInformation();
if (NFCcard.AQTA[1] == 0x02 || NFCcard.AQTA[1] == 0x04) {
Serial.print("Data to be written(string):");
Serial.println((char *)dataWrite);
Serial.print("Data to be written(HEX):");
for (int i = 0; i < BLOCK_SIZE; i++) {
Serial.print(dataWrite[i], HEX);
Serial.print(" ");
}
Serial.println();
//Write data(16 bytes) to sector 1
if (nfc.writeData(2, dataWrite) != 1) {
Serial.println("write failure!");
}
//Read sector 1 to verify the write results
nfc.readData(dataRead, 2);
Serial.print("Data read(string):");
Serial.println((char *)dataRead);
Serial.print("Data read(HEX):");
for (int i = 0; i < BLOCK_SIZE; i++) {
//data[i] = dataRead[i];
Serial.print(dataRead[i], HEX);
Serial.print(" ");
}
}
else {
Serial.println("The card type is not mifareclassic...");
}
}
else {
//Serial.println("no card");
}
delay(2000);
}
Result
A string is written to the cards and then read out.

Was this article helpful?
