You are currently viewing How to Interface Relay with PIC16F877A PIC Advanced Development Board

How to Interface Relay with PIC16F877A PIC Advanced Development Board

Spread the love

The PIC16F877A Advanced Development 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 Advanced Development Board Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 8-bit Microcontrollers

Relay

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.

 Interfacing  Relay with PIC16F877A PIC Advanced Development Board

Fig. 1 Interfacing Relay to Microcontroller

Interfacing Relay with PIC16F877A

We now want to control the relay operations by using PIC16f/18FAdvanced Development 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

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 Advanced Development 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

#include
//Define PIC Registers
#include
//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 Serial_init();
printf("\033[2J");
DelayMs(20);
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(1)
{
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 switch(ReceivChar)
{
case '3': //If '3' is received Relay1 turned ON Relay1=1;
break;
case '4': //If '4' is received Relay1 turned OFF Relay1=0;
break; case '5': //If '5' is received Relay2 turned ON Relay2=1;
break; case '6': //If '6' is received Relay1 turned OFF Relay2=0;
break;
}
}
}
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)
{
while(TXIF==0);
TXREG=data;
//Transmit the data serially
}
void DelayMs(unsigned int Ms)
{
int delay_cnst; while(Ms>0)
{
Ms--;
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 Advanced Development 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 Advanced Development Board .The Relay module is connected with PIC16f/18F Advanced Development Board. When the program is downloading into PIC16F877A in Advanced Development 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.

Leave a Reply

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