## LED Interfacing with PIC18

### LED Interfacing with PIC18

#### Introduction of LED

A light-emitting diode (LED) is a two-lead semiconductor light source. It is a p–n junction diode that emits light when activated.[ When a suitable voltage is applied to the leads, electrons are able to recombine with electron holes within the device, releasing energy in the form of photons. This effect is called electroluminescence, and the color of the light (corresponding to the energy of the photon) is determined by the energy band gap of the semiconductor.

When operated in a forward biased direction Light Emitting Diodes are semiconductor devices that convert electrical energy into light energy.

### Light Emitting Diode Colours

So how does a light emitting diode get its color? Unlike normal signal diodes which are made for detection or power rectification, and which are made from either Germanium or Silicon semiconductor materials, Light Emitting Diodes are made from exotic semiconductor compounds

Different LED compounds emit light in specific regions of the visible light spectrum and therefore produce different intensity levels.

The LED is to be connected in a forward bias condition across a power supply it should be current limited using a series resistor to protect it from excessive current flow. Never connect an LED directly to a battery or power supply as it will be destroyed almost instantly because too much current will pass through and burn it out.

##### LED Series Resistor Circuit

The series resistor value RS is calculated by simply using Ohm´s Law, by knowing the required forward current IF of the LED, the supply voltage VS across the combination and the expected forward voltage drop of the LED, VF at the required current level, the current limiting resistor is calculated as:

An amber coloured LED with a forward volt drop of 2 volts is to be connected to a 5.0v stabilised DC power supply. Using the circuit above calculate the value of the series resistor required to limit the forward current to less than 10mA. Also calculate the current flowing through the diode if a 100&Omega; series resistor is used instead of the calculated first.

series resistor required at 10mA:

An LED can be interfaced with PIC18FXXX Microcontroller for various purpose. The Port Pins are configured in the output direction. So, once the Logic "1" is the at O/P pin, then an LED will be turned "ON". And when Logic "0" is given the LED will be turned "OFF".

Simply: Logic "1" -> 5V DC to output pin

Logic "0" -> 0 V to output pin.

So, as discussed in earlier, the LED with common cathode mode can be connected with PIC18FXX along with a resistor.

Thus, using the current limiting resistor, the LED will blink according to the written program.

#### Embedded C Program

#include<p18f4520.h>
void DELAY();
#pragma config OSC=HS
#pragma config PWRT=OFF
#pragma config WDT=OFF
#pragma config DEBUG=OFF, LVP=OFF
void main()
{
TRISDbits.RD0 = 0x00;          //set RD0 pin direction as an output
while(1)                      //forever loop
{
PORTDbits.RD0=0X01;   //set RD0 pin as HIGH
DELAY();              //call delay
PORTDbits.RD0=0X00;   //CLEAR RD0 piN
DELAY();              //call delay
}
}

void DELAY()
{
unsigned int i;
for(i=0;i<10000;i++);
}

Note- For Ring CounterYou Can interface 8 number of LED's to PORTB, or PORTC, or PORTD. Make Interfacing diagram accordingly

#### Embedded C Program for Ring Counter

#include<p18f4520.h>
#pragma config OSC=HS
#pragma config PWRT=OFF
#pragma config WDT=OFF
#pragma config DEBUG=OFF, LVP=OFF
void DELAY(unsigned int itime);
void main()
{
unsigned int data,I;
TRISD= 0x00;          //set RD0 pin direction as an output
PORTD=0X00;           //All 8 LED's OFF
while(1)                      //forever loop
{
data=0X01;   //set data variable with initial value
for(I=0;I<8;I++)  // for loop for 8 times
{
PORTD=data;   //send value present in data variable to LED's connected to PORTD
data=data<<1; //shift value present in data variable one bit to left side
DELAY(100);  // call some delay
}            //Increment I and if less than 8 then repeat
}

void DELAY()
{
unsigned int i;
for(i=0;i<10000;i++);
}

Note - User can use delay Routine using Timer

Note- For Ring CounterYou Can interface 8 number of LED's to PORTB, or PORTC, or PORTD. Make Interfacing diagram accordingly

#### BCD Counter

Note :- It  will count from 0 to 9

#include<p18f4520.h>
#pragma config OSC=HS
#pragma config PWRT=OFF
#pragma config WDT=OFF
#pragma config DEBUG=OFF, LVP=OFF
void DELAY(unsigned int itime);
void main()
{
unsigned int data,I;
TRISD= 0x00;          //set PORTD pins direction as an output
PORTD=0X00;           //All 8 LED's OFF
while(1)                      //forever loop
{
PORTD=0x00;   //Display 0 value with LED's connected to PORTD
DELAY(100);  // call some delay
PORTD=0x01;   //Display 1 value with LED's connected to PORTD
DELAY(100);  // call some delay
PORTD=0x02;   //Display 2 value with LED's connected to PORTD
DELAY(100);  // call some delay
PORTD=0x03;   //Display 3 value with LED's connected to PORTD
DELAY(100);  // call some delay
PORTD=0x04;   //Display 4 value with LED's connected to PORTD
DELAY(100);  // call some delay
PORTD=0x05;   //Display 5 value with LED's connected to PORTD
DELAY(100);  // call some delay
PORTD=0x06;   //Display 6 value with LED's connected to PORTD
DELAY(100);  // call some delay
PORTD=0x07;   //Display 7 value with LED's connected to PORTD
DELAY(100);  // call some delay
PORTD=0x08;   //Display 8 value with LED's connected to PORTD
DELAY(100);  // call some delay
PORTD=0x09;   //Display 9 value with LED's connected to PORTD
DELAY(100);  // call some delay
}
}

void DELAY()
{
unsigned int i;
for(i=0;i<10000;i++);
}

#### References

• WikiNote Foundation
• Created and Edited by Prof. Sujit Wagh, SKNCOE, Pune