Analog Input

Signal voltages are not always digital such as zero volts or five volts. Many times they are analog, in that they can vary in voltage level, and that is where an Analog-to-Digital converter is used to convert the input signal from an analog voltage level to a digital value that the microcontroller can process. This project monitors a potentiometer controlled voltage that lights up and LED when the threshold is crossed.


Step by Step Instructions

Using the MPLAB® Code Configurator (MCC), this project creates an analog input to read a potentiometer and control an LED from a digital output. When the potentiometer is turned right of the center position the LED will light up. When the potentiometer is turned left of the center position the LED will be off.


The project uses:

PICDEM Lab II Connections:

  • Connect 5 V and Gnd to the power bus of the protoboard.
  • Connect 5 V to the Vdd pin of the PIC16F1769
  • Connect Gnd to the Vss pin of the PIC16F1769
  • Connect a wire from the RA2 pin (pin 17) to the breadboard Row 23
  • Connect a wire from the ground bus to breadboard Row 27
  • Connect the LED Anode to breadboard Row 23
  • Connect the LED Cathode to breadboard Row 24
  • Connect a 330 ohm resistor between Row 24 and Row 27
  • Connect a wire from the RA4 pin (pin 3) to the breadboard Row 8
  • Connect the potentiometers three pins to row 7,8 and 9
  • Connect a wire between Row 9 and the breadboard ground bus
  • Connect a wire between Row7 and the breadboard power bus

To follow along with these steps, MPLAB X should be open and the Programmer/Debugger connected to both the computer and the Development Board. The setup was described in the Setup and Installation section of this training module. You should see a screen similar to the one below to move on to step 1.



Create a new "Stand-Alone" project in MPLAB X for a PIC16F1769. Instructions are below if this is your first project.


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



Select the Peripherals for your project.
In this project these peripherals will need to be selected:

  • System Module
  • Interrupt Module
  • Pin Module
  • ADC

The System Module, Interrupt Module an Pin Module will be automatically included but the ADC must be selected for the project. The list of peripherals will show up under the Device Resources list. Double click on the ADC to have it added to the project. The result should look like the picture below.



Open the Pin Manager and then click on the PORTA pin 2 (RA2) blue lock symbol in the Output row to make it locked and green. This adds this RA2 I/O pin to the project to drive the LED. Click on the PORTA pin 4 blue lock in the ANx row to make it green. This adds the RA4/AN3 input to the project. Vref+ and Vref- should be left to their default values. The Reset line should not have any green locks. It should look like the picture below when completed.



Close the Pin Manager and then click on the Pin Module in the Project Resources to open the center section. It will show RA2 and RA4 listed on the I/O chart.

  1. The RA2 pin will be an output and control the LED. Click on the output box to make the pin an output and uncheck the WPU and Analog boxes.
  2. The RA4 pin is the potentiometer input, so check the Analog box and uncheck the output and WPU boxes if checked.


The System needs to be setup next. Click on the System name in the Project Resources list. It should be highlighted in blue when selected.


The System section will appear. In this section the oscillator settings and the configuration settings are selected.


  1. Select the INTOSC from the drop down menu for the System Clock Select.
  2. Select the 1MHz_HF selection from the Internal Clock drop down menu.

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



Each configuration setting can be changed under the Register tab of the System window. Match the selections shown here.



The ADC needs to be setup. Click on the ADC label in the Project Resources menu to make the ADC setup screen appear.

  1. Check the ADC Enable box and select the FOSC/2 clock source from the drop down menu.
  2. Click on the Right selection for Result Alignment. This will produce a 10-bit ADC result.
  3. Leave Vref+ as VDD and Vref- as VSS.

Note: The name of the RA4 pin is called "channel_AN3". We will use this later in the main.c file.

The window should look like below after the proper setup.



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.
Note: MCC may offer to generate a main.c file. Click yes to allow it to complete that task.


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



The ADC result will produce a value between 0 and 1023 depending on the position of the potentiometer. A value of 0 is the position all the way counterclockwise. A value of 1023 is all the way clockwise. The half-way point is a value of 512. We shall test the position by comparing the value to 512. If it larger than 512 then the LED will be lit. If it's smaller than or equal to 512, then the LED will be off.

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

while (1)
        // Add your application code

        if (ADC_GetConversion(channel_AN3) > 512)


Note: The IO_RA2_SetHigh() and IO_RA2_SetLow() functions are macros that were generated by the MCC, and their definitions are located in the pin_manager.h file.

#define IO_RA2_SetHigh()    do { LATA2 = 1; } while(0)
#define IO_RA2_SetLow()   do { LATA2 = 0; } while(0)


Click on the Build Project Icon (the hammer) to compile the code. You should see a BUILD SUCCESSFUL message in the output window.



Make sure your project has the programming tool selected (Part of Step 1 above) and connect power to your development board.

The PICkit 3 has limited power capability so we recommended powering the board separately.
The ICD 3 can power a development board, but for simplicity sake we recommend powering the board separately.
The RealICE cannot power the development board so powering the board separately is required.

Click on the Make and Program Device icon. 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 and Verify Successful 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 RA2 LED should light up when the RA4 potentiometer is turned clockwise more than the half-way point. The picture below shows the potentiometer beyond the half-way point so the LED is lit.

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 make 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 the 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 3 Files

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

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