8-Bit Digital Output

Getting a digital I/O pin to control a Light Emitting Diode (LED) can be a rewarding project in itself. It confirms that you have completed the software build, the hardware connection, and were able to program the microcontroller successfully. For this reason, this first project will step through a simple example of flashing an LED from a microcontroller pin.


Step by Step Instructions

Using the MPLAB® Code Configurator (MCC), this project controls an LED from a digital output. An LED connected to one of the output pins of the microcontroller will light up.


The project uses:

  • PIC16F18875
  • HPC Curiosity Board
  • MPLAB Code Configurator (MCC) plug-in
  • MPLAB XC8 Compiler

To follow along with these steps, MPLAB X IDE should be open and the HPC Curiosity Board connected to the computer through a USB cable.


Create a new standalone project in MPLAB X for a PIC16F18875.

If this is your first time creating an MPLAB X project, please visit the "Create a Standalone Project" page to follow a step-by-step instruction on how to do this.


Open the MPLAB Code Configurator under the Tools > Embedded menu of MPLAB X IDE.



Select the peripherals for your project. For this project, the following peripherals need to be selected:

  • System Module
  • Interrupt Module
  • Pin Module

The System Module, Interrupt Module, and the Pin Module will all be automatically included when you launch the MCC. The result should look like the picture below:



Open the Pin Manager. Then, click on the PORTA pin 4 (RA4) Output blue lock symbol, which is connected to the D2 LED. Once selected, it should turn green. This adds this RA4 I/O pin to the project. It should look like the picture below when completed:



Close the Pin Manager and then click on the Pin Module selection in the Project Resources section.


The RA4 pin will be made an output pin and control the LED. Click on the Output box to make the pin an output and uncheck the WPU and Analog boxes. Then, change the RA4 pin name to "LED _D2".



Close the Pin Manager. Next, click on System Module in Project Resources to open the center section.


In this section, the oscillator settings and the configuration settings are selected.


  1. Select the HFINTOSC from the Oscillator Select drop-down menu.
  2. Select the 4_MHz selection from the HF Internal Clock drop-down menu.
  3. Select a Clock Divider value of 4.

This will enable the internal 1 MHz internal oscillator as the system clock.


Also, check that the Low-voltage Programming Enable mode is selected at the bottom of the System Module screen.



Click on the Generate Code button to have the MCC create the software libraries for this project.



The project will now have both generated Header Files and Source Files. It should also have a generated a main.c file.


Double click on main.c to open it up in the editor window.



Add the following code to the end of the main.c file:

void main(void)
    // initialize the device

    while (1)

__delay_ms(250) is a delay function built into the XC8 compiler

The LED_D2_Toggle() is a macro generated by the MCC and its definition is located in the pin_manager.h file.

#define LED_D2_Toggle()             do { LATAbits.LATA4 = ~LATAbits.LATA4; } while(0)

This is a simple example of how the MCC generates a library of useful macros to make creating project code much easier and quicker.


Click on the Build Project icon (the hammer) to compile the code; you should see a BUILD SUCCESSFUL message in the output window of MPLAB X IDE.



Make sure your project has the Curiosity Board selected and a USB cable is connected to the board.

Click on Make and Program Device. This will build the project again and launch the programmer. In the Output window, you should see a series of messages and, if successful, it will end with a "Programming/Verify complete" message.


Output Window:

Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.34.11
Firmware type..............Enhanced Midrange

Target detected
Device ID Revision = 6

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
Device Erased...

Programming/Verify complete

The D2 LED connected to RA4 will be blinking on the HPC Curiosity Board.

If it's the first time the programmer is connected to the board, the programming tool may need to download the proper operating firmware for the exact device. You may see a series of processes if this occurs. This should only happen once:

Downloading Firmware…
Downloading bootloader
Bootloader download complete
Programming download…
Downloading RS…
RS download complete
Programming download…
Downloading AP…
AP download complete
Programming download…
Firmware Suite Version…..01.34.11
Firmware type…………..Enhanced Midrange


The project can be closed in MPLAB X IDE. The project is saved automatically when it is built, but any changes to files or configuration may ask to be saved before the project is closed.

The project can be closed under the File Menu > Close Project.



If you have any problems with your project, the completed MPLAB X project file can be downloaded from the link below:

File Download
Windows Linux Mac OSX
Project 1 Files
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.