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

How to Interface Relay with PIC16F877A PIC Evaluation Board

Spread the love

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.

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

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

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
//Define PIC Registers
#include
//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 Serial_init();
DelayMs(20);
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(1) { 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 switch(ReceivChar)
{
case '1': //If '1' was received Buzzer turned ON Relay1=1;
printf("Relay1 Turned ON \n\r");
//Display status break; case '2': //If '2' was received Buzzer turned OFF Relay1=0;
printf("Relay1 Turned OFF \n\r");
//Display status break; case '3': //If '3' was received Relay1 turned ON Relay2=1;
printf("Relay2 Turned ON \n\r");
//Display status break; case '4': //If '4' was received Relay1 turned OFF Relay2=0;
printf("Relay2 Turned OFF \n\r");
//Display status break; case '5': //If '5' was received Relay2 turned ON Relay3=1;
printf("Relay3 Turned ON \n\r");
//Display status break; case '6': //If '6' was received Relay1 turned OFF Relay3=0;
printf("Relay3 Turned OFF \n\r");
//Display status break;
}
}
}
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)
{
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 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

  • For proper working use the components of exact values as shown in Circuit file.
  • Solder everything in a clean way. A major problem arises due to improper soldering, solder jumps and loose joints.
  • Use the exact value crystal shown in schematic.

Leave a Reply

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