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: bool = False, irq: digitalio.DigitalInOut | None = None, reset: digitalio.DigitalInOut | None = None)

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

Create an instance of the PN532 class

SAM_configuration() None

Configure the PN532 to read MiFare cards.

call_function(command: int, response_length: int = 0, params: array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray = b'', timeout: float = 1) bytes | bytearray | None

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.

property firmware_version: Tuple[int, int, int, int]

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

get_passive_target(timeout: float = 1) bytes | bytearray | None

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: int = micropython.const, timeout: float = 1) bool

Send command to PN532 to begin listening for a Mifare card. This returns True if the command was received successfully. 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_add_value_block(block_number: int, amount: int) bool

Increase the balance of a value block. Block number should be the block to change and amount should be an integer up to a maximum of 2147483647. If the value block is successfully updated then True is returned, otherwise False is returned.

mifare_classic_authenticate_block(uid: array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray, block_number: int, key_number: Literal[96, 97], key: array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray) bool

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_fmt_value_block(block_number: int, initial_value: int, address_block: int = 0) bool

Formats a block on the card so it is suitable for use as a value block. Block number should be the block to use. Initial value should be an integer up to a maximum of 2147483647. Address block is optional and can be used as part of backup management.

mifare_classic_get_value_block(block_number: int) int

Read the contents of a value block and return a integer representing the current balance. Block number should be the block to read.

mifare_classic_read_block(block_number: int) bytes | bytearray | None

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_sub_value_block(block_number: int, amount: int) bool

Decrease the balance of a value block. Block number should be the block to change and amount should be an integer up to a maximum of 2147483647. If the value block is successfully updated then True is returned, otherwise False is returned.

mifare_classic_write_block(block_number: int, data: array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray) bool

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: int) bytes | bytearray | None

Read a block of data from the card. Block number should be the block to read. If the block is successfully read the first 4 bytes (after the leading 0x00 byte) will be returned. If the block is not read then None will be returned.

ntag2xx_write_block(block_number: int, data: array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray) bool

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() bool

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: int, response_length: int = 0, timeout: float = 1) bytes | bytearray | None

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: int = micropython.const, timeout: float = 1) bytearray | None

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() None

Perform a hardware reset toggle and then wake up the PN532

send_command(command: int, params: array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray = b'', timeout: float = 1) bool

Send specified command to the PN532 and wait for an acknowledgment. Will wait up to timeout seconds for the acknowledgment and return True. If no acknowledgment 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: busio.I2C, address: int = micropython.const, *, irq: digitalio.DigitalInOut | None = None, reset: digitalio.DigitalInOut | None = None, req: digitalio.DigitalInOut | None = None, debug: bool = False)

Driver for the PN532 connected over I2C.

Create an instance of the PN532 class using I2C. Note that PN532 uses clock stretching. Optional IRQ pin (not used), resetp pin and debugging output.

Parameters:
  • i2c (I2C) – The I2C bus the PN532 is connected to.

  • address (int) – The I2C device address. Defaults to 0x24

  • irq (digitalio.DigitalInOut) – board pin the PN532 IRQ is connected to

  • reset (digitalio.DigitalInOut) – board pin the PN532 RSTOUT_N is connected to

  • req (digitalio.DigitalInOut) – board pin the PN532 P32 is connected to

  • debug (bool) – if True print additional debug statements. Defaults to False

Quickstart: Importing and using the device

Here is an example of using the PN532_I2C class. First you will need to import the libraries to use the sensor

import board
import busio
from digitalio import DigitalInOut
from adafruit_pn532.i2c import PN532_I2C

Once this is done you can define your board.I2C object and define your object

i2c = busio.I2C(board.SCL, board.SDA)
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)
# Configure PN532 to communicate with MiFare cards
pn532.SAM_configuration()

Now you have access to the attributes and functions of the PN532 RFID/NFC shield or breakout

uid = pn532.read_passive_target(timeout=0.5)

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: busio.SPI, cs_pin: digitalio.DigitalInOut, *, irq: digitalio.DigitalInOut | None = None, reset: digitalio.DigitalInOut | None = None, debug: bool = 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.

Create an instance of the PN532 class using SPI Optional IRQ pin (not used)

Parameters:
  • spi (SPI) – The spi bus the PN532 is connected to.

  • cs (digitalio.DigitalInOut) – board pin the PN532 chip select line is connected to

  • irq (digitalio.DigitalInOut) – board pin the PN532 P32 is connected to

  • reset (digitalio.DigitalInOut) – board pin the PN532 RSTOUT_N is connected to

  • debug (bool) – if True print additional debug statements. Defaults to False

Quickstart: Importing and using the device Here is an example of using the PN532_SPI class. First you will need to import the libraries to use the sensor

import board
import busio
from digitalio import DigitalInOut
from adafruit_pn532.spi import PN532_SPI

Once this is done you can define your busio.SPI object and define your PN532 object

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

Now you have access to the attributes and functions of the PN532 RFID/NFC shield or breakout

uid = pn532.read_passive_target(timeout=0.5)
adafruit_pn532.spi.reverse_bit(num: int) int

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: busio.UART, *, reset: digitalio.DigitalInOut | None = None, debug: bool = False)

Driver for the PN532 connected over Serial UART

Create an instance of the PN532 class using Serial connection. Optional reset pin and debugging output.

Parameters:
  • uart (UART) – The uart object the PN532 is connected to.

  • reset (digitalio.DigitalInOut) – board pin the PN532 RSTOUT_N is connected to

  • debug (bool) – if True print additional debug statements. Defaults to False

Quickstart: Importing and using the device

Here is an example of using the PN532_I2C class. First you will need to import the libraries to use the sensor

import board
import busio
from digitalio import DigitalInOut
from adafruit_pn532.uart import PN532_UART

Once this is done you can define your busio.UART object and define your PN532 object

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

Now you have access to the attributes and functions of the PN532 RFID/NFC shield or breakout

uid = pn532.read_passive_target(timeout=0.5)