Hello friends , hope you all are fine and enjoying good health. In today’s tutorial, I am going to design an LED Blinking Project on PIC Microcontroller. In the previous chapters we have seen all the essentials required for doing programming on MPLAB software for PIC microcontroller. So today we are gonna do our first project on PIC.

Its a very simple project so the programming level is very basic. We will just add an LED on one pin of PIC18F452 and then we will program it in such a way that this LED will blink, we can also change the speed of blinking by adding a delay.

I will explain the whole code but if you got any problem may ask in comments.I will recommend you guys to do this project, I know a beginner can’t make this project work in first attempt but when you do it you do mistakes and from them you will learn a lot. So let’s start the project.Best of luck !!!

#### LED Blinking Project on PIC Microcontroller

##### Step 1
• First of all we need an electrical circuitry for the project. Here’s the diagram for this project :
•  The circuit on the left side of PIC is its basic circuit, we need this circuit in order to power up the PIC and to give it a frequency on which it operates. Like in this diagram, I have used crystal oscillator of 10MHz, which is its frequency of operating. You can operate it at different frequencies e.g. 4MHz ,10MHz , 16MHz, 20MHz etc.
• But keep this thing in mind that if you change the oscillator then you must change the capacitors as well e.g. for 16MHz the capacitors will be of 22pF.
• Vdd for PIC is 5V , if you have a 5V battery then its cool but mostly adapters are of 12V. So if you have 12V adapter then use 7805 which converts the 12V into 5V.
• Now come to the circuit on the right side of the PIC, it’s a simple LED which I have connected on pin # 19 of PIC which is PORTD.0 (first pin of PortD).
• On the other side of this LED is a resistor just for current control and then a GND (ground).
• Now when we make this pin high then LED will ON and when we make the pin low LED will OFF.
NOTE :
• Pin High means its at 5V and LOW means its at ground.
##### Step 2
• Now let’s come to the programming part.Open your MPLAB project which we have created yesterday in the last chapter.
• Now in its C file paste the below code.

// ======= Code by TheEngineeringProjects.com ===========

#pragma config OSC = HS, OSCS = OFF
#pragma config PWRT = OFF, BOR = ON, BORV= 42
#pragma config WDT = OFF
#pragma config DEBUG = OFF, LVP = OFF,STVR = OFF

#include <p18f452.h>

void main (void)
{
TRISD       = 0;
PORTD     = 0;

while(1)
{
PORTDbits.RB0  = 1;
PORTDbits.RB0  = 0;
}
}

• Now I am gonna explain the code, it’s a very simple code just five to six lines.
• The first four lines which are starting with the word #pragma are actually used for the settings of PIC, whenever you make a program for PIC paste these four lines as it is in the beginning of your code. These lines tell the PIC to OFF their few characters which mostly remains OFF. So no need to know about these lines just paste them as it is.
• void main (void) This is the main portion of program, the compiler always come straight to this part and leaves the rest it will execute only what is written under its braces.
• TRISD = 0 This code tell the compiler to use the pins as output. If we were using a sensor on any pin then we have to make it 1.
• while(1) This one runs continuously means pic will execute the code with its braces forever and it never stops.
• and in the next two lines we make the pin high and then low which is connected to the LED so now the LED will turn ON and then OFF continuously.
• You can also add a delay between these lines if you want to increase the duration of ON OFF.

I have tried my best to explain it in full detail but if still you got any problem or anything left then ask in comments and I will explain it to you..

Stay Blessed …. ALLAH HAFIZ :))

1. killer says:

awesome sir ji

2. hajra says:

sir how to add delay in between these two lines ?

3. kashif says:

Whats the function of TRISD = 0

4. panchi says:

loved this magic of LEDssss

5. @killer Thanks bro for appreciation …. We will try our best to provide the best thing ….

6. @hajra Dear, to add the delay make a new routine in your code and then call it like :

void delay(unsigned int itime )
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}

This is the delay routine it must be added at the end of code outside the main code and then to use it in my code i will call it as follows :

PORTDbits.RB0 = 1;
Delay(1000);
PORTDbits.RB0 = 0;

If you still feel any problem ask me.

7. @kashif I have explained it quite detail in this tutorial well i try again …. :))

TRIS is a special function register in PIC if you keep this register high it means your that PORT will be input and if you make it low your PORT will be OUTPUT.

@panchi Ya bro i loved it too , its actually a flash image and working really kool, I am thinking of adding it on the home page in future …. what you say ?? :))

8. joey says:

how to stop led blinking…for exp i wan it blink in 3 times only..hope u can help me