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: wikipedia.com 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.
Library
Features
- Read RFID card at 13.56MHz
- Working with SPI
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx GPIO
- STM32F4xx SPI
- TM
- TM SPI
- defines.h
- TM SPI
Pinout
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.
MFRC522 | STM32F4xx | DESCRIPTION |
---|---|---|
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
1 2 3 4 5 6 7 8 9 10 11 12 |
//Default SPI used #ifndef MFRC522_SPI #define MFRC522_SPI SPI1 #define MFRC522_SPI_PINSPACK TM_SPI_PinsPack_2 #endif //Default CS pin used #ifndef MFRC522_CS_PIN #define MFRC522_CS_RCC RCC_AHB1Periph_GPIOG #define MFRC522_CS_PORT GPIOG #define MFRC522_CS_PIN GPIO_Pin_2 #endif |
Initialize RC522
Initialize chip with function below
1 2 3 4 5 6 7 8 9 10 |
/** * Public functions */ /** * Initialize MFRC522 RFID reader * * Prepare MFRC522 to work with RFIDs * */ extern void TM_MFRC522_Init(void); |
Check RFID Card
To check if card was put on receiver, you can check this with function below
1 2 3 4 5 6 7 8 9 10 11 |
/** * Check for RFID card existance * * Parameters: * - uint8_t* id: * Pointer to 5bytes long memory to store valid card id in. * ID is valid only if card is detected, so when function returns MI_OK * * Returns MI_OK if card is detected */ extern TM_MFRC522_Status_t TM_MFRC522_Check(uint8_t* id); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Compare 2 RFID ID's * Useful if you have known ID (database with allowed IDs), to compare detected card with with your ID * * Parameters: * - uint8_t* CardID: * Pointer to 5bytes detected card ID * - uint8_t* CompareID: * Pointer to 5bytes your ID * * Returns MI_OK if IDs are the same, or MI_ERR if not */ extern TM_MFRC522_Status_t TM_MFRC522_Compare(uint8_t* CardID, uint8_t* CompareID); |
Example
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
/** * Keil project for MFRC522 RFID Reader * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 */ #include "defines.h" #include "stm32f4xx.h" #include "tm_stm32f4_ili9341.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_mfrc522.h" #include "tm_stm32f4_disco.h" #include <stdio.h> int main(void) { //Recognized card ID uint8_t CardID[5]; //My cards id //I read them with program below, and write this here uint8_t MyID[5] = { 0x43, 0xdc, 0x52, 0xb6, 0x7b //My card on my keys }; char buffer[50]; //Initialize system SystemInit(); //Initialize delay TM_DELAY_Init(); //Initialize leds TM_DISCO_LedInit(); //Initialize LCD TM_ILI9341_Init(); TM_ILI9341_Rotate(TM_ILI9341_Orientation_Portrait_2); //Initialize MFRC522 RFID TM_MFRC522_Init(); //Watermark ;) TM_ILI9341_Puts(90, 310, "stm32f4-discovery.net", &TM_Font_7x10, ILI9341_COLOR_BLUE, ILI9341_COLOR_WHITE); while (1) { //If any card detected if (TM_MFRC522_Check(CardID) == MI_OK) { //CardID is valid //Check if this is my card if (TM_MFRC522_Compare(CardID, MyID) == MI_OK) { TM_ILI9341_Puts(10, 150, "Hello tilz0R!", &TM_Font_11x18, ILI9341_COLOR_GREEN, ILI9341_COLOR_BLACK); } else { TM_ILI9341_Puts(10, 150, "Good bye man!", &TM_Font_11x18, ILI9341_COLOR_RED, ILI9341_COLOR_BLACK); } //Print on LCD TM_ILI9341_Puts(10, 10, "Card detected ", &TM_Font_11x18, 0x00, 0xFFFF); sprintf(buffer, "0x%02x\n0x%02x\n0x%02x\n0x%02x\n0x%02x", CardID[0], CardID[1], CardID[2], CardID[3], CardID[4]); TM_ILI9341_Puts(10, 30, buffer, &TM_Font_11x18, 0x00, 0xFFFF); } else { //Some printing to delete content TM_ILI9341_Puts(10, 10, "Card not detected", &TM_Font_11x18, 0x00, 0xFFFF); TM_ILI9341_Puts(10, 150, " ", &TM_Font_11x18, ILI9341_COLOR_GREEN, ILI9341_COLOR_WHITE); TM_ILI9341_Puts(10, 30, " \n \n \n \n ", &TM_Font_11x18, 0x00, 0xFFFF); } } } |
View project on Github, download library below.
Read RFID Cards with Mifare MF RC522
Recent comments