Watch the Video Tutorial

Most of PIC microcontrollers today have built-in analog to digital converters (ADC) with the number of channels depending on the number of pins a particular microcontroller have.
Analog signals: Directly measurable quantities in terms of some other quantity
Some examples:
Thermometer: The mercury liquid inside the thermometer rises as temperature rises
Car Speedometer: Needle of a car speedometer moves farther right as you accelerate
Audio Amplifier: The volume of an audio amplifier increases as you turn the knob.
Digital Signals: Have only two states. or 1on or off.
Example: A switch can be either on or off.

The PIC18F4620 (40 pins) has 13 built in ADC channels, the PIC18F2620 (28 pins) has 10 built in ADC channels, the PIC18F1220 (18 pins) has 7 built in ADC channels etc.

These analog to digital converters allow analog continuous voltages to be converted into a discreet digital numbers inside the PIC as the PIC can only process digital numbers. This can enable a PIC to be connected to analog sensors such as temperature sensors, pressure sensors, humidity sensors, optical sensors, and power sensors.

Any sensor which can generate a voltage between 0V and a maximum 5V can be used. If the output voltage is higher than 5V, a method to step it down should be used such as a voltage divider with resistors.

Conversion

To convert an analog signal to digital format, we have two main steps to process:
1. Quantizing which is basically to break down analog value in a set set of finite states
Example: Let say you have 0-5V signals.
The number of possible states that the converter can output is: N=2n
where n is the number of bits in the AD converter.
For a 3 bit A/D converter, N =23=8.
Analog quantization size: Q=(Vmax-Vmin)/N = (5V – 0V)/8 = 0.625V.
We will have a set of voltages from 0 to 5V in a set of discrete states with 0.625V increments:
0V to 0.625V
0.625V to 1.25V
1.25V to 1.875V
1.875V to 2.5V
2.5V to 3.125V
3.125V to 3.75V
3.75V to 4.375V
4.375V to 5V

The resolution of the converter indicates the number of discrete values it can produce over the range of analogue values. The resolution is usually expressed in bits and it is equals to the quantization size (Q).
(Q) = Vrange / 2^n, where Vrange is the range of analog voltages which can be represented.
The higher the resolution, the accurate the converted value. Most of PIC18F microcontrollers have analog channels with a 10-bit resolution.

2. Encoding which is now to assign a digital word or number to each state and matching it to the input signal.
If 0V is detected by a 10-bit ADC channel, then once the conversion is complete, 00 0000 0000 should be stored in memory. On the other extreme, if 5V is detected by the ADC channel, 11 1111 1111 should be stored in memory. Any value in between will produce a corresponding binary number.

MikroC Pro for PIC Analog To Digital (ADC) Library Functions

Below is a quick descriptions of ADC library, For more information, please visit online the mikroC pro for PIC ADC library page.

ADC_Init

This function initializes PIC’s internal ADC module. Before you can use this function, make sure the microcontroller used has an ADC module.

Example: 

This function acquires analog value from the specified channel.
This function returns 10 or 12-bit unsigned value read from the specified channel (depending of a PIC used).
Before you can use this function, the ADC module must be initialized first. (check ADC_Init() function ).
Note:

–> This function doesn’t work with the external voltage reference source, only with the internal voltage reference.
–> Before you can use this function, be sure to configure the appropriate TRISx bits to designate pins as inputs.

Prototype: unsigned ADC_Get_Sample(unsigned short channel);
Parameter: The channel parameter represents the channel from which the analog value is to be acquired.
You must refer to the datasheet for channel-to-pin mapping.

Example:

ADC_Read

This function initializes PIC’s internal ADC module and aquires analog value from the specified channel.
This function returns 10 or 12-bit unsigned value read from the specified channel (depending of a PIC used).
Before you can use this function, the ADC module must be initialized first. (check ADC_Init() function ).
Note:

–> This function doesn’t work with the external voltage reference source, only with the internal voltage reference.
–> Before you can use this function, be sure to configure the appropriate TRISx bits to designate pins as inputs.

Prototype: unsigned ADC_Read(unsigned short channel);
Parameter: The channel parameter represents the channel from which the analog value is to be acquired.
You must refer to the datasheet for channel-to-pin mapping.

Example:

Example  

Display the voltage read from analog channel 6 to the LCD display as shown on the circuit diagram above.

You can download the full project files (MikroC source code and Proteus Schematic design) below here.  All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).

Download ADC mikroC Source code
Download ADC Proteus MikroC