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.

Interfacing EM-18 RFID Module with PIC MicrocontrollerWorking

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.

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

Interfacing with PIC Microcontroller

EM-18 RFID Reader Module can be directly interfaced with 5V PIC Microcontrollers using UART module. For 3.3V deviced 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

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 ?

For more detail: Interfacing EM-18 RFID Module with PIC Microcontroller


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.