Skip to content

Instantly share code, notes, and snippets.

@virgilvox
Created March 26, 2015 23:24
Show Gist options
  • Save virgilvox/7a49fc9de2d238137185 to your computer and use it in GitHub Desktop.
Save virgilvox/7a49fc9de2d238137185 to your computer and use it in GitHub Desktop.
Mifare Print UID
/*
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key;
/**
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
}
byte test;
/**
* Main loop.
*/
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()){
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()){
return;
}
// Show some details of the PICC (that is: the tag/card)
// if(test != mfrc522.uid.uidByte[0]){
// test = mfrc522.uid.uidByte[0];
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
delay(1000);
// }
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment