Getting a PN532 NFC to work on UART
Posted: Sat Feb 17, 2018 7:53 am
This is a summary of getting a PN532 to work on the UART to help other who might have the same problem.
I used this arduino library for the PN532 https://github.com/elechouse/PN532
It basically worked first time on the SPI but I only had 3 pins left so tried to switch to UART 2.
Changed this in the PN532 "iso14443 example"
The first surprise was that the default pins can not be used. This can be changed in one of 2 ways, change the defaults in begin function in the file:
<install directory>\Arduino\hardware\espressif\esp32\cores\esp32\HardwareSerial.cpp
Alternatively the pins can be specified in the begin function call in PN532_HSU.cpp as follows (this will probably result in a compile error if you try to use the library with another arduio that is not an ESP32)
Note: the best would be to change the begin function to add the pins as arguments,e.g. ser.begin(rxPin,txPin);
I initially called in setup() but that does not work since begin() is called again in the PN532 begin code and over write these values. On a scope I could see that the data send is correct and that the PN532 replies correctly. I initially thought that it is a baud rate problem on the receive side but after some experimentation found that I should not call the begin() function in setup(), it should only be called from the PN532_HSU begin().
Currently I am not sure if the problem is because serial1.begin() was called twice or because set HardwareSerial::changeBaud() is called twice (indirectly through begin). I suspect the latter because of the error reported in https://github.com/espressif/arduino-esp32/issues/441
Hope this helps somebody.
I used this arduino library for the PN532 https://github.com/elechouse/PN532
It basically worked first time on the SPI but I only had 3 pins left so tried to switch to UART 2.
Changed this in the PN532 "iso14443 example"
Code: Select all
#elif 1
#include <PN532_HSU.h>
#include <PN532.h>
HardwareSerial Serial1(1); <------------------ add this
PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);
<install directory>\Arduino\hardware\espressif\esp32\cores\esp32\HardwareSerial.cpp
Code: Select all
if(_uart_nr == 1 && rxPin < 0 && txPin < 0) {
rxPin = 16; <---------------------your pin number
txPin = 15; <--------------------- your pin number
}
Code: Select all
void PN532_HSU::begin()
{
_serial->begin(115200,SERIAL_8N1,16,13); <------- your pin assignments
}
I initially called
Code: Select all
Serial1.begin(115200, SERIAL_8N1, 16, 15);
Currently I am not sure if the problem is because serial1.begin() was called twice or because set HardwareSerial::changeBaud() is called twice (indirectly through begin). I suspect the latter because of the error reported in https://github.com/espressif/arduino-esp32/issues/441
Hope this helps somebody.