Interfacing EM-18 RFID Module with PIC Microcontroller

EM-18 RFID Reader Module

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 ?



Buy Here

Share this post

  • pradeep

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

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

  • line

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

  • 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

  • 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..

  • satyaveni

    can you help me pic16f877a interfacing with FIM3030 code

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

  • 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??

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

  • 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…

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

  • jeffin

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

  • Thanks for the info. It is corrected now.

  • S CHAND BASHA

    use terminal v1.9b

  • 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.

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

  • 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

  • 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.

  • John Carlo

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

  • 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?

  • 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.

  • 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]))

  • 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.

  • sanjay

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

  • 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..

  • Lucky Singh

    Thanks a lot. I got the output

  • 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.

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

  • Mathew Ramesh

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

  • 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?

  • You need to write the program to PIC Microcontroller.

  • 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.

  • 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

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

  • 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

  • shake

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

  • 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.

  • It is not there in proteus.

  • Samra

    So we can made it or where we can find it

  • What ?

  • Aaryan

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

  • Some applications we need to interface with PIC.

  • Robelyn Viaje

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

  • Vineeth Vv

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

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

  • You mean LCD displaying “Error ” ??