RFID compatibility issue with RF transmitter

Hey Everyone

Having issues with using the MFRC522 RFID reader with the RF transmitter using VirtualWire or Radiohead libraries.

I am trying to read the NUID from the RFID then send it by RF to another Arduino. I have combined a stripped down version of the ReadNUID example and examples from instructables for the RF transmitter. Both work fine seperately so I know it is wired correctly and that the code should be fine.

I don't think it is a power issue as they are the only two things connected to the Arduino nano?
Could it be a library issue?

It compiles and uploads fine however I did some error testing and as soon as it gets to the following lines it seems to just stop. Not sure if it crashes or is in an infinite loop.

For Virtualwire

vw_setup(4000);

For Radiohead

driver.send((uint8_t *)field, strlen(field));

The codes I used are below,
Virtualwire code

#include <SPI.h>
#include <MFRC522.h>
#include <VirtualWire.h>
#define SS_PIN 10
#define RST_PIN 9
char *field;
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key; 

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

void setup() { 
  Serial.begin(9600);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522 
  Serial.println(F("This code scan the MIFARE Classsic NUID."));
  Serial.println();
  vw_set_ptt_inverted(true); //
  vw_set_tx_pin(4);
  vw_setup(4000);// speed of data transfer Kbps
  
}
void loop() {
 
  // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

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

  if (rfid.uid.uidByte[0] != nuidPICC[0] || 
    rfid.uid.uidByte[1] != nuidPICC[1] || 
    rfid.uid.uidByte[2] != nuidPICC[2] || 
    rfid.uid.uidByte[3] != nuidPICC[3] ) {

    Serial.println(F("A new card has been detected."));
    
    if (rfid.uid.uidByte[0] == 54 || 
      rfid.uid.uidByte[1] == 70 || 
      rfid.uid.uidByte[2] == 247 || 
      rfid.uid.uidByte[3] == 48 ) {
      field = "1";
    }
    if (rfid.uid.uidByte[0] == 133 || 
      rfid.uid.uidByte[1] == 68 || 
      rfid.uid.uidByte[2] == 238 || 
      rfid.uid.uidByte[3] == 197 ) {
      field = "2";
    }
    
    Serial.println(F("The NUID tag is:"));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
    vw_send((uint8_t *)field, strlen(field));
    vw_wait_tx(); // Wait until the whole message is gone
  }
  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
  
}

/**
 * Helper routine to dump a byte array as dec values to Serial.
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

Radiohead code

#include <SPI.h>
#include <MFRC522.h>
#include <RH_ASK.h>
#define SS_PIN 10
#define RST_PIN 9
char *field;
RH_ASK driver;
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key; 

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

void setup() { 
  Serial.begin(9600);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522 
  Serial.println(F("This code scan the MIFARE Classsic NUID."));
  Serial.println();


  
}
void loop() {
 
  // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

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

  if (rfid.uid.uidByte[0] != nuidPICC[0] || 
    rfid.uid.uidByte[1] != nuidPICC[1] || 
    rfid.uid.uidByte[2] != nuidPICC[2] || 
    rfid.uid.uidByte[3] != nuidPICC[3] ) {

    Serial.println(F("A new card has been detected."));
    
    if (rfid.uid.uidByte[0] == 54 || 
      rfid.uid.uidByte[1] == 70 || 
      rfid.uid.uidByte[2] == 247 || 
      rfid.uid.uidByte[3] == 48 ) {
      field = "1";
    }
    if (rfid.uid.uidByte[0] == 133 || 
      rfid.uid.uidByte[1] == 68 || 
      rfid.uid.uidByte[2] == 238 || 
      rfid.uid.uidByte[3] == 197 ) {
      field = "2";
    }
    
    Serial.println(F("The NUID tag is:"));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
    driver.send((uint8_t *)field, strlen(field));
    driver.waitPacketSent();
  }
  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
  
}

/**
 * Helper routine to dump a byte array as dec values to Serial.
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}
byte nuidPICC[3];

  if (rfid.uid.uidByte[0] != nuidPICC[0] ||
    rfid.uid.uidByte[1] != nuidPICC[1] ||
    rfid.uid.uidByte[2] != nuidPICC[2] ||
    rfid.uid.uidByte[3] != nuidPICC[3] ) {

Why are you comparing the first 4 bytes in a 3 element array?

Where do you ever assign a value to nuidPICC?

It is just redundant code left over from the example I used. I could probably just get rid of it.

It was left over from when it did a check to see if it is reading the same card but I want to be able to read the card more than once.

Can't think of any reason that would be causing a problem unless I am missing something?

It is just redundant code left over from the example I used. I could probably just get rid of it.

And possibly resolve problems caused by accessing beyond the end of the array.

A link to the RFID reader you are using would be useful. There may be a pin conflict happening.

Hey Paul,

Thanks for your help.

I am using the MFRC522 RFID reader

and I am using this 433MHz RF transmitter.

I will post up a picture of it wired up as well when I get home.

I couldn't get a good pic that showed all the wiring well so I made it in Fritzing.

PaulS:
Why are you comparing the first 4 bytes in a 3 element array?

I fixed the declaration of the array to this,

byte nuidPICC[4];

It didn't change anything unfortunately.

Ok I am an idiot and I realized what is going wrong but I still am struggling to get it to work.

When I turn the RF transmitter on it interferes with the RFID reader :blush: .

So new plan.What I am trying to do is turn on the RFID reader then read from the RFID reader then turn it off then turn on the RF transmitter send the signal then turn the RF transmitter off. Then repeat.

I am having trouble getting it to work though.

It works for one loop but when I start the second loop through it won't read from the RFID reader.

Does anyone have any advice?

I got it to work. It is VERY crude and I want to make it better but it works. There didn't seem to be much interest in this post but if anyone needs to make a remote handheld RFID reader then this might give them a starting point.

#include <SPI.h>
#include <MFRC522.h>
#include <VirtualWire.h>
#define SS_PIN 10
#define RST_PIN 9
char *field;
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
  Serial.println(F("This code scan the MIFARE Classsic NUID."));
  Serial.println();
  rfid.PCD_Init(); // Init MFRC522
}
void loop() {
  
   
  // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

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

  if (rfid.uid.uidByte[0] != nuidPICC[0] || 
    rfid.uid.uidByte[1] != nuidPICC[1] || 
    rfid.uid.uidByte[2] != nuidPICC[2] || 
    rfid.uid.uidByte[3] != nuidPICC[3] ) {

    Serial.println(F("A new card has been detected."));
    
    if (rfid.uid.uidByte[0] == 54 || 
      rfid.uid.uidByte[1] == 70 || 
      rfid.uid.uidByte[2] == 247 || 
      rfid.uid.uidByte[3] == 48 ) {
      field = "0";
    }
    if (rfid.uid.uidByte[0] == 133 || 
      rfid.uid.uidByte[1] == 68 || 
      rfid.uid.uidByte[2] == 238 || 
      rfid.uid.uidByte[3] == 197 ) {
      field = "1";
    }
    
    Serial.println(F("The NUID tag is:"));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();

  }
  //Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
  
  vw_set_ptt_inverted(true); //
  vw_set_tx_pin(4);
  vw_setup(4000);// speed of data transfer Kbps
  vw_send((uint8_t *)field, strlen(field));
  vw_wait_tx(); // Wait until the whole message is gone
  software_Reset();
}

/**
 * Helper routine to dump a byte array as dec values to Serial.
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}
void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
  asm volatile ("  jmp 0");  
}

There didn't seem to be much interest in this post

What you mean is that YOU were not interested in answering the questions that were asked.

A link to the RFID reader you are using would be useful.

Still no link...

Hey Paul,

Meant no disrespect. I meant that not many people seemed to have had the same problem as I do or was currently struggling with the same problem.

I appreciated your help.

I posted a picture and the name of the RFID reader I am using. I thought that was what you were after? If it was a link to the actual place I bought it from then I am not even sure where I bought it from. Off ebay I think and it didn't have any technical information. My understanding was that it was just a generic MFRC 522 which I got information from a variety of sources.