Categories: Firmware Guide - PIC16F877A

LED Interfacing with PIC16F877A

The light-emitting diode is a semiconductor device capable of emitting light when the current flow occurs through it. Led is a common component In many devices and it finds various applications in the day to day life. The factors such as reduced power consumption, small size, switching speed, Longer lifespan and durability make them a better choice over incandescent light sources. The led finds applications from simple indicators to advanced communication systems. This tutorial will explain the method of interfacing LEDs with PIC16F877A microcontroller and to develop a suitable firmware. LED blinking is the most basic project with a microcontroller to see a physical output. One can understand the concept of input-output configurations of the general-purpose I/O port in a microcontroller with a simple LED blinking project.

Interfacing LED with Microcontroller Pin

The PIC microcontroller PIC16F877A is one of the most famous microcontrollers in the industry. It has a total number of 40 pins and among which 33 pins are general-purpose input-output pins. PIC16f877A has 40 pins among which 33 are general-purpose input-output pins.The input/output pins are integrated as 5 ports, PORTA,PORTB,PORTC,PORTD,PORTE. These ports are used for input /output operations by the microcontroller. There will be a data direction register (TRIS) and a port register ( PORT) corresponding to each port in the microcontroller. TRIS register is used for configuring the port as input or output and the value in the port register will be the state of the pins of the corresponding port.

The above image is the simple way of interfacing a LED with a microcontroller. The anode is connected to the microcontroller and cathode connected to ground through a resistor. When the voltage at the microcontroller pin is high the LED will be turned ON and when the voltage is low the LED will be turned off.

Design Considerations

Forward voltage

It is the required voltage to turn on a diode.T he forward Voltage range  red/orange/yellow LED is 1.8 – 2.2V and green/blue/white LEDs it’s  3.4V

Peak forward current

It is the maximum forward current limit over which it will damage the led. Use the datasheet to find the limits for the led. The red led peak forward current limit is 100mA.

Continuous forward current

This is the optimum current required by the led for its operation and we can utilize this value for our design.

In the above circuit, a switch is used to control the led. The current passing through the led is limited by using a resistor. The forward voltage and Forward current is used to find the value of the resistor. we design a circuit with a 5v supply and a Red LED. We will limit the forward current to 20ma. The resistor value can be calculated as follows.

R = (5 – 2)/20mA = 150 ohms

we can change the above hardware control circuit to a software-controlled one by interfacing it to a microcontroller pin and a suitable firmware.

The current is now sourcing from the microcontroller and it will be difficult when the interfacing increases. One of the possible solutions is using a transistor in the switch configuration. The transistor can be controlled by using the controller pin and it can be designed to provide enough current to the circuit. The constant current drivers can be used to obtain optimum ratings in display applications. one of the commonly used IC is ULN2003.

LED Interfacing with PIC16F877A – Firmware Example

This tutorial intends to create an LED blinking project using a pic16F877A  microcontroller. In this section, we will create a firmware for interfacing led with pic16f877a. LEDs connected to the port pins can be made blink by alternatively loading zero and one to the ports. The port which is connected with the LEDs is configured as output by clearing the data direction register for the port.

It should be noted that the clock frequency should be defined for using the delay function(__delay_ms()). The xc.h header is used to access compiler and device-specific features. You can call the defined functions such as TRIS, PORT, and __delay_ms() after adding xc.h in your source file.

#define _XTAL_FREQ 20000000
#include <xc.h>

PORTC should be configured as output by configuring the TRISD(data direction register)

TRISC =0x00; // Configure PORTC as output

PORTC is loaded with 0xFF(11111111) and 0x00(00000000) with 1-sec delay in between. The whole code is enclosed in an infinite loop for continuous run

  while(1)
    {
        
        PORTC =0xFF;         //load 0xff to make all bits high.
        __delay_ms(1000);    // 1 sec delay
        PORTC=0x00;          //load 0x00 to make all bits low
        __delay_ms(1000);    // 1 sec delay
        
    }

Circuit for led Interfacing with pic16f877a


[fusion_global id=”18383″]

LED Chaser Program


[fusion_global id=”18383″]

 

 

Share