Google Chat: zj734465502@gmail.com
+86-0755-88291180
sales01@spotpear.com
dragon_manager@163.com
services01@spotpear.com
manager01@spotpear.com
WhatsApp:13246739196
This is a Raspberry Pi NFC HAT based on PN532 operating in the 13.56MHz frequency range. It supports three communication interfaces: I2C, SPI, and UART.
NFC (Near Field Communication) is a wireless technology allows contactless point-to-point data communication between devices within a short distance of 10 cm. It is widely used in applications such as access control system, smart tickets, meal card, etc.
Based on the popular NFC controller PN532 with multi interface options, this HAT will easily enable NFC function for your Raspberry Pi.
You can quick test the module by connecting it to PC with USB to TTL module instead of Raspberry Pi
PN532 NFC HAT | USB to TTL Module |
---|---|
3V3 | 3.3V |
GND | GND |
TX | RX |
RX | TX |
55 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF 03 FD D4 14 01 17 00
(Please refer to PN532 User Manual HSU wake up condition Chapter)
The response from PN532 module should be:
00 00 FF 00 FF 00 00 00 FF 02 FE D5 15 16 00
00 00 FF 04 FC D4 4A 01 00 E1 00
Closing card to coil part of module, module scan it and response:
00 00 FF 0C F4 D5 4B 01 01 00 04 08 04 XXXXXXXXXX 00
XXXXXXXXXX in response data is ID (3 bytes) and checksum (1 byte) of card.(Please refer to PN532 User Manual InListPassiveTarget Chapter)
PN532 NFC HAT supports UART, I2C and SPI interface. You can use them according to your situation.After connecting PN532 NFC HAT (hereafter called as PN532) to Raspberry PI, then set the L1, L0 jumpers and DIP switch for different interfaces.
Choose the interface:
Download demo code from #Resources, unzip and copy raspberrypi folder to /home/pi of Raspberry Pi. You can firstly copy it to /boot of SD card, then copy it to /home/pi.
1. Set L0 toL and L1 to H by jumpers
2. Connect RSTPDN->D20 by jumper
3. Set DIP switch to
SCK | MISO | MOSI | NSS | SCL | SDA | RX | TX |
ON | ON | ON | ON | OFF | OFF | OFF | OFF |
4. Connect PN532 NFC HAT to Raspberry Pi
PN532 NFC HAT | Raspberry Pi (BCM) |
---|---|
SCK | SCK |
MISO | MISO |
MOSI | MOSI |
NSS | P4 (D4) |
5. Enable SPI interface
Open Terminal of Raspberry Pi,use command: sudo raspi-config
Choose Interfacing Options -> SPI -> Yes
6. Run demo codes(Use example_get_uid.py and rpi_get_uid.c as example)
Open Terminal, navigate to directory of demo codes
cd ~/raspberrypi/
1) python code:
Enter directory of python codes: cd ~/raspberrypi/python/
Modify example_get_uid.py file,set the initialize code as :
pn532 = PN532_SPI(debug=False, reset=20, cs=4) #pn532 = PN532_I2C(debug=False, reset=20, req=16) #pn532 = PN532_UART(debug=False, reset=20)
Save after modifying, then run the codes with command:
python3 example_get_uid.py
2) C codes:
Enter directory of c code: cd ~/raspberrypi/c/example/
Modify rpi_get_uid.c file, set initialize code as:
PN532_SPI_Init(&pn532); //PN532_I2C_Init(&pn532); //PN532_UART_Init(&pn532);
Save and compile:sudo make
Run the code:
./rpi_get_uid.exe
7. Expected result:Close card to coil part of PN532, the UID of card is read
1. Set L0 to L and L1 to L by jumpers
2. Connecting RSTPDN ->D20 by jumper
3. Set DIP switch to
SCK | MISO | MOSI | NSS | SCL | SDA | RX | TX |
OFF | OFF | OFF | OFF | OFF | OFF | ON | ON |
4. Connect PN532 to Raspberry Pi
PN532 NFC HAT | Raspberry Pi |
---|---|
RX | TX |
TX | RX |
5. Enable Serial port. By default, serial port is used for Shell debugging.
Open Terminal of Raspberry PI and run command: sudo raspi-config
Choose Interfacing Options-> Serial -> No -> Yes
【Note】You need to restart Raspberry Pi after enabling serial port
6. Run demo codes(Use example_get_uid.py and rpi_get_uid.c as examples)
Open Terminal and navigate to directory of demo codes:
cd ~/raspberrypi/
1) python code:
Enter directory of python code: cd ~/raspberrypi/python/
Modify example_get_uid.py file, set initialize code to:
#pn532 = PN532_SPI(debug=False, reset=20, cs=4) #pn532 = PN532_I2C(debug=False, reset=20, req=16) pn532 = PN532_UART(debug=False, reset=20)
Save.Then run code by command:
python3 example_get_uid.py
2) C code:
Enter directory of c code:cd ~/raspberrypi/c/example/
Modify rpi_get_uid.c file, set initialize code to:
//PN532_SPI_Init(&pn532); //PN532_I2C_Init(&pn532); PN532_UART_Init(&pn532);
Save then compile code: sudo make
Run code:
./rpi_get_uid.exe
7. Expected result:Close card to coil part of PN532, the UID of card is read
cd ~/raspberrypi/python/ python3 example_uart_hex.py
Enter data and sent, data sent and received should be printed on terminal as expected. e.g. sent data below to wake up PN532:
55 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF 03 FD D4 14 01 17 00
The response data should be:
00 00 FF 00 FF 00 00 00 FF 02 FE D5 15 16 00
1. Set K0 as H, L1 as L by jumpers
2. Connect RSTPDN ->D20 and INT0 -> D16(avoid from Clock Stretching) by jumpers
3. Set DIP switch to
SCK | MISO | MOSI | NSS | SCL | SDA | RX | TX |
OFF | OFF | OFF | OFF | ON | ON | OFF | OFF |
4. Connect PN532 to Raspberry Pi
PN532 NFC HAT | Raspberry Pi |
---|---|
SCL | SCL |
SDA | SDA |
5. Enable I2C interface
Open Terminal of Raspberry Pi and run command: sudo raspi-config
Choose Interfacing Options-> I2C -> Yes
6. Run code(Use example_get_uid.py and rpi_get_uid.c as examples)
Open ternimal and navigate to directory of demo codes:
cd ~/raspberrypi/
1) python code:
Enter directory of python code: cd ~/raspberrypi/python/
Modify example_get_uid.py file, set initialize code to:
#pn532 = PN532_SPI(debug=False, reset=20, cs=4) pn532 = PN532_I2C(debug=False, reset=20, req=16) #pn532 = PN532_UART(debug=False, reset=20)
Save, then run code
python3 example_get_uid.py
2) C code:
Enter directory of c code:cd ~/raspberrypi/c/example/
Modify rpi_get_uid.c file, set initialize code to:
//PN532_SPI_Init(&pn532); PN532_I2C_Init(&pn532); //PN532_UART_Init(&pn532);
Save then compile codes:sudo make
Run code:
./rpi_get_uid.exe
7. Expected result:Close card to coil part of PN532, the UID of card is read
1. Make sure that you have installed Arduino IDE in your PC
2. Create a new folder in ...\Arduino\libraries (Installation directory of Arduino IDE) and named it as pn532
3. Copy files pn532.c, pn532.h, pn532_uno.cpp and pn532_uno.h to ...\Arduino\libraries\pn532 from Arduino demo codes
4. Demo codes is under examples\arduino (demo codes you download) directory
5. Herein we take Arduino UNO board as example
1. Set L0 to L and L1 to H by hy jumpers
2. Set DIP switch to:
SCK | MISO | MOSI | NSS | SCL | SDA | RX | TX |
ON | ON | ON | ON | OFF | OFF | OFF | OFF |
3. Connect PN532 NFC HAT to Arduino UNO:
PN532 NFC HAT | Arduino UNO |
---|---|
SCK | D13 |
MISO | D12 |
MOSI | D11 |
NSS | D4 |
4. Run codes(Use examples\arduino\uno_get_uid\ uno_get_uid.ino as examples):
Open uno_get_uid.ino file, set initialize code to:
PN532_SPI_Init(&pn532); //PN532_I2C_Init(&pn532);
Compile and upload codes to Arduino UNO
Open Serial monitor, press Reset button of Arduino Uno to reset
5. Expected result: close card to coil part of PN532, the UID of card is read and printed.
1. Set L0 to L and L1 to H by jumpers
2. Set DIP switch to
SCK | MISO | MOSI | NSS | SCL | SDA | RX | TX |
OFF | OFF | OFF | OFF | ON | ON | OFF | OFF |
3. Connect PN532 NFC HAT to Arduino UNO
PN532 NFC HAT | Arduino UNO |
---|---|
SCL | A5 |
SDA | A4 |
4. Run code(Use examples\arduino\uno_get_uid\ uno_get_uid.ino as example):
Open uno_get_uid.ino file, set initialize code to:
//PN532_SPI_Init(&pn532); PN532_I2C_Init(&pn532);
Compile and upload codes to Arduino UNO board
Open Serial monitor, press Reset button of Arduino UNO board to reset.
5. Expected result: close card to coil part of PN532, the UID of card is read and printed.
The development board used here is Open103C which is based on STM32F103CBT6.
1. Open project by keil software(...\MDK-ARM\pn532_stm32.uvprojx),Click Rebuild to compile project.
2. Choose programmer:Options for Target -> Debug-> Use,default: ST-Link Debugger。
3. Choose download method:Options for Target -> Debug选项卡 -> Settings -> Debug -> Port,Default: JTAG.
4. Connect Open board to PC by programmer. Note that you should power Open board separately.
5. Click Download to download project.
SPI connecting
1. Set L0 to L and L1 to H by jumpers
2. Set DIP switch to
SCK | MISO | MOSI | NSS | SCL | SDA | RX | TX |
ON | ON | ON | ON | OFF | OFF | OFF | OFF |
3. Connect PN632 to Open103C board
PN532 NFC HAT | STM32F103CBT6 |
---|---|
SCK | PA5 |
MISO | PA6 |
MOSI | PA7 |
NSS | PA4 |
I2C connecting
1. Set L0 to L and L1 to H by jumpers
2. Set DIP switch to
SCK | MISO | MOSI | NSS | SCL | SDA | RX | TX |
OFF | OFF | OFF | OFF | ON | ON | OFF | OFF |
3. Connect PN632 to Open103C board
PN532 NFC HAT | STM32F103CBT6 |
---|---|
SCL | PB6 |
SDA | PB7 |
Open project and choose the interface according to interface used. recompile and download
// PN532_SPI_Init(&pn532); PN532_I2C_Init(&pn532);
4. Connect USB to TTL module to UART1 interface (PA9->RX, PA10->TX) of STM32 and PC.
5. Open serial assistant software, and reset Open103C
6. Expected result: close card to coil part of PN532, the UID of card is read and printed.
Examples above are used to read UID of Mifare Classic card (example_get_uid.py / rpi_get_uid.exe / uno_get_uid.ino / stm32_get_uid), here we describe other examples.
【Note】
Before run the examples, you should set the L0/L1 pins and DIp switch according to interfaces used. You cannot set all the pins to ON,it will cause that data received are wrong. Here we take 0 is OFF and 1 is ON
Examples | Hardware platform |
example_dump_mifare.py | Raspberry Pi |
rpi_dump_mifare.c | Raspberry Pi |
uno_dump_mifare.ino | Arduino UNO |
stm32_dump_mifare/MDK-ARM/pn532_stm32.uvprojx | STM32F103CBT6 |
Expected result:Close the Mifare Classic card to PN532 NFC HAT, the data in card are printed:
0 : 37 F9 20 69 87 08 04 00 62 63 64 65 66 67 68 69 1 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 2 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3 : 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF 4 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 5 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7 : 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF 8 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 9 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 11 : 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF … … 63 : 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
Note:
e.g. To read block 6, we need to use cipher of the related sector, that is the data of block 7.
7 : 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
The first six bytes are KEY A, it is default FF FF FF FF FF FF, if you read it, the data response would be 00 00 00 00 00 00. The last six bytes are KEY B saved in plaintext. KEY A and KEY B of Mifare Classic card are default FF FF FF FF FF FF.
Four bytes in the middle are Access Bits, default FF 07 80 69. Access Bits are used for controlling access permission, it will be locked if user write wrong data to them.
For example, if you write 0xFF to byte 6, high four bits of byte 7 should be 0b0000,low four bits of byte 8 should be 0b0000. For more details, please refer to Access conditions for data blocks section of MF1S50YYX_V1.pdf
The card provided can be unlock by "back door" leaved, however, if you use common Mifare card, once you write wrong data to Access Bits, the card will be locked and unable to restore. Be careful when you writing card
Examples | Hardware Platform |
example_rw_mifare.py | Raspberry Pi |
rpi_rw_mifare.c | Raspberry Pi |
uno_rw_mifare.ino | Arduino UNO |
stm32_rw_mifare/MDK-ARM/pn532_stm32.uvprojx | STM32F103CBT6 |
Expected result: Close Mifare Classic card to PN532 NFC HAT, block 6 will be written with data 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F.
Note:
Examples | Hardware plaftform |
example_dump_ntag2.py | Raspberry Pi |
rpi_dump_ntag2.c | Raspberry Pi |
uno_dump_ntag2.ino | Arduino UNO |
stm32_dump_ntag2/MDK-ARM/pn532_stm32.uvprojx | STM32F103CBT6 |
Expected result: Close Ntag215 card to PN532 NFC HAT, data of the card will be printed
0: 04 85 32 3b 1: 92 a8 64 80 2: de 48 00 00 3: e1 10 3e 00 4: 03 00 fe 00 5: 00 00 00 00 6: 00 00 00 00 7: 00 00 00 00 … … 134: 00 00 00 00
Note:
Examples | Hardware platform |
example_rw_ntag2.py | Raspberry Pi |
rpi_rw_ntag2.c | Raspberry Pi |
uno_rw_ntag2.ino | Arduino UNO |
stm32_rw_ntag2/MDK-ARM/pn532_stm32.uvprojx | STM32F103CBT6 |
Expected result: Close Ntag215 card to PN532 NFC HAT, data 00 01 02 03 are written to Page 6.
Note:
Examples | Hardware platform |
example_write_gpio.py | Raspberry Pi |
rpi_write_gpio.c | Raspberry Pi |
uno_write_gpio.ino | Arduino UNO |
stm32_write_gpio/MDK-ARM/pn532_stm32.uvprojx | STM32F103CBT6 |
Expected result: Print status of PN532's GPIO
Pin P30: 1 Pin P31: 0 Pin P32: 1 Pin P33: 0 Pin P34: 1 Pin P35: 0 Pin P71: 0 Pin P72: 1 Pin I0: 1 Pin I1: 0
Note:
Examples will set GPIO:P30 -> HIHG,P31 -> LOW,P33 -> LOW,P35 -> LOW,P71 -> LOW,P72 -> HIGH
Note:
The states of PIN P30 -- P35 will be set to Hihg after hardware reset (RSTPDN is set to Low for 2s and then set to High)
Examples | Hardware platform |
example_read_gpio.py | Raspberry Pi |
rpi_read_gpio.c | Raspberry Pi |
uno_read_gpio.ino | Arduino UNO |
stm32_read_gpio/MDK-ARM/pn532_stm32.uvprojx | STM32F103CBT6 |
Expected result: Print states of PN532's GPIO
Port P3: 0x3f Port P7: 0x07 Port I: 0x07 Pin P30: 1 Pin P31: 1 Pin P32: 1 Pin P33: 1 Pin P34: 1 Pin P35: 1 Pin I0: 1 Pin I1: 0
Note:
The states of PIN P30 -- P35 will be set to Hihg after hardware reset (RSTPDN is set to Low for 2s and then set to High)