PIC16F877A – Relay Interfacing

This article is a continuation of the series of tutorials on the PIC16F877A Microcontroller. The aim of this series is to provide easy and practical examples that anyone can understand. In the previous tutorial, we have interfaced the DC motor with PIC16F877A. In this tutorial, we will learn Relay interfacing with PIC16F877A. Relay is a very important component to interface the heavy appliances with the help of a microcontroller.


Relays are devices that 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.


Current flowing through the coil of the relay creates a magnetic field that attracts a lever and changes the switch contacts. The coil current can be on or off so relays have two switch positions and most have double throw (changeover) switch contacts.

Construction and working

Relays are made up of an electromagnet and a set of contacts generally based on Single Pole Double Throw (SPDT) or Double Pole Double Throw (DPDT) switching method. It has 3 pins to perform a function –

  • COM = Common, always connect to NC; it is the moving part of the switch.
  • NC = Normally Closed, COM is connected to this when the relay coil is off.
  • NO = Normally Open, COM is connected to this when the relay coil is on.

You can easily understand the concept of Relay by looking the below image.


Before Interfacing, everyone should know about the Relay Driver which is used to interface the relay to the microcontroller.

Relay Interfacing with PIC16F877A

Circuit Diagram

In this tutorial, I’m connecting One Relay in Port B.0. And the switch is connected into Port B.1.


If you want to make Relay ON, Just give the High Signal (1) to that pin that is connected to ULN2803. The concept of this code is just to turn On the Relay when the button is pressed.


#define relay RB0
#define sw RB1

void main()
    while(1) {
        } else {


relay interfacing with pic16f877a [ Please find the output image Here ]

So Programming wise it is very simple. It is like a LED program. But Hardware wise we should be careful. Because we might connect AC power supply devices here. So Be careful guys. You may connect Fan, AC Motor, Lamp, etc to relay.

In our next tutorial, we will see how to interface the Keypad with PIC16F877A.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Notify of

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

Newest Most Voted
Inline Feedbacks
View all comments
Table of Contents