Advertisement
dhruvag2000

readRFID.py

Mar 21st, 2022
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Code tested and is working as of 21/01/2022 for BP
  3. # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
  4. # SPDX-License-Identifier: MIT
  5.  
  6. """
  7. This example shows connecting to the PN532 with I2C (requires clock
  8. stretching support), SPI, or UART. SPI is best, it uses the most pins but
  9. is the most reliable and universally supported.
  10. After initialization, try waving various 13.56MHz RFID cards over it!
  11. """
  12.  
  13. import board
  14. import busio
  15. from digitalio import DigitalInOut
  16.  
  17. #
  18. # NOTE: pick the import that matches the interface being used
  19. #
  20. from adafruit_pn532.i2c import PN532_I2C
  21.  
  22. # from adafruit_pn532.spi import PN532_SPI
  23. # from adafruit_pn532.uart import PN532_UART
  24.  
  25. # I2C connection:
  26. i2c = busio.I2C(board.SCL, board.SDA)
  27.  
  28. # Non-hardware
  29. # pn532 = PN532_I2C(i2c, debug=False)
  30.  
  31. # With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
  32. # harware reset
  33. reset_pin = DigitalInOut(board.D6)
  34. # On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
  35. # wakeup! this means we don't need to do the I2C clock-stretch thing
  36. req_pin = DigitalInOut(board.D12)
  37. pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
  38.  
  39. # SPI connection:
  40. # spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
  41. # cs_pin = DigitalInOut(board.D5)
  42. # pn532 = PN532_SPI(spi, cs_pin, debug=False)
  43.  
  44. # UART connection
  45. # uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=100)
  46. # pn532 = PN532_UART(uart, debug=False)
  47.  
  48. ic, ver, rev, support = pn532.firmware_version
  49. print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
  50.  
  51. # Configure PN532 to communicate with MiFare cards
  52. pn532.SAM_configuration()
  53.  
  54. print("Waiting for RFID/NFC card...")
  55. while True:
  56.     # Check if a card is available to read
  57.     uid = pn532.read_passive_target(timeout=0.5)
  58.     print(".", end="")
  59.     # Try again if no card is available.
  60.     if uid is None:
  61.         continue
  62.     print("Found card with UID:", [hex(i) for i in uid])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement