Ora

What is ADC in PIC microcontroller?

Published in PIC Microcontroller Peripherals 5 mins read

The Analog-to-Digital Converter (ADC) in a PIC microcontroller is a crucial internal peripheral that translates real-world analog signals, such as temperature, light intensity, or pressure, into a digital format that the microcontroller can understand and process.

Understanding the ADC in PIC Microcontrollers

PIC microcontrollers, like many other microcontrollers, operate digitally, meaning they primarily work with binary values (0s and 1s). However, the physical world is largely analog, characterized by continuously varying signals. An ADC acts as a bridge, converting these continuous analog inputs into discrete digital values.

Specifically, the ADC in many PIC microcontrollers is capable of converting an analog input signal into a 10-bit binary digital representation of that signal. This means it can distinguish between 2^10 = 1024 different voltage levels within its reference voltage range.

Why is an ADC Essential?

Microcontrollers need to interact with various sensors and devices in the real world. Most sensors output analog signals (e.g., a thermistor's resistance changes with temperature, leading to a varying voltage). Without an ADC, a PIC microcontroller would be unable to read and respond to these vital inputs, severely limiting its application in embedded systems.

How the PIC ADC Works

The process of converting an analog signal to a digital one involves several steps within the PIC's ADC module:

1. Analog Input Channels

PIC microcontrollers often have multiple pins that can be configured as analog input channels. These Microchip MCU analog inputs are multiplexed into a single sample and hold circuit. This means that although there are several physical input pins, only one analog signal can be converted at a time. The multiplexer selects which analog pin's voltage is fed to the sample and hold circuit.

2. Sample and Hold Circuit

Once a channel is selected, the sample and hold circuit takes a snapshot of the analog voltage at that precise moment and holds it constant. This is crucial because the conversion process takes time, and the input voltage might change during this period. Holding the value ensures an accurate conversion of the sampled voltage.

3. Conversion Process

The held analog voltage is then fed into the actual converter. The PIC's ADC typically uses a Successive Approximation Register (SAR) method. This method compares the input voltage against a series of internally generated reference voltages to determine the digital output bit by bit, starting from the Most Significant Bit (MSB).

4. Digital Output

After the conversion is complete, the 10-bit digital result is stored in dedicated ADC result registers (typically ADRESH and ADRESL). The microcontroller's CPU can then read these registers and use the digital value in its program logic.

Key Features and Specifications

Understanding the specifications of a PIC's ADC is vital for effective programming and hardware design:

  • Resolution: Commonly 10-bit, offering 1024 discrete levels. Higher resolution (e.g., 12-bit) ADCs provide finer granularity and better accuracy.
  • Conversion Time: The time it takes for the ADC to complete one conversion. This is influenced by the ADC clock source and is specified in the PIC's datasheet.
  • Input Channels: The number of physical pins capable of acting as analog inputs.
  • Reference Voltage (Vref): The maximum voltage that the ADC can measure. Signals exceeding Vref or falling below Vss (ground) will be clipped or interpreted incorrectly. This can be an internal reference, the power supply voltage (VDD), or an external reference voltage applied to dedicated pins.
  • ADC Clock Source: The clock signal that drives the ADC module. It must be carefully chosen to meet the minimum and maximum conversion time requirements for accurate operation.

ADC Control Registers

To configure and operate the ADC module, specific Special Function Registers (SFRs) are used:

Register Purpose
ADCON0 Controls ADC operation, including channel selection, Go/Done bit, and ADC ON/OFF.
ADCON1 Configures analog input pins, voltage reference source, and result justification.
ADRESH Stores the Most Significant Bits (MSBs) of the 10-bit conversion result.
ADRESL Stores the Least Significant Bits (LSBs) of the 10-bit conversion result.

For detailed bit descriptions and configurations, always refer to the specific PIC microcontroller's datasheet, which can be found on Microchip's official website.

Practical Applications of PIC ADC

The ADC functionality opens up a wide array of possibilities for embedded systems:

  • Temperature Sensing: Reading NTC thermistors or analog temperature sensors (e.g., LM35) to monitor environmental conditions.
  • Light Sensing: Interfacing with photoresistors (LDRs) to detect light levels for automatic lighting systems or light-following robots.
  • Voltage Monitoring: Measuring battery voltage, power supply levels, or input signal amplitudes.
  • User Interface: Reading potentiometers for volume control, motor speed adjustment, or menu navigation.
  • Environmental Monitoring: Connecting to gas sensors, humidity sensors, or pressure sensors for various applications.
  • Motor Control: Providing feedback for closed-loop motor control systems by measuring current or position.

Example: Reading a Potentiometer

To read a potentiometer using a PIC's ADC, you would typically:

  1. Connect: Connect the potentiometer's wiper (variable output) to an analog input pin (e.g., RA0). Connect the other two ends to VDD and VSS.
  2. Configure ADC:
    • Set the analog input pin (RA0) as an analog input in ADCON1.
    • Select the correct reference voltage (e.g., VDD and VSS).
    • Select the channel (AN0 for RA0) in ADCON0.
    • Choose an appropriate ADC clock source.
    • Enable the ADC module (ADON bit in ADCON0).
  3. Start Conversion: Set the GO/DONE bit in ADCON0 to initiate the conversion.
  4. Wait for Completion: Poll the GO/DONE bit until it clears, indicating the conversion is finished. Alternatively, use an ADC interrupt.
  5. Read Result: Read the 10-bit result from ADRESH and ADRESL.
  6. Process: Use the digital value in your application logic (e.g., map it to a specific range or control an output).

The ADC is a fundamental component for bridging the gap between the analog real world and the digital domain of PIC microcontrollers, enabling them to be versatile tools for a vast range of embedded applications.