PN532 NFC Library [PORTED]

If we are going for the cheapest we can find… here’s one:

The NFC looks pretty cool for a keyless entry or user authentication system.

Looking forward to seeing how people use this stuff.

I’ve used this nfc target board from TI - the NFC chip itself is very cheap, the target board I believe was around 16$ or so. So that would be a way cheaper option and the board works over i2c or SPI if I remember it correctly.

In my opinion the hard and complex part is to come up with a decent library that wraps the low-level NDEF creation functions. For typical tasks like putting a URL on a dynamic nfc tag or reading a URL that was put onto a NFC tag and touched the reader, the NDEF format has to be parsed. Besides accessing the correct registers for turning the tag in read/write/whatever mode, that was most complicated for me.

Here’s a blog post I once created, might show a bit more details:

1 Like

I found that with the Arduino, the Elechouse module was the cheapest and easiest to get going.
It was a case of plug and play, and it worked perfectly.

@hansamann- In regards to NDEF tags - you’re right - they’re complicated, and most of the tutorials online on how to craft them are totally and simply wrong. I’ve worked extensively with tags over the last year or so (we embedded over 200,000 tags last year!) - and wrote a simple NDEF library to assist.

This one is written in PHP, but I also have a JS version somewhere.

Hope it’s of assistance.

hi @BDub!

So i have the NFC reader, the PN532 model, and this is their instruction:

I would like to use this module with your code, can you help me with the wiring? Not sure how should i connect it to the Spark Core and which one should i use from the table in order to get it work with your ported code (

Your help would be much appreciated, can’t wait to get this baby work :slight_smile:

Kind regards,

Here are the connections:

Now there is typically a way to set it to I2C mode as well… so if you could link me to which module you have exactly, I can help with that. Unless that part is more obvious to you, then just just go for it! :smile:

hey @Bdub!


So here is the manual for the nfc reader:

I connected as you suggested, i also set it to i2c mode (switch 1 to ON, kept switch 2 on OFF).

Added your code to the core, but when i read a card it’s not reporting anything on the serial port. Do you think it’s normal or i’m screwing something?

hey @Bdub!

I gave another try and now it reads the NFC tags.

Two doubts:

    1. I think it’s not really stable coz sometimes it reads, sometimes it’s not. Do you think i should add any pull up resistor to D1/D0 to streamline operation? Can that be an issue?
    1. If i keep the card on the reader it keeps reading it continuously. Is there any way to add a stop after the card was readed? (with either hardware or software?)

Thanks for your help!


I should have ordered one of these cards 12 days ago when I ported the library :frowning: Good news is I just ordered one from Adafruit. The PN532 shield… so I’ll have it soon enough and work out all of the bugs. The I2C might still be flaky, and SPI might be a better way to go right now. However the port I did is not SPI enabled… so some work will need to be done to get both modes in one library. There are several libraries Adafruit has that can be ported as well… so once I get the reader things will be much easier to code and debug. If you can hang in there I’ll work on this more when I get the reader.[quote=“Noten, post:28, topic:2675”]
Do you think i should add any pull up resistor to D1/D0 to streamline operation? Can that be an issue?

I guess if it communicates at all it must have pull up resistors on that board. I don’t see a schematic for their board though to verify. You could try adding a 4.7k pull up to each I2C pin just for good measure. From D0 to VIN and D1 to VIN.

Typically you could just add a variable to your code that you can initially set to false before reading, and after you get a successful read you set it to true. Then just don’t read any more if it’s true. You can make it automatically reset back to false after a time out. You just have to think about how you want the action to work, and then figure out a way to code it. For instance, if you drop a tag on your scanner, but you don’t want it to read multiple times, but do want it to read again right away if you pull the tag away and put it back… you have to see how fast it’s reading when sitting on the scanner vs. how long it takes you to pull the card away and put it back. Hopefully the multiple read time is much faster than the pulling away and putting back time, because you’d want your timeout value to be somewhere in between those two. Hopefully that makes sense?

I can provide an example once I get my stuff :smile:

Just found the schematic… looks like you need to add some pull ups. Try 4.7k like I suggested above. They level shift to 3.3V so you can really connect the pull ups to VIN (4.8V) or 3V3 (3.3V). The point is to just get a nice fast transition to high level when the D0 and D1 lines go HI-Z (open).

hi @BDub!

That’s great news, let me know how it goes! I can beta test anything :smiley:

Thanks for the tips as well!

I will perform a few more tests and will try to modify your code not to read the same continuously. Just one question, do you know how to put the UDID into a variable? The code nfc.puthex(buf+1, buf[0]); writes it directly to the serial port, and not sure how can it be pushed into a variable instead.

Kind regards,

hi @BDub!

Just tried the library with a galaxy note 3, but it seems it’s reading the tag randomly. Not sure whether it’s the issue with the nfc chip in the android or with the code…

Do you have any experience with it?

Kind regards,

Not yet! :slight_smile: Still waiting on some hardware. I probably won’t be able to help much until next week. :frowning:

EDIT: Adafruit finally shipped my stuff 2 days after I ordered it. I miss the days when you would get an email about an hour after your order that it was shipping.

1 Like

Hi guys, I’m trying the same Elechouse board. It works fine with an Arduino so at least I have working hardware. I connected like this:

Core    PN532
D0      SDA with 10K pull-up resistor to +3.3V
D1      SCL with 10K pull-up resistor to +3.3V
3.3V    VCC

Used @BDub code and also his nice trick to put a delay to start the serial monitor. I flash the firmware, get the LED on to tell me to start the serial monitor and finally I just get:

MF1S50 Reader Demo From Elechouse!
Didn't find PN53x board

Do you have any suggestions what I might be doing wrong? I think the problem with the 7 bit I2C addresses is solved now so I am not sure what else to try.

hi @adrian!

I didn’t use any pull up resistors for d0 and d1, did you try like that? @BDub’s code just worked fine with my elechouse rfid reader (i flashed it from the web interface).

I’m not sure how i connected, but maybe you can try connecting the VCC to the core’s VIN port. I just remember that i directly connected the reader with the core and worked fine without any pull-up resistors.

Give it a try, let us know how it goes.


Hi @Noten, I removed the resistors, but same. With #define PN532DEBUG I ran the test with nothing connected, I got

Sending: 00 00 FF 02 FE D4 02 2A 00 
IRQ received
Reading: FF FF FF FF FF FF 
No ACK frame received!
Didn't find PN53x board

With the board connected, it changes only one line to

Reading: 80 80 80 80 80 80

But don’t know if that means anything useful

hi @adrian!

Sorry, I’m also not a pro and don’t know what can possibly go wrong :frowning: Hope somebody else will be able to help you.


Well, I have no clue what happened, I connected everything again and it works with the same connections that I mentioned above, so will get to testing now the PN532, thanks @BDub for the initial port that helps me get started.

1 Like

Any update on getting SPI working in the library?? I’m using this:

I’m at a hackathon, so if there is a way to get something working today - I’d be more than excited :stuck_out_tongue:

I’m guessing your not at the hackathon anymore… if only this forum had THAT kind of support, that would be amazing. You could call into a 1-800-SPARK-ME and ask “Tank, I need an exit!” and you know… a phone would ring somewhere… no no no, a Spark Squad representative would guide you through your current issue.

Just wanted to update that I got the NFC board from Adafruit finally. I do not recommend USPS shipping from them :stuck_out_tongue: Not to mention that there is a USPS sorting facility in Forrest Park, IL that has a resemblance to the Bermuda Triangle… packages go in and don’t come out for a week or more, if they are ever seen again!

I’m trying to finish up a project that was fairly involved before I get into testing this though. Hopefully I can be done tonight with that, maybe even have time left to get started tonight on this.