PN532, ESP32, MicroPython, and Me! (I need help)

I have an ESP32 running the MicroPython firmware and have met the blinkenlights, said hello to the world, all the usual goodies. What fun. I am now attempting to connect to a PN532 via SPI interface and I am running out of sticks with which to poke it. If any of you fine folks have experience setting up that combo, I could use a hand.

What I have:

  • I have an Elechous PN532 NFC reader board [manual here].
  • I have them connected following this diagram:
  • I have the MicroPython port of the PN532 drivers and demo code from here
  • I have read this post where it would appear the OP is connecting theirs the exact same way and have adjusted the spi_dev assignment to match theirs.

What I get:

Traceback (most recent call last):
  File "main.py", line 18, in <module>
  File "nfc_drivers/NFC_PN532.py", line 307, in get_firmware_version
  RuntimeError: Failed to detect the PN532

What I’ve tried:

  • Debugging indicates that it gets into the _wait_ready method then times out on the while loop. I flipped it to a while True just to make sure it wasn’t a race condition and it still fails.
  • The Elechous manual [linked above] says that this board uses 115200 baud so I changed that in the sample code’s spi_dev assignment
  • I have frankly tried every permutation of the two DIP switches to set SPI mode, but are these the right positions?
    image

I’m sure you’ve done this already - but just in case, I’d doublecheck the pinouts. What board was that demo code running on originally as D5 on that may not actually be the D5 as labelled on your ESP.

Edit: sorry not sure if that is much help. The PN532 has a built-in level shifter right? i.e. it’s not a 3.3 vs 5v issue?

1 Like

I won’t rule anything out. I’m just stepping out so I’ll get some better hookup pics when I get home, but this is what I have at the moment.



The Pn532 does claim to have a level shifter. The specs I saw claimed it would work on 3.3, and the power led comes on at 3.3v, but I can try that. :person_shrugging:

1 Like

I take that back, I am willing to rule out faulty connections. I’ve also used a continuity tester from pin to pin (leads on the back soldered side of each connected pin), so it’s not the wires, connections, breadboard, bad/faulty solders in the headers, etc.

1 Like

I suspect I know what it is, but I’ll walk you through my journey to see the troubleshooting process I used.

You linked to the driver github:

I looked at the only defined pin, CS, and traced it back on your diagram. Goes to pin 5, looks fine.

Your link to the forum post is faulty, so I googled micropython pn532 and found what I assume you were trying to link to:
https://forum.micropython.org/viewtopic.php?t=7152#p40672

The thing that jumped out at me there was the first argument of the SPI declaration. The example github and linked post both say SPI(1), while the following post says SPI(2). Generally those first arguments are the SPI peripheral number, for microcontrollers that have a few SPI drivers at their disposal, usually with different pin configurations.

Googling that along with ESP32 confirms:
https://docs.micropython.org/en/latest/esp32/quickref.html#hardware-spi-bus

Then checking a handy quick ref:

GPIO19 is an easy one to check. It corresponds to VSPI instead of HSPI.

So, in conclusion, make sure you’re using SPI(2) to initialise your driver.

4 Likes

That could very much be it. I misunderstood the purpose of the “id” parameter in the SPI call, and set it to 1 thinking it was like dev1. That makes sense, and I appreciate your explanation. I’ll try it out this afternoon!

1 Like

It’s always so handy to have a Jarrett around.

1 Like

It’s always so handy to have a Jarrett around.

For sure!

Yes, that forum post (and the one right after it) were the ones I was trying to link to but screwed up. Unfortunately, the SPI id is not the (only) problem.

1 Like

Gerdermit. Any chance the power supply is less than 1 amp? Might need more pixies.

edit: p.s I know you’ll figure this out.

Everything looks good to me, now. At this point I usually recommend the $10 Amazon / AliExpress logic analyzer, which is honestly the highest utility-to-price ratio of any tool I’ve ever had.

2 Likes

I’m powering the ESP32 off the USB connection to my laptop (power and programming it off the one cable) and the PN532 off the ESP32. So that’s possible? IIRC USB ports are rated at .5A?

I’ll check them out, thanks.

1 Like

I know I’ve banged my head on the desk with ESP boards only to realize later I didn’t have enough juice.

Keep us posted - I have a few ESP32s here so I’m curious to see what the solution is.

1 Like

Analyzer should be here tomorrow or Monday.

1 Like

The Rigol scope in the space has a built-in logic analyzer.

2 Likes

There is also a Saleae logic analyzer at the space. Works great. I believe there’s also a Bus Pirate which is handy for snooping on serial buses.

1 Like

Alright, well, my cheapo Amazon one came in. I have no idea what I’m doing with hit yet, but I’ll start poking at it and see what I can find. Cheers, folks.

3 Likes