Getting the RFID-RC522 to work! [SOLVED]



yup. triple checked that… i’d love to tell you that i am doing something wrong but i’m quite good at following instructions :wink:

do you happen to use skype? we could check up on skype if you’d like ?
i think we might have a bug…


korneel, I looked through the RC522 specs and made some changes to setup() (that are most likely not required) but I believe I may have found the real problem. The Spark is a 32-bit processor and the Arduino is an 8-bit one so the size of variable “types” is different. So all “unsigned char” were changed to “uint8_t” but the potentially problematic one, “unsigned int”, was changed to “uint16_t”. Grab the entire code back from my github repo and give it another shot. If this doesn’t work, I will need one of those units to test cause I can’t find anything else wrong. :smile:



so there seems to be an error somewhere. when i try to use the new code i get

In file included from ../inc/spark_wiring.h:30:0,
  from ../inc/application.h:31,
  from RFID.h:9,
  from RFID.cpp:12:
 ../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
 RFID.cpp:22:11: error: expected constructor, destructor, or type conversion before '(' token
 RFID.cpp:36:1: error: prototype for 'RFID::RFID(int, int, uint8_t, uint8_t, uint8_t)' does not match any in class 'RFID'
 In file included from RFID.cpp:12:0:
 RFID.h:120:7: error: candidates are: constexpr RFID::RFID(RFID&&)
 RFID.h:120:7: error: constexpr RFID::RFID(const RFID&)
 RFID.h:125:5: error: RFID::RFID(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)
 RFID.h:123:5: error: RFID::RFID(uint8_t, uint8_t)
 make: *** [RFID.o] Error 1


Korneel, seems I had cut&paste problems. Reload RFID.cpp and it should compile now. :smile:


it compiles and flashes… but no card…

let’s talk on skype and get you one of these modules…


I ran the latest code on the GitHub repo and I can confirm that it’s working!


felixekman, when you say it’s working, do you mean working with an actual RC522 module like the one korneel is using???


i guess he means it compiles…
can you ping me on skype when you have a chance? i’ll get you a module.


Sorry for the late replay, I mean that it works with an actual RC522 module. I have one and tested it with your code and it runs wonderfully!


felixekman, thanks for the reply! korneel was finally able to get it to work. :smile:


and i once again want to thank Peekay123 for his help…

he is my new Spark god :smile:


ok, quick followup on this.

the values of the card are saved in an array, called RC522.serNum[i],HEX
this array has 5 values, 0 to 4.

now what i want to do is save this array to a single string, containing 10 HEX characters, so that i can compare the outcome to a valid card.

so what i tried is setting up a string and adding the values, but that was no good…

is there any way anyone can point me in the right direction ?

my end goals is to read a card, validate it’s a valid card, then proceed to do an action based on this. (switch a relais).



korneel, the serial number is ultimately a 5-digit hex value which can be represented by a unique integer value. So the first question is why 10 hex characters? Second is, why not convert the hex to an integer value and compare that? It will avoid doing (time consuming) String operations and reduce your code size.


Hi @korneel

Maybe @peekay123 and the other guys on this thread know what you want exactly, but it is not super clear for me. Over in another thread I was able to help decode a different RFID system card numbers to get the mapping between what is printed on the card and what the reader gets by having an example of both sides.

Something like with card 0x1234abcdef, you get an array like { 1, 2, 3, 4, 10}. Obviously these numbers are not correct, but I hope you get the idea.

Can you show us an example?

peekay123 is right that is will be easier to do the comparison with numbers rather than strings, but I know with the other system, they wanted to enter what was printed on the card in their program, rather than preprocess it into a number. Either way, with an example we could help more.


bam! i fixed it. did a simple fix…
will post code later…


bko, thinking about it I believe korneel was looking along the lines of what you did in the other thread. Since he’s fixed the problem, I suspect he stuck with strings. :smile:


@peekay123 , this is truly excellent work!

I have loaded the code from your github and it compiles and flashes just fine. However, I always get the " Card NOT detected" result, even when the card is present.

I have the RC522 connected via hardware SPI (and I have changed the variables in the .ino file). I am using spark-cli to monitor the serial output.

Any advice?

Also, I’m still new to SPI – could you point me anywhere that could describe the difference between hardware SPI and software SPI and why I would use one over the other? (this may be a completely silly question :blush: )

Did anyone ever figure out how to write to the RFID cards, or is this read only?

@korneel , did you ever post the code you used to get this working with an LCD? I may have just missed it, but I’d like to see it :smile:


@austinpe, @korneel had the same problem and got it working by carefully reviewing his wiring.

Like a hardware serial port, “hardware SPI” uses hardware inside the STM32 processor to do all the work of sending and receiving SPI data without requiring processing time. Hardware SPI uses dedicated pins on the Spark. Software SPI on the other hand, uses code to do the same thing using “bit banging” to control the SPI lines. It is inherently slower then hardware SPI. The only advantage of software SPI is that any I/O pins can be used. :smile:


@peekay123 , hmmm. I have double checked my wiring, made sure that the USE_SOFT_SPI was commented out, triple checked my wiring… reloaded your code, tried a different RST pin besides D2, did a continuity test on my wires …

then I tried it in software SPI (reloading your code and uncommenting out USE_SOFT_SPI ) …

Still, all I get is “no card detected” :frowning:

Maybe I have a faulty RC522?

Any thoughts?


@austinpe, in the example code, under setup() there is this line:


You can try changing the SPI clock speed to DIV16 or DIV32 and trying each one to see if it works. Otherwise, I have found a more advanced Arduino library I could port :smile: