Interfacing of led with PIC16F877A microcontroller


In this tutorial we are going to learn about following below things:-

  • What is LED?
  • Interfacing of LED with PIC16F877A microcontroller
  • Connection of LED with PIC16F877A using Proteus Design
  • Programming in C language
  • We will also learn basic of LED operations & how to generate logic for LED & with the help of microcontroller, we will learn how we can ON/OFF LED.


A LED is a p-n junction semiconductor device which emits light when biased in forward direction. i.e, the positive terminal is to be connected to the anode & negative terminal of supply is to be connected to cathode of Led.

Led have two terminals 1st are longer which tends to anode of Led and 2nd is shorter which tends to cathode terminal.

led terminals

As we already know that Led will work on forward biasing means +ve to anode & -ve to cathode of Led, similar way in digital electronics +5V means HIGH or Logic 1(one) & -Ve means LOW or Logic 0(zero).

So its means when Logic 1 or HIGH applied across to anode & LOW or Logic 0 to cathode then Led will Glow. So to glow the Led , toggle & control the Led according to customization of program. We are using one 8-bit PIC16F877A microcontroller of microchip technology PIC family controller.

LEDs have dropped a voltage of 1.3 V to 2.2 V and require 7 to 10 mA to glow Led at full intensity.

R=(Vcontroller – Vled)/I

If we want to allow 10mA current through the LED then resistor value will be as below:-

R = (Vcc-led voltage)/current.

R = (5-1.7)/10mA.

R= 330 Ohms

The following picture describes “how to glow a led”.

led circuit

Now it’s time for interfacing of Single Led with microcontroller. We are using 8-bit  PIC16F887A Microchip 40 pin microcontroller, it has all 33 input/output port, CPU, and peripheral in a single chip. Reason for using a PIC microcontroller is that it uses low power, and is application specific and PIC16F877based on HIGH Performance RISC (Reduced Instruction Set Computer) machine. It has 14.3KB flash memory & 368 bytes SRAM with UART & SPI , I2C communication protocol support in a single chip IC.

PIC pin diagram

Circuit Design

Proteus Design for interfacing of led with PIC16F877A microcontroller.

For connection of Led with PIC16F877A , we take PORTB as output direction and connect the RB0 pin to +ve terminal of Led and – ve terminal of Led connected Ground as you can see from below figure.

Pic Led Design

C Code

C programming for interfacing of Led with PIC16F877A microcontroller using MicroC compiler.


This program is developed by EMBEDDINATOR DEVELOPERS.

void delay(int a)


int b,c;

for(b=0;b<a;b++)< p=””>




void main() {




PORTB=0b00000001;        //on the led (led get 1)

delay(100);                //delay of 100 msec.

PORTB=0b00000000;        //off the led (led get 0)



End program

Thank you for visiting our website