Library 23- Read RFID tag with MF RC522 on STM32F4

In this library I’m talking about RFID (Radio-Frequency IDentification).

Radio-frequency identification (RFID) is the wireless non-contact use of radio-frequency electromagnetic fields to transfer data, for the purposes of automatically identifying and tracking tags attached to objects. The tags contain electronically stored information. Some tags are powered by and read at short ranges (a few meters) via magnetic fields (electromagnetic induction). Others use a local power source such as a battery, or else have no battery but collect energy from the interrogating EM field, and then act as a passive transponder to emit microwaves or UHFradio waves (i.e., electromagnetic radiation at high frequencies). Battery powered tags may operate at hundreds of meters. Unlike a barcode, the tag does not necessarily need to be within line of sight of the reader, and may be embedded in the tracked object.

source: For that I used Mifare RC522 RFID reader from NXP. I got it on Ebay almost for free with 2 RFID cards included. This chip works with RFID on 13.56MHz. To get this to work, you also need RFID cards with same working frequency (included in packet from ebay). I got this working on STM32F429 Discovery board with SPI communication and display data on LCD on board. Detection distance was about 10cm from receiver.



  • Read RFID card at 13.56MHz
  • Working with SPI


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx SPI
  • TM
    • TM SPI
    • defines.h


My RFID uses SPI. This chip has also support for UART or I2C communication. And also, my RFID board has CS (chip select) pin marked as SDA (I2C Serial Data) so don’t worry if you don’t have CS pin on your board.

CS (SDA) PG2 Chip select for SPI
SCK PB3 Serial Clock for SPI
MISO PB4 Master In Slave Out for SPI
MOSI PB5 Master Out Slave In for SPI
GND GND Ground
VCC 3.3V 3.3V power
RST 3.3V Reset pin

You can also choose other pins. To change them open your defines.h file and edit what you need

Initialize RC522

Initialize chip with function below

Check RFID Card

To check if card was put on receiver, you can check this with function below

Compare RFID Card

I also made a compare function, which is reeealy simple function to compare 2 5 bytes long arrays if they are equal. Useful when you have to compare your ID number with card number to detect if you “have access” to eg. open door.


In this example I check for my RFID card (blue). RFID reads card id and if they are equal, it simply write data to LCD.

No RFID Card on receiver

No RFID Card on receiver

RFID Card detected and OK

RFID Card detected and OK

RFID Card detected OK but wrong card

RFID Card detected OK but wrong card

View project on Github, download library below.


Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...