You are on page 1of 13

PIC HOW-TO GUIDE

Interfacing Relay with PIC16F877A

Contents at a Glance
PIC16F/18F Evaluation Board ...........................................3 Relay ...............................................................................3 Interfacing Relays ............................................................4 Interfacing Relay with PIC16F877A ...................................5 Pin Assignment with PIC16F877A .....................................5 Circuit Diagram to Interface Relay with PIC16F877A .........6 Source Code ....................................................................6 C Program to control Relay in PIC16F877A .......................7 Testing the Relay with PIC16F877A ................................ 10 General Information ...................................................... 11

Join the Technical Community Today! http://www.pantechsolutions.net

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. Microchips 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. Join the Technical Community Today! http://www.pantechsolutions.net

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 Buzzer to Microcontroller Join the Technical Community Today! http://www.pantechsolutions.net

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
PIC16F PORTC.0 PORTC.1 PORTC.2 Connections * 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.

Join the Technical Community Today! http://www.pantechsolutions.net

Circuit Diagram to Interface Relay with PIC16F877A


LS1 5 D14 1N4007 Q3 BC547 R20 2 3 4 1 2 JP4 1 2 3 PBT-3PIN GOODSKY -6V-RELAY D15 1N4007 LS2 Q4 BC547 5 3 4 1 2

MCLR

1K 0.1uF C31

VDD

JP6 1 2 3 PBT-3PIN

11 32

U18

12 31

R23 2

MCLR/Vpp

VDD VDD

VSS VSS

1K GOODSKY -6V-RELAY LS3 JP5 5 D17 1N4007 Q5 BC547 R21 3 4 1 2 1 2 3 PBT-3PIN GOODSKY -6V-RELAY

PIC16F877A RC0/T1OSO/T1CKI U14 RC1/T1OSI/CCP2 RC2/CCP1 13 Y7 10 Mhz 14 OSC1/CLKIN

15 16 17

C30

22pF

22pF C29

OSC2/CLKOUT 1K

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. Join the Technical Community Today! http://www.pantechsolutions.net

C Program to control Relay in PIC16F877A


*************************************************************************************** Title : Program to control Relay *************************************************************************************** #include<pic.h> #include<stdio.h> #define Relay1 RC0 #define Relay2 RC1 #define Relay3 RC2 //Define PIC Registers //Define I/O Function //RC0 interfaced to Relay1 //RC1 interfaced to Relay2 //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); RCIF=0; ReceivChar=RCREG; //Wait until the reception is over //Clear the flag for next reception //Store the rcv character to a var

Join the Technical Community Today! http://www.pantechsolutions.net

switch(ReceivChar) { case '1': //If '1' was received Buzzer turned ON Relay1=1; printf("Relay1 Turned ON break; case '2': Relay1=0; \n\r"); //Display status

//If '2' was received Buzzer turned OFF \n\r"); //Display status

printf("Relay1 Turned OFF break; case '3': Relay2=1;

//If '3' was received Relay1 turned ON \n\r"); //Display status

printf("Relay2 Turned ON break; case '4': Relay2=0;

//If '4' was received Relay1 turned OFF \n\r"); //Display status

printf("Relay2 Turned OFF break; case '5': Relay3=1;

//If '5' was received Relay2 turned ON \n\r"); //Display status

printf("Relay3 Turned ON break;

case '6': //If '6' was received Relay1 turned OFF Relay3=0; printf("Relay3 Turned OFF \n\r"); //Display status break; } } }

Join the Technical Community Today! http://www.pantechsolutions.net

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; } void DelayMs(unsigned int Ms) { int delay_cnst; while(Ms>0) { Ms--; for(delay_cnst = 0;delay_cnst <220;delay_cnst++); } } //Transmit the data serially

Join the Technical Community Today! http://www.pantechsolutions.net

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. Join the Technical Community Today! http://www.pantechsolutions.net

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 & Debug a Project in Mplab using PIC16F using Hi-Tech Compiler. 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. More instructions are available in following articles, User Manual of PIC16F/18F Evaluation Board. Create & Debug a project in Mplab using PIC16F877A.

Join the Technical Community Today! http://www.pantechsolutions.net

Did you enjoy the read?


Pantech solutions creates information packed technical documents like this one every month. And our website is a rich and trusted resource used by a vibrant online community of more than 1, 00,000 members from organization of all shapes and sizes.

Join the Technical Community Today! http://www.pantechsolutions.net

What do we sell?
Our products range from Various Microcontroller development boards, DSP Boards, FPGA/CPLD boards, Communication Kits, Power electronics, Basic electronics, Robotics, Sensors, Electronic components and much more . Our goal is to make finding the parts and information you need easier and affordable so you can create awesome projects and training from Basic to Cutting edge technology.

Join the Technical Community Today! http://www.pantechsolutions.net

You might also like