LED Control Using Potentiometer

In this project, the brightness of an LED (Light Emitting Diode) is controlled by the PIC16F18855 microcontroller using the Xpress development board platform.


Hardware Tools (Optional)

Tool About Purchase
MPLAB® Xpress
Development Board

Software Tools

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment

Exercise Files

File Download
Windows Linux Mac OSX
Project and Source Files


The project uses:

  • MPLAB Xpress Cloud-Based Integrated Development Environment with MPLAB Code Configurator Plug-in found here: MPLAB Xpress Plug In
  • PIC16F18855 Microcontroller: www.microchip.com/pic16f18855
  • MPLAB Xpress Development Board

To follow along with these steps, MPLAB® Xpress should be open and the user should be logged in so that the MPLAB Code Configurator plug-in can be used.


Open the MPLAB® Code Configurator under the Tools > Embedded menu of MPLAB Xpress.


A java installation file will automatically download, named standalone.jnlp. Open this file.
Once completed, the Code Configurator window will automatically open.
Note: Follow the instructions for Opening MCC in Xpress if this is your first project .


The first window that opens automatically is the system module. The system module is where core device operational parameters are setup such as oscillator and configuration bits selection. Select the settings shown in the picture.

Oscillator Select: HFINTOSC (1MHz)
HF Internal Clock: 4MHz
CPU Divisor: 4


This project uses the Analog-to-Digital Converter (ADC) to read the input from the on-board potentiometer. This potentiometer is connected to pin RA4 on the board, as can be seen from the section of the Xpress board schematic below:


To see the full schematic and details on this board visit here .

Therefore we need an ADC, a PWM (to send the brightness signal to the LED), and a TMR (to run the PWM).


Double click on these peripherals in the resources area to add them to your project.


Ensure that the appropriate package is selected in the Pin Manager using the Package drop-down menu. This project utilizes the Xpress board populated with a PIC16F18855 in 28-pin UQFN package:


Connect the ADC to the potentiometer (Pin RA4), as well as the PWM to the LEDs (RA0, RA1) in the MCC Pin Configurator by clicking on the locks show in the picture to change them from blue to green.


Analog-to-Digital Converter: The ADC is used to collect information from transducers, which gather information from the physical world, and turn it into electrical signals. Analog data, the type of data gained from the physical world, can have an almost infinite range of values. However, computers must work with only ones and zeros. Therefore, an ADC converts the physical world range of values into computer ones and zeros, with higher bit resolution providing higher accuracy.

For the purposes of this application, there are a few things to change in the ADCC block. Update the Clock Source to FRC and update the Result Alignment to right justification so that the ADC is reading the correct end of the updated ADC data array.



LED Brightness: This project uses the PWM block to send different signals to the LED based upon the values read from the ADC. Change the PWM block timer to TMR4:


We also need to update TMR4 to change the Clock Source to FOSC/4



To make the code easier to understand, change the RA4 pin name to "Pot" in the Pin Module:



Programming: This tutorial requires us to add some code to the main.c file in the Source Files in the Project. In order to learn to interface with the software in the code, it is always helpful to look in the Header Files to find the functions for the different blocks being used.

Under the adcc.h header file, we find the function:

adc_result_t ADCC_GetSingleConversion(adcc_channel_t channel);

which returns the current ADC value. The adc_result_t gives the type of variable that the function returns, and the adcc_channel_t channel gives the source of the input variable. Therefore, we want to replace adcc_channel_t channel with the Pot variable (with which we replaced the name of RA4).

Second, we want to be able to control the PWM duty cycle with the ADC output. Therefore, we name the output variable of the above function PotVal and give this to the function:

void PWM6_LoadDutyValue(uint16_t dutyValue);

found in the pwm6.h header fille.

This results in the code below:

{adc_result_t PotVal = ADCC_GetSingleConversion(Pot);


Create the .hex file by clicking the Make and Program Device icon.



Program the MPLAB Xpress board by dragging the project .hex file from the downloads section of the browser and dropping the file on to the XPRESS drive.


The Programmer LED on the Xpress board will quickly flash from green to red and then back to green indicating that the .hex file was successfully programmed to the PIC16F18855.


If configured correctly, the potentiometer will control the fading of the LEDs:



This simple project shows how to use the Xpress board to use a few of the more common peripherals to control a digital output with an analog input. This can be expanded in many ways.

© 2016 Microchip Technology, Inc.
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.