how to read only UID of the rfid tag using RC522 RFID reader

can any one tell me what i have done mistake..??

there is no syntax error but some logic error is there..

my code

#include<SPI.h>
#include<MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10

MFRC522 mfrc522(SS_PIN, RST_PIN);
unsigned long UID[3];
unsigned long UID1;

void setup() {
Serial.begin(9600);
//while(!Serial);
SPI.begin();

}

void loop() {
if(!mfrc522.PICC_IsNewCardPresent())
{
return;
}
if(!mfrc522.PICC_ReadCardSerial())
{
return ;
}

}

for (int i = 0; i < 3; i++) {
UID = mfrc522.uid.uidByte*;*
_ UID1 = UID*;
}*
Serial.println("hi your uid is ");
Serial.print(UID1,HEX);_

can any one tell me what i have done mistake..??

Mistake number 1. No [­code] ... [­/code] tags around the code
Mistake number 2. Not posting the whole program
Mistake number 3. Not describing the problem.

Please help us to help you.

The problem is that you don't tell your Arduino which bytes you want to read, do it like this.

UID[i] = mfrc522.uid.uidByte[i];

Get rid of the UID1 and do this and add this in the same for loop.

Serial.print(UID[i], HEX)

Let me know if it fixed your problem.

akatchi:
The problem is that you don't tell your Arduino which bytes you want to read, do it like this.

Let me know if it fixed your problem.

No, the problem is they didn't use code tags, so the subscripts got rendered as italics

AWOL:
No, the problem is they didn't use code tags, so the subscripts got rendered as italics

Oh sorry :(, my bad

hope it ll help other too...i got it correctly..
here code for getting only UID of RFID tad in decimal form as well as hexadecimal form..

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
String id1="";
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
}

void loop() {
id1="";
if (mfrc522.PICC_IsNewCardPresent()&&mfrc522.PICC_ReadCardSerial()) {

for (byte i = 0; i < mfrc522.uid.size; i++) {
unsigned long id = (mfrc522.uid.uidByte*);*

  • id1 =id1 + id;*
  • Serial.print(id,HEX);*
  • //Serial.print(" ");*
  • }*
  • Serial.print("\n");*
  • Serial.print(id1); //to print id of Tag in serial monitor*
  • mfrc522.PICC_HaltA();*
  • Serial.print("\n"); //to give newline to seril output*
    }
    }

tenzin:
hope it ll help other too...i got it correctly..
here code for getting only UID of RFID tad in decimal form as well as hexadecimal form..

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
String id1="";
void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
}

void loop() {
id1="";
if (mfrc522.PICC_IsNewCardPresent()&&mfrc522.PICC_ReadCardSerial()) {

for (byte i = 0; i < mfrc522.uid.size; i++) {
unsigned long id = (mfrc522.uid.uidByte*);*

  • id1 =id1 + id;*
  • Serial.print(id,HEX);*
  • //Serial.print(" ");*
  • }*
  • Serial.print("\n");*
  • Serial.print(id1); //to print id of Tag in serial monitor*
  • mfrc522.PICC_HaltA();*
  • Serial.print("\n"); //to give newline to seril output*
    }
    }
    [/quote]
    Did you read what we said... edit your post and put it between the tags...

sorry i don't know

no it didn't work..
Its easy to print the UID but to assign to some varible i did my own way..

thank you..

i modified the example code ReadNUID which only prints UID hope it helps

/*
 * --------------------------------------------------------------------------------------------------------------------
 * Example sketch/program showing how to read new NUID from a PICC to serial.
 * --------------------------------------------------------------------------------------------------------------------
 * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
 * 
 * Example sketch/program showing how to the read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
 * Reader on the Arduino SPI interface.
 * 
 * When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
 * then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
 * you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
 * will show the type, and the NUID if a new card has been detected. Note: you may see "Timeout in communication" messages
 * when removing the PICC from reading distance too early.
 * 
 * @license Released into the public domain.
 * 
 * Typical pin layout used:
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       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 SS_PIN 10
#define RST_PIN 9
 
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key; 

// Init array that will store new NUID 
byte nuidPICC[4];

void setup() { 
  Serial.begin(9600);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522 

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }

 
}
 
void loop() {

  // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;



 for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   
  printHex(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
   rfid.PICC_HaltA();

  
  rfid.PCD_StopCrypto1();
}



void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

Uh... maybe the serial.print should be in the loop function?