Interfacing EM-18 RFID Module with PIC Microcontroller

Interfacing EM-18 RFID Module with PIC Microcontroller

EM-18 RFID Reader Module is the one the most commonly used module for Radio Frequency Identification Projects. It features Low Cost, Small Size, Low Power Consumption and Easy to use. It can be directly interfaced with microcontrollers using UART communication. Software UART can be used for microcontrollers having no UART modules. In this tutorial we will see How to Interface EM-18 RFID Reader Module with PIC 16F877A Microcontroller. By understanding the basic idea, you will be able to interface it with any microcontrollers.

Working of RFID

The EM-18 RFID Reader module generates and radiates RF Carrier Signals of frequency 125KHz through its coils. When a 125KHz Passive RFID Tag (have no battery) is brought in to this field, will get energized from it. These RFID Tags are usually made using a CMOS IC EM4102. It gets enough power and master clock for its operations from the electromagnetic fields produced by RFID Reader.

RFID - System Principle

RFID – System Principle

By changing the modulation current through the coils, tag will send back the information contained in the factory programmed memory array.

Passive RFID Tag Block Diagram

Passive RFID Tag Block Diagram

Interfacing with PIC Microcontroller

EM-18 RFID Reader Module can be directly interfaced with 5V PIC Microcontrollers using UART module. For 3.3V devices you need to add additional voltage divider resistors to reduce 5V to 3.3V. You may also use Software UART if a dedicated UART module is not available.


When a RFID Tag is bring in to the field of EM-18 RFID Reader, it will read its tag number and give output via TX terminal. The BEEP terminal will become LOW to indicate valid tag detection. The UART output will be 12 ASCII data, among these first 10 will be tag number and last 2 will be XOR result of the tag number which can be used for error testing.

For eg : If the RFID tag number is 500097892E, output of EM-18 Reader will be 500097892E60 where 60 is 50 xor 00 xor 97 xor 89 xor 2E.

Circuit Diagram

Interfacing EM-18 RFID Reader Module with PIC Microcontroller

Interfacing EM-18 RFID Reader Module with PIC Microcontroller

0.1uF, 10uF, 100uF capacitors are for filtering power supply.  Note that TX of RFID Reader is connected to the RX of microcontroller. When a 125KHz RFID tag is bring in to the filed of Reader Module, BEEP terminal becomes low. This will turns on the Buzzer and LED. Meantime RFID tag data will be send to microcontroller via UART.

Programming

MikroC Pro Code

// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

void main()
{
 char i, rfid[13];
 Lcd_Init();                       // Initialize LCD
 Lcd_Cmd(_LCD_CLEAR);              // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF);         // Cursor off
 Lcd_Out(1,1,"RFID Tag Reader");   // Write text in first row
 
 UART1_Init(9600);                 // Initialize UART, 9600 baud rate    
 
 rfid[12] = '\0';                  // String Terminating Character

 while(1)                          // Infinite Loop
 {
   if(UART1_Data_Ready())          // If UART Data Ready
   {
     for(i=0;i<12;)                // To Read 12 characters 
     {
       if(UART1_Data_Ready())
       {
         rfid[i] = UART1_Read();
         i++;
       }
     }
     // Check For Errors
     if((rfid[0] ^ rfid[2] ^ rfid[4] ^ rfid[6] ^ rfid[8] == rfid[10]) && (rfid[1] ^ rfid[3] ^ rfid[5] ^ rfid[7] ^ rfid[9] == rfid[11]))
     {
       Lcd_Out(2,1,rfid);     
     }
     else
     {
       Lcd_Out(2,1,"Error ");
     }
   }
 }
}

You should be familiar with our MPLAB XC8 LCD Library and MPLAB XC8 UART Library before understanding following program.

MPLAB XC8 Code

#define _XTAL_FREQ 8000000

// LCD Module Connections
#define RS RB2
#define EN RB3
#define D4 RB4
#define D5 RB5
#define D6 RB6
#define D7 RB7
// END LCD Module Connections

#include <xc.h>
#include "lcd.h";
#include "uart.h";
#include <pic16f877a.h>

// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
//END CONFIG

void main()
{
  char i,rfid[13];

  TRISB = 0x00;                        // PORTB as Output, to connect LCD
 
  Lcd_Init();                          // Initialize LCD
  Lcd_Clear();                         // Clear display
  Lcd_Set_Cursor(1,1);
  Lcd_Write_String("RFID Tag Reader"); // Write text in first row

  UART_Init(9600);                     // Initialize UART, 9600 baud rate 

  rfid[12] = '\0';                     // String Terminating Character

  while(1)                      
 {
   if(UART_Data_Ready())
   {
     for(i=0;i<12;)
     {
       if(UART_Data_Ready())
       {
         rfid[i] = UART_Read();
         i++;
       }
     }
     // Checking for Errors
     if((rfid[0] ^ rfid[2] ^ rfid[4] ^ rfid[6] ^ rfid[8] == rfid[10]) && (rfid[1] ^ rfid[3] ^ rfid[5] ^ rfid[7] ^ rfid[9] == rfid[11]))
     {
       Lcd_Set_Cursor(2,1);
       Lcd_Write_String(rfid);
     }
     else
     {
       Lcd_Set_Cursor(1,1);
       Lcd_Write_String("Error ");
     }
   }
  }
}

Note : You should include header files “lcd.h” and “uart.h” to the project directory before building the project. For more details please read the following tutorials.

Hope you can understand the program as it is well commented. If you have any doubts, just comment below.

Download

You can download entire project files here.

Want to See the Output ?

Share this post

Comments (62)

  • pradeep

    thanks sir for this nice tutorial, i want to use why you use
    rfid[12] = ”; // String Terminating Character

    November 22, 2014 at 7:33 am
  • Ligo George

    Sorry, it is rfid[12]=” … .It was an HTML mistake… Now corrected..
    Thanks..

    November 22, 2014 at 10:43 am
  • line

    thanks buddy. did you enable pull up resistor?
    how can I separate first 10 number from last 2 XOR?

    November 22, 2014 at 1:31 pm
  • Ligo George

    What PULL UP resistor ??
    You may use the following method to separate first 10 bytes..
    …….
    ……..
    char tag[11];
    ………
    ……….
    tag[10] = ”;
    ………….
    …………..
    for(i=0;i<10;i++)
    tag[i] = rfid[i];

    Print tag

    November 23, 2014 at 4:29 pm
  • Ligo George

    Try once more… make sure that connection TX to RX is correct…
    I tried it with PL2303 USB to UART, it worked without any problem..

    December 5, 2014 at 1:50 pm
  • satyaveni

    can you help me pic16f877a interfacing with FIM3030 code

    January 20, 2015 at 10:54 pm
  • Ligo George

    Please use our forums (https://electrosome.com/forums) for asking doubts not related to above article.

    January 21, 2015 at 11:13 am
  • komal

    helo sir… thank u for this code it works really well…. i had one small, stupid doubt…. that if we use rfid reader v7.0 instead of em-18 reader…. will that make any difference??

    February 25, 2015 at 2:36 pm
  • Ligo George

    Sorry, I haven’t yet used rfid reader v7.0. Kindly check the datasheet of your reader.

    March 2, 2015 at 5:48 pm
  • Er Nitin Ahuja

    sir, after power on, only first time it reads the tag correctly ,when we again use the tag it shows incorrect tag no. it miss last two bytes in display…i have downloaded your code…pls check it
    thanks….always for your support….i have learnt many things from electrosome…

    April 4, 2015 at 6:02 pm
  • Ligo George

    Above programes are tested and verified in hardware.. It is working fine.

    April 5, 2015 at 1:13 pm
  • jeffin

    thnks sir for the great help
    this code missses a clossing bracket for main function

    April 13, 2015 at 12:21 am
  • Ligo George

    Thanks for the info. It is corrected now.

    April 13, 2015 at 10:40 am
  • S CHAND BASHA

    use terminal v1.9b

    May 21, 2015 at 11:04 am
  • Ahmed

    hello sir, Can u please tell me that how can we test the circuit by simulation in Proteus, Coz i couldn’t find EM-18 sensor in proteus.

    June 18, 2015 at 4:24 am
  • Ligo George

    There is no EM-18 in proteus, so you should try it in real hardware.

    June 18, 2015 at 8:44 am
  • John Carlo

    thanks sir for the tutorial, actually planning to make a rfid based password system, like i want to ask some questions sir
    1) what is the range of the reader? like 1 inch?
    2) can i also put a 4*3 matrix for manual password incase the tag is not available?
    3) also, which pin serves an output of “1” if the tag(rfid) or password(matrix) correct?
    4) can i also connect a servo ?
    5) lastly, are all this possible to connect with just 1 PIC or do i need more?
    thank you sir, please reply 🙂

    Great tutorial, the best there i could find

    November 22, 2015 at 10:12 am
  • Ligo George

    1. Range of EM-18 RFID reader is about 10cm
    2. Yes, you need to program it.
    3. You can program any of the IO pin as Output.
    4. Yes,
    6. Yes, only 1 PIC is required.

    November 22, 2015 at 5:36 pm
  • John Carlo

    thank you very much for the quick reply sir, now i can (probably) go through with my project!

    November 22, 2015 at 8:47 pm
  • Lucky Singh

    Sir I am new to this, I want your help in clearing my doubt.I have bought EM-18 Rfid reader module(complete board).But my reader contains pin as RS232 in place of Tx as as shown here.Sir my doubt are,
    1)My RFID reader contain DB9 and DC connection for 9-15V,can it be worked using either one?

    2)Is RS232 pin is same as Tx(did they do that by mistake?)
    3)Pin available for me are GND,RS232,BEEP,WDATA0,WDATA1.So sir to do above project I need to use GND,RS232,BEEP and one power supply?
    My question may look stupid to you ,but I need help sir?

    November 28, 2015 at 11:59 am
  • Ligo George

    Answers :
    1. Your board has voltage regulator IC, so you can provide input from 9V to 15V.
    2. If you want to use RS232, you should use MAX232 IC. But your board has TTL output pins. So it is better to use it. Or you can even unplug the EM-18 module from the PCB
    3. Better you try to unplug the EM-18 (black square box) from the PCB, then you will understand all.

    December 19, 2015 at 3:24 pm
  • vignesh

    Hi ligo I have a small doubt why you have use this condition…..if((rfid[0] ^ rfid[2] ^ rfid[4] ^ rfid[6] ^ rfid[8] == rfid[10]) && (rfid[1] ^ rfid[3] ^ rfid[5] ^ rfid[7] ^ rfid[9] == rfid[11]))

    December 21, 2015 at 6:13 pm
  • Ligo George

    Don’t you read above article ? It is clearly mentioned there.

    Last two bytes are checksum for error detection. So it just checks whether the data is correct or not.

    December 30, 2015 at 8:35 am
  • sanjay

    sir can you provide project files to interface rfid with pic in proteus (using virtual terminal as rfid)?

    December 30, 2015 at 11:32 pm
  • Jobin Jacob

    dear sir,
    is it possible to interface along with these components( i.e. PIC 16f877A, rfid module and lcd display), a tts component…and can we substitute the entire rf tag number as a character like A or something and then display it as some string characters….
    expecting an early reply..

    January 11, 2016 at 10:30 am
  • Lucky Singh

    Thanks a lot. I got the output

    January 18, 2016 at 9:12 pm
  • Ligo George

    What do you meant by tts component ?
    Most of the tags available in the market are read only. There are writable tags. You can write your own code in those using a RFID writer.

    January 24, 2016 at 12:45 pm
  • Ligo George

    You can use the project files above. Use the same program. It will work in proteus.

    February 2, 2016 at 5:11 pm
  • Mathew Ramesh

    I am new to using RFID .And this program upload into what?

    February 17, 2016 at 3:47 pm
  • Mathew Ramesh

    I am going to do a Automatic Ration shop using a RFID card and GSM.Can i add a customer details into this card?

    February 17, 2016 at 3:52 pm
  • Ligo George

    You need to write the program to PIC Microcontroller.

    February 27, 2016 at 7:53 pm
  • Ligo George

    Why should you need to add customer details to the card ? There will be unique identification number for each cards. You can save those details to database corresponding to the unique id of the card.

    February 27, 2016 at 7:54 pm
  • ROLLS

    hello Sir, your article above is very helpful. but am having issues with the code. when i tried out the code on my hardware. it displays weird first character on the lcd. i’ve tried my best to detect what could be the cause but still cannot fix it. please help. Thanks

    March 4, 2016 at 1:55 pm
  • Ligo George

    Above program is 100% working. It is for EM-18 RFID reader module. It might not work for other readers.

    March 19, 2016 at 10:49 am
  • petermuniko rindwi

    Thanks sir! for a very powerful article, I need you are help am designing rfid car parking system and payment management,simply once the car enter in a parking lot, rfid reader detect the valid tag from the car and send the signal to trigger microcontroller pic 16f877a. to start count timespent in the parking lot, and it calculate cost according to time spent on lot and send the information to database (tag id, time, and costAnd the system will have a gsm module to send txt to client about cost and time..If the tag is not valid the system will alert by using buzzer all the user will be registered. ……
    I need ur help on how to design a circuit and source code using proteus and micro C

    March 28, 2016 at 10:31 pm
  • shake

    sir..how to find em18 rfid reader module in proteus??

    March 29, 2016 at 8:30 pm
  • Ligo George

    Hello,
    I am sorry, We cannot provide this kinds of project development freely. As you know it is impossible for us to develop project for each and everyone free of cost. If you need it, please hire one of our engineers and pay for it. It will help us to live.

    April 15, 2016 at 7:49 am
  • Ligo George

    It is not there in proteus.

    April 15, 2016 at 8:02 am
  • Samra

    So we can made it or where we can find it

    May 23, 2016 at 8:55 am
  • Ligo George

    What ?

    June 2, 2016 at 6:41 pm
  • Aaryan

    Plzzzz hep…
    Why we are interacing reader to pic??….if we can directly connect reader to pc…..need of pic

    July 30, 2016 at 7:55 am
  • Ligo George

    Some applications we need to interface with PIC.

    August 27, 2016 at 1:57 pm
  • Robelyn Viaje

    May I know what simulation tool did you use in creating the circuit diagram?

    October 5, 2016 at 12:24 pm
  • Vineeth Vv

    Hi Ligo i tried the same code and every time it gives ERROR displayed on LCD

    October 10, 2016 at 2:41 pm
  • Ligo George

    I didn’t simulated above project, only tested in hardware.

    November 27, 2016 at 1:04 pm
  • Ligo George

    You mean LCD displaying “Error ” ??

    November 27, 2016 at 1:10 pm
  • Master

    Hi Ligo,

    Can you just tell me when this tag will give error? because for me, its detecting all RFID tag…

    December 6, 2016 at 11:14 am
  • Ligo George

    Just display it and see what data your tag is giving.

    December 7, 2016 at 11:37 am
  • Master

    As per your code… Once Tag is detected, All time, its displaying RFID, that’s all.
    So… how can we understand whether tag is correct or not..

    Thanks in advance!!

    December 7, 2016 at 4:36 pm
  • Master

    Hello Ligo Sir,

    I done the same thing as above BUT now its take number but only showing ‘6’ digit for all card. So lease tell me solution for that

    December 8, 2016 at 12:12 pm
  • Ligo George

    You need to write codes for it.

    December 9, 2016 at 9:32 am
  • Ligo George

    It won’t show same data for all cards. It will disply the code of that particular card only. I don’t know whether you are using 125KHz card or no. EM-18 works only with 125KHz cards.

    December 9, 2016 at 9:33 am
  • Master

    Hello Ligo..

    Need your help for the GSM SIM900 Module with PIC Controller.
    Can you Give me any suggestion ?

    December 15, 2016 at 6:35 pm
  • Ligo George

    Please use our forums ( https://electrosome.com/forums/ ) for asking doubts outside the scope of above article.

    December 17, 2016 at 2:21 pm
  • Shreya Ravi

    Hello Sir! Thanks for the tutorial. I am having PIC18F4550 instead of PIC16F. I have done the required changes in the code but I am getting values like “/+=/+=/+=/+=” in the LCD. The garbage value displayed is the same every time for a card. Can you tell where I am going wrong?

    January 25, 2017 at 9:55 am
  • Francis Sunny

    can we connect 2 rfid readers (em 18 ) in the same pic???

    March 24, 2017 at 7:45 am
  • Francis Sunny

    im using pic 16f877a

    March 24, 2017 at 7:45 am
  • Sergej Tot

    Hello sir, I have to make a NFC reader, which can recognize my android phone from others. I should use PN532 and PIC16f. Is it the same principle and similiar as you did in this project? I am a student and that is my school project. Thank you very much in advance. Sergey

    April 29, 2017 at 9:42 pm
  • Anjali Jayashree Menon

    I am cuurently working on a gate access system using RFID module and Tag and PIC 16f77. I just need the detection of the card as an output using which i will open up the Gate. Would I need a UART program for that? If not how else can i retrieve the output. Would be grateful if you help me with this .

    November 29, 2017 at 9:04 am
  • Ali Hamed Mohamed

    sorry, but i want to know if you have finished your pic coding and you can send it to me please?

    December 26, 2017 at 4:42 am
  • Ligo George

    I have developed a similar project before few years. It is connected with a computer software. If you like I can share the details. Drop me a mail [email protected]

    December 28, 2017 at 10:01 am
  • Ligo George

    NFC is different from RFID

    December 28, 2017 at 10:02 am

Leave a Reply

Your email address will not be published. Required fields are marked *