How to Interface Relay with PIC16F877A

Relay Interfacing With PIC16F877A Slicker

Tags: Interfacing Relay with PIC16F877A, Circuit Diagram to Interface Relay with PIC16F877A,C Program to control Relay in PIC16F877A, control 230 v using PIC16F877A, RELAY Control using PIC16F877A,
Overall rating
PIC16F/18F Slicker Board

The PIC16F/18F Slicker board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports in system programming (ISP) which is done through USB port.

Microchip’s PIC (PIC16F877A), PIC16F/18F Slicker Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 8-bit Microcontrollers.


Relays are devices which allow low power circuits to switch a relatively high Current/Voltage ON/OFF. A relay circuit is typically a smaller switch or device which drives (opens/closes) an electric switch that is capable of carrying much larger current amounts.

Interfacing Relays

Fig. 1 shows how to interface the Relay to microcontroller. There are 2 input channels. Each input is connected to the triggering coil of the respective relay. There are 2 output channels that each correspond to an input. When the input is energized, the relay turns on and the '+' output is connected to +12v. When the relay is off, the '+' output is connected to Ground. The '-' output is permanently wired to Ground.


Fig. 1 Interfacing Relay to Microcontroller

Interfacing Relay with PIC16F877A

We now want to control the relay operations by using PIC16F/18F Slicker Board. Here we are using two Relays. The relay consists of a coil and a switch. When the coil is energized, the switch closes, connecting the two contacts together. ULN2803 is used as a driver for port I/O lines, drivers output connected to relay modules. Connector provided for external power supply if needed.

Relay Module: Port B pins (Relay1 – PORTB.0) and Relay2-PORTB.1) for relay module, make port pins to high, relay will activated.

Pin Assignment with PIC16F877A





* Connect PORTB with JP5 (E-MECH) via FRC Cable


* Turn ON TXD and RXD Pins of CONFIG switch SW1. Connect Serial   

    cable between USART Section in the Board and PC and open HyperTerminal

* Press RESET Once.


Output: Choose Any one from 1 to 6. The Corresponding Action Will Be carried out.


Circuit Diagram to Interface Relay with PIC16F877A


Source Code

The Interfacing Relay with PIC16F877A program is very simple and straight forward, which control the relays in PIC16F/18F Slicker Board. The relay is working that uses a delay procedure loop based software delay. The C programs are developed in Mplab software with Hi-Tech Compiler.

C Program to control Relay in PIC16F877A


Title : Program to control Relay




#include<pic.h>      //Define PIC Registers

#include<stdio.h>    //Define I/O Function

#define Relay1 RB0   //RB1 interfaced to Relay1

#define Relay2 RB1   //RB2 interfaced to Relay2

__CONFIG(0x3f72);//Select HS oscillator, Enable (PWRTE,BOREN),

                 //Disable (CPD,CP,WDTEN,In-circuit Debugger)

#define FOSC       10000      //10Mhz==>10000Khz

#define BAUD_RATE   9.6       //9600 Baudrate

#define BAUD_VAL   ((char)(FOSC/ (16 * BAUD_RATE )) – 1)     

//Calculation For 9600 Baudrate @10Mhz


void Serial_init(void);  //Serial Communication Initialization

void DelayMs(unsigned int);


void main()


   unsigned char ReceivChar;

   TRISB = 0x00;  

   PORTB = 0x04;        //PORTB configured as O/P




   printf("\n\r\t3: Relay1 ON  \n\r\t4: Relay1 OFF");

   printf("\n\r\t5: Relay2 ON  \n\r\t6: Relay2 OFF\n\r\n");



      while(RCIF==0);    //Wait until the reception is over

      RCIF=0;            //Clear the flag for next reception

      ReceivChar=RCREG; //Store the received char to a variable

      printf(" %c",ReceivChar);//Dislpay the character received




         case '3':      //If '3' is received Relay1 turned ON



         case '4':      //If '4' is received Relay1 turned OFF



         case '5':      //If '5' is received Relay2 turned ON



         case '6':      //If '6' is received Relay1 turned OFF






void Serial_init()


   TRISC=0xc0;          //RC7,RC6 set to usart mode(INPUT)  

   TXSTA=0x24; //Enable(Serial TXn,//Asynchronous, High Speed)

   SPBRG=BAUD_VAL;      //9600 baud at 10Mhz

   RCSTA=0x90; //Usart Enable, Continuous receive enable

   TXIF=1;              //Start Transmit


void putch(unsigned char data)



   TXREG=data;         //Transmit the data serially


void DelayMs(unsigned int Ms)


   int delay_cnst;




      for(delay_cnst = 0;delay_cnst <220;delay_cnst++);






To compile the above C code you need the Mplab software & Hi-Tech C Compiler. They must be properly set up and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.

In Mplab, you want to develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without PIC16F/18F Slicker Board.

The PICKIT2 software is used to download the hex file into your microcontroller IC PIC16F877A through USB port.

Testing the Relay with PIC16F877A

Give +12V power supply to PIC16F/18F Slicker Board; the Relay module is connected with PIC16F/18F Slicker Board. When the program is downloading into PIC16F877A in Slicker Board, the Relay output is working that the Relay is ON some time period and the Relay is OFF some other time of period.

If you are not getting any output from Relay, then you just check the jumper connections & check the Relay is connected properly. Otherwise you just check it with debugging mode in Mplab. If you want to see more details about debugging just see the videos in below link.

How to create and Debug a Project in Mplab using PIC16F using Hi-Tech compiler.

General Information

Join the World's Largest Technical Community

we respect your privacy.