As an Amazon Associate we earn from qualifying purchases.
RFID (radio-frequency identification) lets a microcontroller identify a tag or card without contact. This article shows how to read RFID cards in MikroC on a PIC microcontroller, print a message on a 16×2 LCD, and tell several known cards apart. The complete example code is included, followed by an explanation of how it works and how to improve it.
How RFID works in a simple project
An RFID system has two parts: a tag (the card or key fob) and a reader. The reader sends out a radio field, the tag answers with its unique ID, and the reader passes that ID to your microcontroller.
Many low-cost readers, such as the EM-18 RFID reader, do this over a serial (UART) connection. When a card comes near the antenna, the reader sends the card ID as ASCII characters. Your program only needs to read those characters from the UART and decide what to do with them.
The example below assumes this kind of reader, running at 9600 baud, with its TX pin connected to the RX pin of the microcontroller’s hardware UART.
What you need
- A PIC microcontroller with a hardware UART (for example, a PIC16F877A)
- A serial RFID reader and a few compatible cards or tags
- A 16×2 character LCD
- A stable 5 V supply, a crystal oscillator and a programmer
- MikroC PRO for PIC to compile the code
Hardware connections
The LCD in this example is wired to PORTD in 4-bit mode. The pin definitions at the top of the code show the mapping:
- RS to RD2
- EN to RD3
- D4 to RD4, D5 to RD5, D6 to RD6, D7 to RD7
Connect the reader’s TX output to the microcontroller’s RX pin, and join the grounds of the reader, LCD and microcontroller. Check your reader’s datasheet for its supply voltage and signal levels before powering it up.
Example code: reading RFID cards in MikroC
The program starts the UART at 9600 baud, shows a short start-up message on the LCD, and then keeps checking for incoming data. When a card is read, it compares part of the ID with three known cards and prints “card found 1”, “card found 2” or “card found 3” on the second LCD line.
char cardletter;
void lcdstart(void);
void card1(void);
void card2(void);
void card3(void);
void rfid(void);
// LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
void main(){
uart1_init(9600);
Delay_ms(100); // Wait for UART module to stabilize
lcdstart();
while(1)
{
rfid();
}
}
void rfid(void)
{
if (UART1_Data_Ready())
{
cardletter = UART1_Read();
card1();
card2();
card3();
}
}
void card1(void)//card no.1000699A58BB(PRINTED NO.10539512)
{
if(cardletter=='8')
{
while(!(UART1_Data_Ready()));
cardletter = UART1_Read();
if(cardletter=='B')
{Lcd_Out(2,1,"card found 1");}
}
}
void card2(void)//card no.180089205DEC (PRINTED NO.13708285)
{
if(cardletter=='D')
{
while(!(UART1_Data_Ready()));
cardletter = UART1_Read();
if(cardletter=='E')
{Lcd_Out(2,1,"card found 2");}
}
}
void card3(void)//card no.1800893719BF (PRINTED NO.13714105)
{
if(cardletter=='9')
{
while(!(UART1_Data_Ready()));
cardletter = UART1_Read();
if(cardletter=='B')
{Lcd_Out(2,1,"card found 3");}
}
}
void lcdstart(void)
{
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,6,"RFID reader");
delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
}
Note: the start-up text in the original listing read “Alchol detector”, which looks like a leftover from another project. It now says “RFID reader”.
How the code works
UART and LCD setup
uart1_init(9600) starts the hardware UART at the reader’s baud rate. A short delay gives the module time to settle. lcdstart() initializes the LCD, turns the cursor off, shows the start-up text for one second and clears the screen.
Reading characters
In the main loop, rfid() calls UART1_Data_Ready(). When it returns true, UART1_Read() fetches one character into cardletter, and the three card functions check it.
Matching a card
Each card function looks for a specific pair of characters. For the first card (ID 1000699A58BB), the code waits for an 8 followed by a B. Those are the 10th and 11th characters of the ID. The other two cards are matched the same way, using D then E, and 9 then B. The comments in the code also give the number printed on each physical card.
Limits of this approach
The example is short and easy to follow, but it has weaknesses you should know about:
- It matches only two characters, so a different card that contains the same pair could be accepted by mistake.
- Matching can also trigger on characters in the middle of an ID rather than at the start.
- The
while(!(UART1_Data_Ready()))loops block the program until the next character arrives. - The LCD message is never cleared, so “card found” stays on screen after the card is removed.
A more reliable way: compare the whole ID
A better method is to read the full ID into a buffer and compare it with a stored string. Readers like the EM-18 commonly send 12 ASCII characters (the card number plus a checksum), which matches the 12-character IDs in the comments above. Check your reader’s datasheet for its exact output format.
char buf[13];
void read_card(void)
{
char i;
for (i = 0; i < 12; i++)
{
while (!UART1_Data_Ready());
buf[i] = UART1_Read();
}
buf[12] = 0; // end of string
}
void check_card(void)
{
if (strncmp(buf, "1000699A58BB", 12) == 0)
Lcd_Out(2, 1, "card found 1");
else if (strncmp(buf, "180089205DEC", 12) == 0)
Lcd_Out(2, 1, "card found 2");
else if (strncmp(buf, "1800893719BF", 12) == 0)
Lcd_Out(2, 1, "card found 3");
else
Lcd_Out(2, 1, "unknown card ");
}
Call read_card() and then check_card() when the first byte arrives. Replace the three example IDs with the ones printed in your own serial monitor or LCD output. If your reader also sends end-of-line bytes, read or discard them before the next card is scanned.
Troubleshooting
- Nothing on the LCD: check the contrast pot, the pin mapping and that the LCD pins match the
sbitdefinitions. - Garbage characters: confirm the baud rate and the oscillator frequency set in MikroC’s project settings.
- No card detected: check that the reader’s TX goes to the microcontroller’s RX, that the grounds are shared and that the supply is stable.
- Wrong card names: read your own cards first and write down their IDs, because each card has a different one.
Where to go next
Once card reading works, you can use the same pattern in access control, attendance systems or payment demos. If you work on other platforms, see our guides on RFID interfacing with 8051 and RFID with Arduino. For another MikroC sensor example that uses the same LCD routines, read about ultrasonic sensor reading in MikroC. A larger practical build is electronic toll collection using RFID.
FAQ
Which UART function reads RFID data in MikroC?
Use UART1_Data_Ready() to check whether a byte has arrived and UART1_Read() to take it from the receive buffer. Start the UART first with UART1_Init() at the reader’s baud rate.
Why does the printed number differ from the ID the reader sends?
The number printed on a card is often a decimal version of part of the ID, while the reader sends hexadecimal characters. In the example, card 1 is printed as 10539512 but is read as 1000699A58BB. Always use the ID your reader actually outputs.
Can I use this code with other PIC chips?
Yes, but check the UART pins, the LCD port and the TRIS register names for your chip. The MikroC library calls stay the same on most PICs with a hardware UART.
How can I store more than three cards?
Keep the known IDs in an array of strings and loop through it with strncmp(). For a larger list, you could store IDs in EEPROM so you can add cards without reprogramming the chip.