LED Blinking with PIC Microcontroller

In: Project Code: PE000004
Share

In this project you will learn how to blink an led connected to the GPIO pins of PIC16F877A microcontroller using MPLAB IDE and PICKIT programmer. If you don't have access to your own hardware for this project, you can and execute it remotely. If you don't have your own hardware for this project, you can and execute it remotely.

Subscribe to enable complete access to all , and and earn certificates from .

Complete this project and update to consider it for requirements and earn certificate.

course thumb

Project Content


Required hardware and software


The required hardware and software for this project is as below:

  • A breadboard
  • 1 x 4mm LED
  • 1 x resistor between 220 Ohm to 1 kOhm
  • Hookup cables
  • MPLAB IDE from Microchip
  • PICKIT3 Programmer, connecting cable from PICKIT3 to your computer
  • Development board hosting PIC16F877A Microcontroller
  • Power supply for the development board
  • 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 current is applied to the leads, electrons are able to recombine with electron holes within the device, releasing energy in the form of photons. Light-emitting diodes are used in applications as diverse as aviation lighting, automotive headlamps, advertising, general lighting, traffic signals, camera flashes, lighted wallpaper and medical devices.

  • Light emitting diode(LED) has +ve and -ve polarity. The long lead of led is +ve and short lead of led is -ve.
  • The resistor has no polarity. So you can connect it in any direction
  • I am using our company for this project. You can use any PIC development board for this project. You can also and execute the project remotely from anywhere.


    Schematic diagram


    The schematic diagram for this project is as shown below:

    Required Components

    PIC16f877A GPIO description


    PIC16F877A is a 8-bit microcontroller. Download the datasheet of the microcontroller . It has total 40 pins out of which 33 pins are used for GPIO. PIC16F877A has 5 ports namely PORTA, PORTB, PORTC, PORTD, PORTE. The GPIO pins are used interfacing different input/output devices to the PIC microcontroller.

    PORTA - PORTE registers in PIC16F877A microcontroller have alternate functions which means when a pin in the register is used for alternate function it cannot be used for general purpose input/output functions. This is determined by the internal register settings configured by the user program. So to use as a led control pin we need to configure the pin connected to the led as GPIO output.

    GPIO Registers

    Register Register Description
    TRISX Determines whether the PORT or pin is used for input or output. If TRISX bit=1, the corresponding port pin is set as input. If TRISX bit=0, the corresponding port pin is set as output.
    PORTX The actual data that will be written on the PORT or pin.

    The PORTA - PORTE registers, their pins and alternate functions are defined in the following table.

    PORTA - PORTE Registers, pins, alternate functions

    Register Pins Alternate functions
    PORTA PA0 - PA5 Analog to Digital Converter(ADC).
    PORTB PB0 - PB7 Interrupts.
    PORTC PC0 - PC7 PWM, UART, I2C.
    PORTD PD0 - PD7 Parallel slave port.
    PORTE PE0 - PE2 Analog to digital converter(ADC).

    Building the software program, creating binaries


    Before starting on software development, let me introduce you some basic stuff on programming. To solve a problem you are creating a project/product. You need to develop source code for the project. To edit source code you need an editor or IDE. You need a compiler to compile your source code. We will use MPLAB X IDE for this project. I have downloaded and installed XC8 compiler which is detected by the MPLAB IDE and used to compile the source code. Follow the below steps to edit source code, compile source code, create binary for your project:

  • Download MPLAB X IDE 5.05 from and install it in your computer.
  • Create a project, click on file->New Project. Choose project as standalone project. Click Next >.
  • Select device as Baseline 8-bit MCUs and device as PIC16F877A. Click Next >
  • Select Tool as PicKit3. Click Next >
  • Select compiler as XC8. Click Next >
  • Provide a project name and folder to store it in your computer. Click Finish
  • On the MPLAB IDE Projects window you will see the project created. I have named the project as led. Right click on the Source Files under project led and click on New->C Source file. Give the name of file as led.c and click on Finish.
  • Double click on the led.c file under Source Files and enter the following code in it.
  • Right click on project led from the project list and click on Build. You will see Build Successful on the output window of MPLAB IDE.

  • led.c program to blink led connected at RA0 pin.
    
    #include "xc.h"
    #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
    #pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
    #pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
    #pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = ON         // Data EEPROM Memory Code Protection bit (Data EEPROM code-protected)
    #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
    #define _XTAL_FREQ 11059200     //11.0592MHZ
    #define DELAY 100 
    void main()
    {
      TRISAbits.TRISA0 = 0;
      while(1)
      {
          PORTAbits.RA0 = 1;
          __delay_ms(DELAY);
          PORTAbits.RA0 = 0;
          __delay_ms(DELAY);
      }
    }
    

    In the above program led.c we have configured RA0 pin as output by configuring TRISAbits.TRISA0 to 0. Led is an output device. As RA0 pin is configured as output we can connect an led to the RA0 pin. Then in an infinite while loop PORTA RA0 pin is set to 1 or 0 by configuring PORTAbits.RA0 to 1 or 0. A delay of 100ms is provided when led state changes between on and off.

    Loading binaries on the microcontroller


    When you install MPLAB X on your computer, you will get two shortcuts like MPLAB IDE and MPLAB IPE on your desktop. We used MPLAB IDE for editing source code, compiling the source code and creating binaries. Now we will use MPLAB IPE to load the binary on the Microcontroller. Following steps will help you to load binary on the microcontroller.

  • Connect the PICKIT-3 pins 1 - 5 to the programming header P2 on the development board
  • Connect other end of the PICKIT3 programmer to your computer USB port
  • Open MPLAB IPE and enter family as Baseline 8-bit MCUs
  • Enter device as PIC16F877A
  • Automatically you should see Tool field is filled up with PICKIT3 serial NO
  • Click on apply
  • Click on Connect. A caution message will pop up and click on OK. You will see message Target device PIC16F877A found.
  • Click on Browse and give the path of the hex file in the folder where you saved your project
  • Click on Program and you will see Programming complete message on Output IPE window
  • This is captured in the following snapshot

    IPE Output

    Project output observations, video explanations


    Project output observations

    Once you load the binary in the microcontroller, you should observe that the led connected at the RA0 pin should blink with a delay of 100ms.

    Update this project completion for online courses

    If you are considering this project to complete any online course click to update your project status. To see your project completion in online course status report you will be redirected to your online course status page.
    Looking for a new project? Write and upload your project information .
    Share

    Schedule a project

    Embedded Systems Projects

    Linux Software Projects

    Articles

     Online Certificate Courses

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude