| 1 | /**************************************************************************/ |
| 2 | /*! |
| 3 | @file iso14443a_uid.pde |
| 4 | @author Adafruit Industries |
| 5 | @license BSD (see license.txt) |
| 6 | |
| 7 | This example will attempt to connect to an ISO14443A |
| 8 | card or tag and retrieve some basic information about it |
| 9 | that can be used to determine what type of card it is. |
| 10 | |
| 11 | Note that you need the baud rate to be 115200 because we need to print |
| 12 | out the data and read from the card at the same time! |
| 13 | |
| 14 | This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards |
| 15 | This library works with the Adafruit NFC breakout |
| 16 | ----> https://www.adafruit.com/products/364 |
| 17 | |
| 18 | Check out the links above for our tutorials and wiring diagrams |
| 19 | These chips use SPI or I2C to communicate. |
| 20 | |
| 21 | Adafruit invests time and resources providing this open source code, |
| 22 | please support Adafruit and open-source hardware by purchasing |
| 23 | products from Adafruit! |
| 24 | |
| 25 | */ |
| 26 | /**************************************************************************/ |
| 27 | #include <Wire.h> |
| 28 | #include <SPI.h> |
| 29 | #include <Adafruit_PN532.h> |
| 30 | |
| 31 | // If using the breakout with SPI, define the pins for SPI communication. |
| 32 | #define PN532_SCK (2) |
| 33 | #define PN532_MOSI (3) |
| 34 | #define PN532_SS (4) |
| 35 | #define PN532_MISO (5) |
| 36 | |
| 37 | // If using the breakout or shield with I2C, define just the pins connected |
| 38 | // to the IRQ and reset lines. Use the values below (2, 3) for the shield! |
| 39 | #define PN532_IRQ (2) |
| 40 | #define PN532_RESET (3) // Not connected by default on the NFC Shield |
| 41 | |
| 42 | // Uncomment just _one_ line below depending on how your breakout or shield |
| 43 | // is connected to the Arduino: |
| 44 | |
| 45 | // Use this line for a breakout with a SPI connection: |
| 46 | Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS); |
| 47 | |
| 48 | // Use this line for a breakout with a hardware SPI connection. Note that |
| 49 | // the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's |
| 50 | // hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are |
| 51 | // SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin. |
| 52 | //Adafruit_PN532 nfc(PN532_SS); |
| 53 | |
| 54 | // Or use this line for a breakout or shield with an I2C connection: |
| 55 | //Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); |
| 56 | |
| 57 | void setup(void) { |
| 58 | Serial.begin(115200); |
| 59 | while (!Serial) delay(10); // for Leonardo/Micro/Zero |
| 60 | Serial.println("Hello!"); |
| 61 | |
| 62 | nfc.begin(); |
| 63 | |
| 64 | uint32_t versiondata = nfc.getFirmwareVersion(); |
| 65 | if (! versiondata) { |
| 66 | Serial.print("Didn't find PN53x board"); |
| 67 | while (1); // halt |
| 68 | } |
| 69 | |
| 70 | // Got ok data, print it out! |
| 71 | Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); |
| 72 | Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); |
| 73 | Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); |
| 74 | |
| 75 | // Set the max number of retry attempts to read from a card |
| 76 | // This prevents us from waiting forever for a card, which is |
| 77 | // the default behaviour of the PN532. |
| 78 | nfc.setPassiveActivationRetries(0xFF); |
| 79 | |
| 80 | Serial.println("Waiting for an ISO14443A card"); |
| 81 | } |
| 82 | |
| 83 | void loop(void) { |
| 84 | boolean success; |
| 85 | uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID |
| 86 | uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) |
| 87 | |
| 88 | // Wait for an ISO14443A type cards (Mifare, etc.). When one is found |
| 89 | // 'uid' will be populated with the UID, and uidLength will indicate |
| 90 | // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) |
| 91 | success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength); |
| 92 | |
| 93 | if (success) { |
| 94 | Serial.println("Found a card!"); |
| 95 | Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); |
| 96 | Serial.print("UID Value: "); |
| 97 | for (uint8_t i=0; i < uidLength; i++) |
| 98 | { |
| 99 | Serial.print(" 0x");Serial.print(uid[i], HEX); |
| 100 | } |
| 101 | Serial.println(""); |
| 102 | // Wait 1 second before continuing |
| 103 | delay(1000); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | // PN532 probably timed out waiting for a card |
| 108 | Serial.println("Timed out waiting for a card"); |
| 109 | } |
| 110 | } |