How to Interface Relay with PIC16F877A

Relay Interfacing with PIC16F877A Friendly

Tags: Relay interface with pic16f877a,relay c source code for pic16f877a, interfacing relay using PIC16F877A, circuit diagram for relay interfacing,,
Overall rating
PIC16F/18F Evaluation Board

The PIC16F/18F Evaluation 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 Evaluation 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 Evaluation 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 – PORTC.0), (Relay2 – PORTC.1) and Relay2 - PORTC.2) for relay module, make port pins to high, relay will activated.

Pin Assignment with PIC16F877A





* Connect PORTC 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 Evaluation 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 RC0          //RC0 interfaced to Relay1

#define Relay2 RC1          //RC1 interfaced to Relay2

#define Relay3 RC2          //RC2 interfaced to Relay3

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

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

void Serial_init(void);  //Serial Communication Initialization

void DelayMs(unsigned int Ms);


void main()


   unsigned char ReceivChar;

   TRISC = 0x00;  

   PORTC = 0x00;            //PORTB configured as O/P



   printf("Press   1: Relay1 ON  \n\r\t2: Relay1 OFF");

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

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




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

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

      ReceivChar=RCREG;  //Store the rcv character to a var




         case '1':       //If '1' was received Buzzer turned ON


         printf("Relay1 Turned ON   \n\r");  //Display status


         case '2':     //If '2' was received Buzzer turned OFF


         printf("Relay1 Turned OFF  \n\r");  //Display status


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


         printf("Relay2 Turned ON   \n\r");  //Display status


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


         printf("Relay2 Turned OFF  \n\r");  //Display status


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


         printf("Relay3 Turned ON   \n\r");  //Display status


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


         printf("Relay3 Turned OFF  \n\r");    //Display status






void Serial_init()


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

   TXSTA=0x24; //(Serial TXion, Asynchronous , High Speed mode)

   SPBRG=64;            //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 Evaluation 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 Evaluation Board; the Relay module is connected with PIC16F/18F Evaluation Board. When the program is downloading into PIC16F877A in Evaluation 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.

General Information

Join the World's Largest Technical Community

we respect your privacy.