Write to serial port by detecting a RFID card

Hi! I am working on a project, in which I want Processing to show a specific image, when Arduino detects a RFID card. I got four different cards, and four different images: each card has one corresponding image. So, I thought the solution would be pretty easy: connect a specific code (maybe the NUID) to each card that will be written to the serial port, when the card is detected.

However, I am stuck. The RFID system is much more complicated than I expected and all the codes I found on the internet were about reading or writing data on the card itself.

Has someone any idea how to solve this?

P.S. Actually, the screens I want to connect to the cards are a bit more difficult. I have different screens and want them to appear on a tablet (so I connect Processing to a tablet). However, that is not important for this part, since the principle is the same: what is needed is that a code is connected to each card and written to the serial port after scanning it.

(deleted)

What kind of RFID cards do you have? What RFID reader? Almost all readers come with libraries to read the data, unless the RFID reader is a serial device.

Have you managed to read an ID from a card and display it on the Serial monitor ?
What type of card and reader are you using ?
Which Arduino board are you using ?

Please post any code that you have tried and describe any problems with it.

Woops, sorry, stupid... Never posted something before here (surprise).

I'm using a RFID-RC522. Furthermore, I don't use an Arduino, but a Teensy 3.2.

Jep, I managed to read an ID from the cards and display it on the Serial monitor. The library 'MFRC522' has some examples, one of them is reading the NUID.

Also, I have a code that should work in the way I want to. However, when I connect it, the serial monitor shows squares (like undefined symbols) that appear in a row.
I attached the code to this reply. Maybe, you cannot run it since you're missing the required library. Through this link: http://www.instructables.com/id/Arduino-RFID-Reader-MFRC522-Turorial/ you can find a zip file, that you should use to install the library.

The array 'cards' contains a list of numbers that are connected to one (in this case out of 5) cards. In the void loop is checked whether the scanned card contains a list which corresponds to one of the lists inside 'cards'. If so, it writes the number of the place inside the array 'cards' to the serial port. If not, it writes '6' to the serial port.

Code_Armando.ino (1.2 KB)

when I connect it, the serial monitor shows squares (like undefined symbols) that appear in a row.

What baud rate have you got the Serial monitor set to ?

It must match the baud rate specified in Serial.begin() in the program.

Both at 9600.

The array 'cards' contains a list of numbers that are connected to one (in this case out of 5) cards. In the void loop is checked whether the scanned card contains a list which corresponds to one of the lists inside 'cards'. If so, it writes the number of the place inside the array 'cards' to the serial port. If not, it writes '6' to the serial port.

That will be a problem if you decide to add more cards. Writing -1 for card not known means that that value will remain constant, no matter how many cards are added.

Thanks for the tip. I'm going to use 4 cards probably, so it wouldn't form a problem, but it results in a neater code :).

 else
  {
    Serial.write(6);
  }

This is causing the squares. Why the program should print it I do not know.

Try this example from the Instructables page

#include <RFID.h>

/*
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS/SDA: Pin 10
* RST: Pin 9
*/

#include <SPI.h>
#include <RFID.h>

#define SS_PIN 10
#define RST_PIN 9

RFID rfid(SS_PIN, RST_PIN);


int led = 7;
int power = 8;
int serNum[5];
int cards[][5] =
{
  {5, 117, 21, 219, 190}

};

bool access = false;

void setup()
{
  Serial.begin(115200);
  SPI.begin();
  rfid.init();
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop()
{
  if (rfid.isCard())
  {
    if (rfid.readCardSerial())
    {
      Serial.print(rfid.serNum[0]);
      Serial.print(" ");
      Serial.print(rfid.serNum[1]);
      Serial.print(" ");
      Serial.print(rfid.serNum[2]);
      Serial.print(" ");
      Serial.print(rfid.serNum[3]);
      Serial.print(" ");
      Serial.print(rfid.serNum[4]);
      Serial.println("");
      for (int x = 0; x < sizeof(cards); x++)
      {
        for (int i = 0; i < sizeof(rfid.serNum); i++ )
        {
          if (rfid.serNum[i] != cards[x][i])
          {
            access = false;
            break;
          }
          else
          {
            access = true;
          }
        }
        if (access) break;
      }
    }
    if (access)
    {
      Serial.println("Welcome!");
      digitalWrite(led, HIGH);
      delay(1000);
      digitalWrite(led, LOW);
      digitalWrite(power, HIGH);
      delay(1000);
      digitalWrite(power, LOW);
    }
    else
    {
      Serial.println("Not allowed!");
      digitalWrite(led, HIGH);
      delay(500);
      digitalWrite(led, LOW);
      delay(500);
      digitalWrite(led, HIGH);
      delay(500);
      digitalWrite(led, LOW);
    }
  }
  rfid.halt();
}

UKHeliBob:
This is causing the squares. Why the program should print it I do not know.

Try this example from the Instructables page

When I run this code, I don't get anything on my Serial Monitor. Since my cards don't correspond to the card of this code, (I thought) I should get the text "Not allowed!", but nothing shows up.

By the way, when I replace the '6' for '-1' I got mirrored question marks instead of squares. This does not solve anything, but maybe it helps someone understanding what goes wrong.

When I run this code, I don't get anything on my Serial Monitor.

What baud rate have you got the Serial monitor set to ? Note the baud rate in the code that I posted.

By the way, when I replace the '6' for '-1' I got mirrored question marks instead of squares. This does not solve anything, but maybe it helps someone understanding what goes wrong.

What is wrong is that you are writing control characters to the Serial monitor. Serial.println() some text instead.
l
Presumably you want to send it via Serial to Processing but get it on the screen first then you can tackle getting it to Processing.

UKHeliBob:
What baud rate have you got the Serial monitor set to ? Note the baud rate in the code that I posted.

Yea, I changed that to 9600 already. In case you are running the code yourself as well (since you need the RFID reader and stuff probably not, but no harm, no foul), do you get the text "Not allowed!" after running?

UKHeliBob:
Presumably you want to send it via Serial to Processing but get it on the screen first then you can tackle getting it to Processing.

Yes, I agree with that. When I replace Serial.write() for Serial.println() I got the exact same situation, but now a '6' that keeps appearing. A difference is that it is not showing up at one line anymore, but scrolls down. However, I assume that is caused because it is printed now (instead of written) to the serial port.

Yes I am running the code and yes I get "Not allowed!"

Check the 7 connections to the RFID reader very carefully, Which pin on the Arduino does each pin on the reader connect to ?

UKHeliBob:
Yes I am running the code and yes I get "Not allowed!"

That's so damn strange (and frustrating).

UKHeliBob:
Check the 7 connections to the RFID reader very carefully, Which pin on the Arduino does each pin on the reader connect to ?

Yeah, checked that like a hundred times:
Pin 9 - RST
Pin 10 - SDA (same as SS)
Pin 11 - MOSI
Pin 12 - MISO
Pin 13 - SCK
Which means that IRQ is not connected. Furthermore, GND is connected to the ground of Teensy and 3.3V to 3.3V of Teensy. All pins (9 until 13) are digital pins at the Teensy, so that should not be a problem.

The only thing I can imagine is that something is wrong inside the RFID-RC522 reader or Teensy itself, don't you think so? Or is that impossible since I can run codes, like reading the NUID?

(I rebuilt the circuit at another place of my board, because I thought maybe something inside the board could be broken. It had no effect.)

Your connections are the same as mine but I am using a Nano

UKHeliBob:
Your connections are the same as mine but I am using a Nano

Mm, I came up with another idea. The code I use to read the NUID is an example of the MFRC522 library. However, the code from that website runs on the library you can add by downloading the zip file. Maybe that is where it goes wrong? I added the library by downloading the zip file and click on 'Sketch' --> 'Include Library' --> 'Add .ZIP library' --> Selecting the zip file. Did I made a mistake here?

And one more question. In the circuit, I only included the MFRC522 reader that I connected to my Teensy. The circuit of that site is also using LED and a TIP31 transistor. Although, I thought the code should just work, only without turning on a LED, right? Did you also only used the MFRC522 reader with your Arduino Nano?

I added the library by downloading the zip file and click on 'Sketch' --> 'Include Library' --> 'Add .ZIP library' --> Selecting the zip file.

That sounds right.

Note that the code I posted that works for me and not for you uses a basic RFID library

The library files have this comment in them

/* RFID.h - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT.
 * Based on code Dr.Leong   ( WWW.B2CQSHOP.COM )
 * Created by Miguel Balboa (circuitito.com), Jan, 2012. 
 */

but I have no idea where I downloaded it from

UKHeliBob:
Note that the code I posted that works for me and not for you uses a basic RFID library

But you also had to download the ZIP library of the link I sent, right? (Here is the link again http://www.instructables.com/id/Arduino-RFID-Reader-MFRC522-Turorial/ ). I added two libraries within Arduino for this code. The one inside that ZIP file and the MRFC522 that you can find by clicking on "Managing Libraries".

UKHeliBob:
but I have no idea where I downloaded it from

I think that this one is the MRFC522 library. If you click on 'More information', you're sent to this page GitHub - miguelbalboa/rfid: Arduino RFID Library for MFRC522 . It is indeed created by Miguel Balboa. Which version of this library are you using?

Furthermore, I was trying to take another code and start working from there. I picked the code for reading the NUID (see below).

#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;
  }

  Serial.println(F("This code scan the MIFARE Classsic NUID."));
  Serial.print(F("Using the following key:"));
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
}
 
void loop() {

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

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

  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  Serial.println(rfid.PICC_GetTypeName(piccType));

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    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."));

    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   
    Serial.println(F("The NUID tag is:"));
    Serial.print(F("In hex: "));
    printHex(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
    Serial.print(F("In dec: "));
    printDec(rfid.uid.uidByte, rfid.uid.size);
    Serial.println();
  }
  else Serial.println(F("Card read previously."));

  // Halt PICC
  rfid.PICC_HaltA();

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


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

/**
 * 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);
  }
}

I decided to focus on the DEC instead of HEX, so I removed the HEX part. Also, you can remove all the sentences that are printed to the serial monitor like "This code scan the MIFARE Classsic NUID." and stuff. Makes it easier to read. Eventually, you end up with this code:

#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;

  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    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] ) {

    // Store NUID into nuidPICC array
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   
    printDec(rfid.uid.uidByte, rfid.uid.size);
  }
  else Serial.println(F("Card read previously."));

  // 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);
  }
}

On the bottom of the code, the void can be found with which printDec is created. When scanning a card, it only gives a specific number of the card (provided by the void printDec). However, I don't succeed in getting it written to the serial port instead of printed. Tried different things (inside the void loop and the void printDec), but it does not work. Do you see a solution inside this code?

P.S. The message was too long, so I removed the explanatory comments that were shown at head of the code. I will post them as a new message.

I don't succeed in getting it written to the serial port instead of printed. Tried different things (inside the void loop and the void printDec), but it does not work. Do you see a solution inside this code?

Sorry, but I am not very familiar with Serial comms on the Teensy but using Serial.print() should output to the USB port and hence the Serial monitor.

But you also had to download the ZIP library of the link I sent, right?

No. I already had the RFID library on my system.

Note that the code I posted that works for me does not #include MFRC522.h and it is also not #included by the RFID library