Microcontrollers are a really useful piece of technology. We can do really amazing stuff with them. But if you are a beginner in the field of Microcontroller programming, don’t worry. I’ll guide you through the very basics of microcontroller programming for a simple LED blinking project. So let’s see what we have for our beginner’s guide to blinking an LED with the PIC16F877A Microcontroller.
What are we trying to achieve here?
If you keep going with me till the end of this project, you will be blinking an LED using the PIC16F877A microcontroller.
Disclaimer
This is intended for educational purposes only. I am not responsible for any damages incurred to your equipment, circuit components, or other properties. This project is using a simple 9V battery as the power source. Working with higher voltages for this is not recommended. If you find any error in the following (which I have proof-read), please don’t hesitate to leave a comment below or to contact me over LinkedIn or Facebook. Enjoy!
Requirements
- A Computer
- MPLAX X IDE Software
- XC8 Compiler
- A Microcontroller
- PIC16F877A + Datasheet
- Circuit Components
- Breadboard
- LM7805 Voltage regulator + Datasheet
- An LED
- 20MHz Crystal Oscillator
- 2 x 22pF capacitors (C2 and C4 in the circuit diagram)
- 0.1uF Capacitor (C3 in the circuit diagram)
- 220nF capacitor (C1 in the circuit diagram)
- 330R (330 Ohm) Resistor (R1 in the circuit diagram)
- 47k (47 kiloOhm) Resistor (R2 in the circuit diagram)
- Enough Jumper wires
- PICkit 3 Programmer
- A Cup of tea to celebrate your achievement!
First things first
What is a microcontroller
Not getting too technical, a Microcontroller is just a small computer on an Integrated Chip (IC).
Microcontrollers have pins for data transfer. The microcontroller we use in this project (PIC16F877A) has 40 pins.
What is PICkit Programmer?
You want a microcontroller to do some task right? How does it know what to do? Obviously you program it using your computer. But how to write that program to the microcontroller? That’s where PICkit comes in. It’s called a Programmer. (No, not the people type).
The 3.5 here just means it’s version. Don’t worry about that. Of course, you can build your own programmer but for the sake of this tutorial, let’s just use what’s available.
The tech people just love to call the process of writing the code to the microcontroller as Downloading. I know it’s a bit weird, but let’s stick to the norm.
What is a Breadboard?
Normally in professional circuits, the components are soldered. But when we are using a breadboard, we can fit the components without soldering so that they are readily removable from the breadboard. Just like you can fit a pencil into a loaf of bread (although I don’t recommend doing so).
What is Crystal Oscillator?
Crystal Oscillator is what keeps the timing of our microcontroller. Here, 20MHz is the frequency of our oscillator.
These work under the principle of Piezoelectric Effect.
What is LM7805?
The LM7805 is a voltage regulator. The last 5 in its name gives us the output voltage from that regulator.
This voltage regulator outputs 5V regardless of input voltage but to a certain input range. This range is indicated in the datasheet of the LM7805 regulator.
Why do we need capacitors here?
Although the actual explanation would make you close this article right now, the simple explanation is they smoothens current, and also blocks DC current from passing through certain paths in the circuit. That’s all we need to know for right now.
resistors, our familiar friends
We use resistors of different ratings for controlling current flow. The resistor notation 330R just means 330 Ohms. 10k means just 10 kilo Ohms.
Jumper wires? what are those?
Jumper wires are just connecting wires we use in such electronics projects which use breadboards. We just have to plug and pull them into and out of the breadboard.
Mind you, there are several types of jumper wires and should be used according to your need.
- Male – Male Jumper wires
- Female – Female Jumper wires
- Male – Female Jumper wires
Getting the things needed
Circuit components

Here, the most expensive thing is the PICkit3 (PICkit4 is also available). That would be around $15. Others should cost less than another $10. So our total project would cost around $25.
Required softwares (FREE downloads)
- MPLAB X IDE – https://www.microchip.com/mplab/mplab-x-ide
- XC8 Compiler – https://www.microchip.com/mplab/compilers
Enough with the intro, Let’s Build our Circuit!
You have done puzzles when you were young right? Well, get yourself ready for another puzzle. Combining all the electronic components of any electronics project is no more complex than the puzzles that we solved as kids. That’s how I’d like to think of it when it feels overwhelming.
Just follow the circuit diagram given below, and try to fit the stuff into the breadboard. If you find it difficult, please stay tuned for the upcoming video tutorial.
Here’s a .pdf on how to connect the wires of the PICkit if you need it. Don’t worry it’s for PICkit4, but will work just fine for our simple project. http://ww1.microchip.com/downloads/en/devicedoc/50002721a.pdf



Here’s the C code
This is the full C code that we need for our simple LED blinking project.
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF #include <xc.h> #define _XTAL_FREQ 20000000 void main(void) { TRISB=0; while(1) { PORTBbits.RB0=1; __delay_ms(1000); PORTBbits.RB0=0; __delay_ms(1000); } }
You may wonder is that all. Of course that’s all. Now let’s break the code line by line.
If you find this code hard-to-follow, I suggest you get a hand on C programming language by taking a look at my article at http://34.138.6.168/c-program-to-a-student-simple-text-file-database/.
Code Explanation
Lines 1 to 8
#pragma config FOSC = HS #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config BOREN = OFF #pragma config LVP = OFF #pragma config CPD = OFF #pragma config WRT = OFF #pragma config CP = OFF
These are some preprocessor directives in use.



These special lines of code are mostly auto-generated using the Configuration Bits option in MPLAB X IDE.
Let’s just use them as-is for now, and in later article let’s discuss them.
Lines 10 and 11
#define _XTAL_FREQ 20000000 #include <xc.h>
These are the preprocessor directives in use.
The #define directive is used to define a symbolic constant. Here the name of the constant is _XTAL_FREQ. The value of that constant is 20000000, which is the frequency of the crystal oscillator (20MHz). Notice that here we have followed the norm that names of symbolic constants are in uppercase in C.
The #include directive is used to include a header file. Here we have included the xc.h header file which contains important information regarding microcontroller programming.
Line 14
TRISB = 0;
In Microcontroller programming, setting a port to 0 (using TRISB = 0) sets all pins of that port (B port) to be output pins. Setting a port to 1 (using TRISB = 1) sets all pins of that port (B port) to be input pins.
According to that, here we have set all the pins of the port B to be Output pins, since our LED will be connected to one of the pins of the B port.
Line 15 to 21
while(1) { PORTBbits.RB0 = 1; __delay_ms(1000); PORTBbits.RB0 = 0; __delay_ms(1000); }
The while(1) loop is what contains all the code in our Microcontroller program.
Line 17 sets the RB0 pin to be HIGH. For this, we have to use some fancy code like PORTBbits.RB0 but don’t worry about that. It’s just some code. We know that 1 represents boolean TRUE (or digital HIGH) and 0 represents boolean FALSE (or digital LOW)
The 1000 in lines 18 and 20 represents the time in milliseconds for which the program should be delayed. Here we have used the __delay_ms() function.
Why do we set the RB0 pin to HIGH? Because that’s the pin that we connect our LED to. So when we pass a digital HIGH to our LED, it lights up. Voila!
Programming our Microcontroller
After setting up our project circuit as in the above circuit, connect the PICkit3 to your computer’s USB port.
Turn on the switch of the circuit (if not using a switch, connect the 9V battery to the terminals as in the above circuit). After setting up our project circuit as in the above circuit, connect the PICkit3 to your computer’s USB port.



Then Programming (downloading our code into the microcontroller) the Microcontroller is as easy as just clicking the button indicated in the image.



If you see the following displayed on the Output Window in MPLAB X IDE, that means you are good to go!
Remove all the connections of PICkit3 from the circuit and enjoy your success!
Conclusion



Congratulations! You did it. You’ve successfully completed your first simple LED blinking project using the PIC16F877A microcontroller. Remember that cup of tea I told you to have on hand? Take a break and have it. You deserve it!
That’s it! That’s my beginner’s guide to blinking an LED using a microcontroller (PIC16F877A).
Video media will be added soon. So stay tuned with rukbook!
It’s really advantageous 👍🏻
Thank you for the comment Vidushika
Waiting for the next blog..
Thank you for your enthusiasm Pabasara, me too!
No words to admire…. Excellent
Never hope such a Herculean task from you at the beginning…But,you have already done.
Thank you very much Nimasha for the overwhelming appreciation!
Thank u for sharing your marvellous knowledge with us brother 🤩🤩🤩
Thank you for your appreciation Pulini. It’s my pleasure
Thanks brother for sharing your knowledge with us.🤝❤.This is a great.
Thank you for your kind words Bhanuka, it’s my pleasure
Excellent mchn
Thank you Luminda
Great job bro✌️✌️waiting for the next one🤓🤓🤓
Thank you very much for your kind words Rashmika. Hopefully, I’ll get it done soon.
Great work 👍🏼
Keep it up
Thanks for the comment Raveen
Really helpful Thank U
Thank you, Akila
Excellent ! Very useful and thanks for sharing…
Thank you Sachini, you are welcome!
Great✌️Thank you for taking your time to share your knowledge❤️
Thank you Kavindu, it’s my pleasure!
Man! You have done a great job. Keep it up. Good luck for your future works too.
Thanks for the comment and the inspiration Sarjoon!
Great work Rukshan…Keep it Up…🤩
Thank you very much Lakshitha!
Good article.. Keep up the good work.
Thank you very much