Introduction

Documentation Status Discord Build Status

CircuitPython driver for the PN532 NFC/RFID Breakout and PN532 NFC/RFID Shield

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-pn532

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-pn532

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .env
source .env/bin/activate
pip3 install adafruit-circuitpython-pn532

Usage Example

Check examples/pn532_simpletest.py for usage example

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

Documentation

For information on building library documentation, please check out this guide.

Table of Contents

Simple test

Ensure your device works with this simple test.

examples/pn532_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This example shows connecting to the PN532 with I2C (requires clock
stretching support), SPI, or UART. SPI is best, it uses the most pins but
is the most reliable and universally supported.
After initialization, try waving various 13.56MHz RFID cards over it!
"""

import board
import busio
from digitalio import DigitalInOut

#
# NOTE: pick the import that matches the interface being used
#
from adafruit_pn532.i2c import PN532_I2C

# from adafruit_pn532.spi import PN532_SPI
# from adafruit_pn532.uart import PN532_UART

# I2C connection:
i2c = busio.I2C(board.SCL, board.SDA)

# Non-hardware
# pn532 = PN532_I2C(i2c, debug=False)

# With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
# harware reset
reset_pin = DigitalInOut(board.D6)
# On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
# wakeup! this means we don't need to do the I2C clock-stretch thing
req_pin = DigitalInOut(board.D12)
pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)

# SPI connection:
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
# cs_pin = DigitalInOut(board.D5)
# pn532 = PN532_SPI(spi, cs_pin, debug=False)

# UART connection
# uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=100)
# pn532 = PN532_UART(uart, debug=False)

ic, ver, rev, support = pn532.firmware_version
print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))

# Configure PN532 to communicate with MiFare cards
pn532.SAM_configuration()

print("Waiting for RFID/NFC card...")
while True:
    # Check if a card is available to read
    uid = pn532.read_passive_target(timeout=0.5)
    print(".", end="")
    # Try again if no card is available.
    if uid is None:
        continue
    print("Found card with UID:", [hex(i) for i in uid])

adafruit_pn532

This module will let you communicate with a PN532 RFID/NFC shield or breakout using I2C, SPI or UART.

  • Author(s): Original Raspberry Pi code by Tony DiCola, CircuitPython by ladyada

Implementation Notes

Hardware:

Software and Dependencies:

exception adafruit_pn532.adafruit_pn532.BusyError

Base class for exceptions in this module.

class adafruit_pn532.adafruit_pn532.PN532(*, debug=False, irq=None, reset=None)

PN532 driver base, must be extended for I2C/SPI/UART interfacing

SAM_configuration()

Configure the PN532 to read MiFare cards.

call_function(command, response_length=0, params=[], timeout=1)

Send specified command to the PN532 and expect up to response_length bytes back in a response. Note that less than the expected bytes might be returned! Params can optionally specify an array of bytes to send as parameters to the function call. Will wait up to timeout seconds for a response and return a bytearray of response bytes, or None if no response is available within the timeout.

firmware_version

Call PN532 GetFirmwareVersion function and return a tuple with the IC, Ver, Rev, and Support values.

get_passive_target(timeout=1)

Will wait up to timeout seconds and return None if no card is found, otherwise a bytearray with the UID of the found card is returned. listen_for_passive_target must have been called first in order to put the PN532 into a listening mode.

It can be useful to use this when using the IRQ pin. Use the IRQ pin to detect when a card is present and then call this function to read the card’s UID. This reduces the amount of time spend checking for a card.

listen_for_passive_target(card_baud=<sphinx.ext.autodoc.importer._MockObject object>, timeout=1)

Send command to PN532 to begin listening for a Mifare card. This returns True if the command was received succesfully. Note, this does not also return the UID of a card! get_passive_target must be called to read the UID when a card is found. If just looking to see if a card is currently present use read_passive_target instead.

mifare_classic_authenticate_block(uid, block_number, key_number, key)

Authenticate specified block number for a MiFare classic card. Uid should be a byte array with the UID of the card, block number should be the block to authenticate, key number should be the key type (like MIFARE_CMD_AUTH_A or MIFARE_CMD_AUTH_B), and key should be a byte array with the key data. Returns True if the block was authenticated, or False if not authenticated.

mifare_classic_read_block(block_number)

Read a block of data from the card. Block number should be the block to read. If the block is successfully read a bytearray of length 16 with data starting at the specified block will be returned. If the block is not read then None will be returned.

mifare_classic_write_block(block_number, data)

Write a block of data to the card. Block number should be the block to write and data should be a byte array of length 16 with the data to write. If the data is successfully written then True is returned, otherwise False is returned.

ntag2xx_read_block(block_number)

Read a block of data from the card. Block number should be the block to read. If the block is successfully read a bytearray of length 16 with data starting at the specified block will be returned. If the block is not read then None will be returned.

ntag2xx_write_block(block_number, data)

Write a block of data to the card. Block number should be the block to write and data should be a byte array of length 4 with the data to write. If the data is successfully written then True is returned, otherwise False is returned.

power_down()

Put the PN532 into a low power state. If the reset pin is connected a hard power down is performed, if not, a soft power down is performed instead. Returns True if the PN532 was powered down successfully or False if not.

process_response(command, response_length=0, timeout=1)

Process the response from the PN532 and expect up to response_length bytes back in a response. Note that less than the expected bytes might be returned! Will wait up to timeout seconds for a response and return a bytearray of response bytes, or None if no response is available within the timeout.

read_passive_target(card_baud=<sphinx.ext.autodoc.importer._MockObject object>, timeout=1)

Wait for a MiFare card to be available and return its UID when found. Will wait up to timeout seconds and return None if no card is found, otherwise a bytearray with the UID of the found card is returned.

reset()

Perform a hardware reset toggle and then wake up the PN532

send_command(command, params=[], timeout=1)

Send specified command to the PN532 and wait for an acknowledgment. Will wait up to timeout seconds for the acknowlegment and return True. If no acknowlegment is received, False is returned.

adafruit_pn532.i2c

This module will let you communicate with a PN532 RFID/NFC shield or breakout using I2C.

  • Author(s): Original Raspberry Pi code by Tony DiCola, CircuitPython by ladyada,
    refactor by Carter Nelson
class adafruit_pn532.i2c.PN532_I2C(i2c, *, irq=None, reset=None, req=None, debug=False)

Driver for the PN532 connected over I2C.

adafruit_pn532.spi

This module will let you communicate with a PN532 RFID/NFC shield or breakout using SPI.

  • Author(s): Original Raspberry Pi code by Tony DiCola, CircuitPython by ladyada,
    refactor by Carter Nelson
class adafruit_pn532.spi.PN532_SPI(spi, cs_pin, *, irq=None, reset=None, debug=False)

Driver for the PN532 connected over SPI. Pass in a hardware or bitbang SPI device & chip select digitalInOut pin. Optional IRQ pin (not used), reset pin and debugging output.

adafruit_pn532.spi.reverse_bit(num)

Turn an LSB byte to an MSB byte, and vice versa. Used for SPI as it is LSB for the PN532, but 99% of SPI implementations are MSB only!

adafruit_pn532.uart

This module will let you communicate with a PN532 RFID/NFC shield or breakout using UART.

  • Author(s): Original Raspberry Pi code by Tony DiCola, CircuitPython by ladyada,
    refactor by Carter Nelson
class adafruit_pn532.uart.PN532_UART(uart, *, reset=None, debug=False)

Driver for the PN532 connected over Serial UART

Indices and tables