Simple read and write code for rc522

Hi guys. I hope you can help me with my problem. I'm planning to build a system where a barcode scanner scans and the data read is transmitted to LabVIEW, and then LabVIEW will send the data to the arduino to be written in a PICC card whenever it scans a card. i have seen sample codes on miguelbalboa rfid forum but i didn't see a code where the input will be variable. the barcode data will be sent so that it can be written to the PICC card when rc522 detects a card. but the codes i see used constant values to be written in the card. i'm not familiar with C-programming. that's why i want to ask help from in short, i need a code for rc522 that can write data with variable input from a serial. and also a reader to read the data written.BTW, only the code for read and write is what i need. i can handle the labview program to communicate the two softwares... thanks in advance :slight_smile:

How is that constant ID represented in the code? If it is a string constant, you can replace it by a variable, which you fill with input from Serial or some other input device, then write it to the card.

Hi sir DrDiettrich, this is the portion of the code where the constant value is.


int block=2;//this is the block number we will write into and then read. Do not write into 'sector trailer' block, since this can make the block unusable.

byte blockcontent[16] = {"11111111"};//an array with 16 bytes to be written into one of the 64 card blocks is defined!!!!!!!!!!

Is there any way i can make it a variable input so that if i scan from my barcode scanner thru labview, i can send it to the arduino using serial? without running the arduino.exe. If it scans a card, it will automatically write the data that was passed from labview via the serial. Thanks sir.

You have to put the characters, received from Serial, into the blockcontent array. Like this:

 for (int i = 0; i < 16; i++) {
   while (Serial.available() == 0) ; //wait for the next character
   blockcontent[i] =; //get and store the character

Some synchonization code should be added, so that above loop is guaranteed to start at the begin of an ID, transmitted from LabView, and has received all 16 bytes of the ID. See the many examples for parsing input from Serial.

Ok sir. i will. thanks for your reply. :slight_smile: i will post here again if i have any more questions. thanks sir!

Hint: You can add a Karma point for every helpful answer.