Arduino projects

Arduino and PN532 NFC module

Arduino and PN532 NFC module

In this article, we will use a development board based on Arduino and PN532 NFC module – Maduino Zero NFC board. Maduino Zero NFC board is an IoT solution based on Atmel’s 32-bit SAMD21 MCU and NXP near field communication controller PN532. This development board can be used in payment applications, entrance systems, and security system projects.

Arduino and PN532 NFC module

Maduino Zero NFC is based on the Arduino framework, and users can use the Arduino IDE to program it quickly. This article will help you understand the basic usage of Maduino Zero NFC as well as hardware overview and programming.

RFID technology can be used over long distances, while NFC technology is limited to short distances. NFC is best used to securely transmit a sequence of data over short distances, so it is common in access control and payment applications. RFID, on the other hand, is more suitable for fast-moving environments with a large number of moving parts, and is most commonly used for vehicle access control and asset management purposes.

What is NFC?

Near-Field Communication (NFC), full name Near-Field Communication , refers to technology that allows enabled devices that are close to each other to share data wirelessly. NFC technology evolved from radio frequency identification (RFID) technology and is more refined.

Arduino and PN532 NFC module

NFC operates on the principle of inductive coupling, at least for short-range implementations. This basically involves the card reader device creating a magnetic field by passing an electric current through a coil. When a tag (with its own coil) is brought nearby, the field induces a current within the tag. Then, once the initial handshake is complete, any data stored on the tag is wirelessly transmitted to the reader.

Introduction to Maduino Zero NFC Development Board

Maduino Zero NFC is a small Arduino-compatible development board with NFC (Near Field Communication) functionality. It is designed based on the ATSAMD21G18 controller and PN532 NFC chip, which is actually used by the Arduino Zero board, so Maduino Zero NFC can also be used as an Arduino Zero development board.

Arduino and PN532 NFC module

 

This development board will be a bridge to help you interact wirelessly with the environment, drive motors, unlock, control relays, and read data from temperature sensors. That’s because it has built-in NFC connectivity right out of the box.

Arduino and PN532 NFC module

The development board has a Micro USB port for serial communication and power supply. We can connect 5V power input or solar panel to charge the lithium battery. It also has a 3.7V lithium polymer battery connector and a power switch for turning the module on/off. NFC antenna comes with IPX interface for scanning NFC tags. The status indicator LED is connected to D2 and has a power indicator LED. There is a SWD switch on the board for downloading the bootloader. You can reset the ATSAMD21G18 controller by pressing the reset button on the board.

There is an SD card slot on the back of the board. You can insert an SD card in the slot and store the necessary data in text format.

How to use Maduino NFC development board?

The Maduino NFC board has an Arduino Zero controller and PN532 NFC chip. To use this development board in Arduino IDE, we need to set up Arduino IDE first.

The ATSAMD21G18A development board support package is not pre-installed in the Arduino IDE. Therefore, we need to install “Arduino Zero Board” from the Board Manager.

Open the Boards Manager in the Arduino IDE menu , select the menu Tools->Board->Boards Manager… to open the Boards Manager dialog box. Then install Arduino SAMD Boards (32-bits ARM Cortex-M0+) .

Arduino and PN532 NFC module

Once the installation is complete, you can now select the Arduino Zero development board as shown in the image below. To program this development board, a Micro USB data cable needs to be connected.

Arduino and PN532 NFC module

Next, hardware connection is required. First, insert the NFC antenna into the interface of the ANT1 identifier. Then plug the Micro USB cable into the Maduino Zero NFC. Use a 4-pin female header to connect the 0.96-inch I2C OLED display to the circuit board.

After uploading the program to the development board, you can use a 3.7V lithium battery for power since it works between 3.4V and 4.2V. Both batteries and MicroUSB can be used for power. When Micro USB is connected, the board is powered by Micro USB, and when Micro USB is unplugged, it automatically switches to battery.

After the hardware is connected, it needs to be programmed. Select the Arduino Zero board from the Arduino IDE menu. Then select the correct serial port number. Now you can upload the following code to the Maduino development board.

When compiling code using Arduino IDE, you need to install some library files. Download the listed library files and add them to the Arduino IDE via the library manager:

1.    Adafruit PN532 Library

2.    Adafruit GFX Library

3.    SSD1306 OLED library

The complete code used in this article is given at the end of the article. Download and copy to Arduino IDE, compile and upload.

Effect test

After the code is uploaded successfully, you can start testing. After powering on, the OLED display will print out that the firmware version is 1.6 and request to scan the card.

Now place some NFC cards near the antenna of the Maduino Zero development board. The NFC card can be your bank card or travel card, etc.

When you place the card near the antenna, it will read the byte length as well as the UID value and display them on the OLED screen.

If you don’t want your project to use an OLED display, you can also display the UID value and byte length on the serial monitor.

The above is how to use the Maduino Zero NFC development board based on Arduino and PN532 NFC module to implement payment security system applications. The complete code used in this article:

#include <Wire.h>
#include <SPI.h>
#include “Adafruit_PN532.h”
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define PN532_SS (9)
#define PN532_IRQ (2)
#define PN532_RESET (11) // Not connected by default on the NFC Shield

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128×64, 0x3C for 128×32

Adafruit_PN532 nfc(PN532_SS);

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#if defined(ARDUINO_ARCH_SAMD)
#define Serial SerialUSB
#endif

#define STA_LED 2 //D2

void setup(void)
{
pinMode(STA_LED, OUTPUT);
digitalWrite(STA_LED, HIGH); // turn the LED off (HIGH is the voltage level)

#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(115200);
SerialUSB.begin(115200);

if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(F(“SSD1306 allocation failed”));
for (;;); // Don’t proceed, loop forever
}

nfc.begin();

uint32_t versiondata = nfc.getFirmwareVersion();

if (! versiondata)
{
SerialUSB.print(“Didn’t find PN53x board”);
while (1); // halt
}

// Got ok data, print it out!
SerialUSB.print(“Found chip PN5”);
SerialUSB.println((versiondata >> 24) & 0xFF, HEX);
SerialUSB.print(“Firmware ver. “);
SerialUSB.print((versiondata >> 16) & 0xFF, DEC);
SerialUSB.print(‘.’);
SerialUSB.println((versiondata >> 8) & 0xFF, DEC);

display.clearDisplay();
display.setCursor(0, 0); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.print(“Found chip PN5”);
display.print((versiondata >> 24) & 0xFF, HEX);

display.setCursor(0, 20); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.print(“Firmware ver. “);
display.print((versiondata >> 16) & 0xFF, DEC);
display.print(“.”);
display.print((versiondata >> 8) & 0xFF, DEC);

nfc.setPassiveActivationRetries(0xFF);

// configure board to read RFID tags
nfc.SAMConfig();

SerialUSB.println(“Waiting for an ISO14443A card”);

display.setCursor(0, 40); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.print(“Waiting for NFC Card”);
display.display();

}

void loop(void)
{
boolean result_success = 0;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

result_success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);

if (result_success)
{
SerialUSB.println(“Found a card!”);
SerialUSB.print(“UID Length: “);
SerialUSB.print(uidLength, DEC);
SerialUSB.println(” bytes”);
SerialUSB.print(“UID Value: “);

display.clearDisplay();
display.setCursor(10, 0); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.print(“UID Length:”);
display.print(uidLength, DEC);
display.print(” bytes”);

display.setCursor(35, 20); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(“UID Value: “);
display.setCursor(5, 35); //oled display

for (uint8_t i = 0; i < uidLength; i++)
{
SerialUSB.print(“0x”);
SerialUSB.print(uid[i], HEX);

display.print(” 0x”);
display.print(uid[i], HEX);
display.display();
}
SerialUSB.println(“”);
digitalWrite(STA_LED, LOW);// turn the LED on (LOW is the voltage level)
result_success = 0;
// Wait 1 second before continuing
delay(1000);
}
else
{
// PN532 probably timed out waiting for a card
SerialUSB.println(“Timed out waiting for a card”);
}
digitalWrite(STA_LED, HIGH); // turn the LED off (HIGH is the voltage level)
}

Related Articles

Back to top button