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.

 Materials

Hardware Tools (Optional)

Tool About Purchase
Xpress-50px.png
MPLAB® Xpress
Development Board

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

 Procedure

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.

1

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

Figure0.png

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 .

Figure01.jpg

The first window that opens automatically is the system module. The system module is where core device operational parameters are setups 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

2

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:

Figure1.JPG

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).

Figure2.JPG

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

3

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:

Figure3.JPG

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.

4

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.

Figure4.JPG

5

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:

Figure5.JPG

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

Figure6.JPG

6

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

Figure7.JPG

7

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 file.

This results in the code below:

while(1)
{adc_result_t PotVal = ADCC_GetSingleConversion(Pot);
PWM6_LoadDutyValue(PotVal);
}

8

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

Main_Program_Target_Project.png

9

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.

Figure10.png

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.

10

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

Figure10.jpg

 Conclusions

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.

© 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.