Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Complete Sensor Guide
      • Engineering Deep Dives
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Tech Terms
    • Webinars & Digital Events
  • Resources
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • White Papers
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to work with inbuilt ADC Module of PIC Microcontroller (PIC18F4550)- (Part 11/25)

By Amit Joshi June 14, 2011

A microcontroller, a digital device, can read, execute and transmit only digital signals. On the contrary, the outputs of the most of the transducers are analog in nature. Thus it is hard to interface these transducers directly with controllers. Analog-to-digital convertor (ADC) ICs are one way to make the analog input compatible with the microcontroller.

Using an external ADC adds complexity to the circuit. To avoid this complexity, PIC Microcontrollers have in-built ADC module which reduces the cost and connections of the circuit. This article explains the in-built ADC of PIC18F4550 controller.


 

 

As mentioned in the summary, a PIC microcontroller has inbuilt ADC for A/D conversion. The ADC module of PIC18F4550 controller has following specifications:

·         10-bit resolution output which means that an analog input gets converted into a corresponding 10-bit digital output.
·         13 channels which means that a total of 13 analog signals can be converted simultaneously into digital.
·         Vref+ (RA3) and Vref- (RA2) pins for external reference voltage.
·         8 selectable clock options.
·         ADC can be in auto-triggering mode for continuous A/D conversion.
 
 
ADC Registers:
To work with the inbuilt ADC of this PIC microcontroller, the certain registers are required to be configured. Each of these ADC registers has been explained below.
 
1. ADCON0 (A/D CONTROL REGISTER 0)
 
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
—
—
CHS3
CHS2
CHS1
CHS0
GO/DONE
ADON
 
Fig. 2: Bit Configuration of ADCON0 Control register enable/disable the ADC peripheral of PIC
 
ADON: This bit is used to enable/disable the ADC peripheral of the PIC.
1 = A/D converter module is enabled
0 = A/D converter module is disabled
GO/DONE: This is A/D conversion status bit. For ADON=1,
1 = A/D conversion in progress
0 = A/D Idle
CHS3: CHS0: These bits are used to select a particular analog channel from 13 available channels (0-12) which are multiplexed with digital I/O pins. The following table shows the bit configuration to select these analog channels:
 
CHS3:CHS0
Analog Channel
Pin
0000
Channel 0
RA0/AN0
0001
Channel 1
RA1/AN1
0010
Channel 2
RA2/AN2
0011
Channel 3
RA3/AN3
0100
Channel 4
RA5/AN4
0101
Channel 5
RE0/AN5
0110
Channel 6
RE1/AN6
0111
Channel 7
RE2/AN7
1000
Channel 8
RB2/AN8
1001
Channel 9
RB3/AN9
1010
Channel 10
RB1/AN10
1011
Channel 11
RB4/AN11
1100
Channel 12
RB0/AN12
 
Fig. 3: Bit configuration to select analog channels in PIC
 
2. ADCON1 (A/D CONTROL REGISTER 1)
 
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
—
—
 VCFG1
VCFG0
PCFG3
PCFG2
PCFG1
PCFG0
 
PCFG0:PCFG3: As mentioned earlier, there are 13 analog channels in PIC18F4550 which are multiplexed with digital I/O pins. This means that such a (multiplexed) pin can act as either a digital I/O pin or an analog input pin. Either of these configurations is selected by these bits. The following table shows the bit configuration to make a pin D (Digital I/O) or A (Analog input):
 
PCFG3:
PCFG0
AN12
AN11
AN10
AN9
AN8
AN7
AN6
AN5
AN4
AN3
AN2
AN1
AN0
0000
A
A
A
A
A
A
A
A
A
A
A
A
A
0001
A
A
A
A
A
A
A
A
A
A
A
A
A
0010
A
A
A
A
A
A
A
A
A
A
A
A
A
0011
D
A
A
A
A
A
A
A
A
A
A
A
A
0100
D
D
A
A
A
A
A
A
A
A
A
A
A
0101
D
D
D
A
A
A
A
A
A
A
A
A
A
0110
D
D
D
D
A
A
A
A
A
A
A
A
A
0111
D
D
D
D
D
A
A
A
A
A
A
A
A
1000
D
D
D
D
D
D
A
A
A
A
A
A
A
1001
D
D
D
D
D
D
D
A
A
A
A
A
A
1010
D
D
D
D
D
D
D
D
A
A
A
A
A
1011
D
D
D
D
D
D
D
D
D
A
A
A
A
1100
D
D
D
D
D
D
D
D
D
D
A
A
A
1101
D
D
D
D
D
D
D
D
D
D
D
A
A
1110
D
D
D
D
D
D
D
D
D
D
D
D
A
1111
D
D
D
D
D
D
D
D
D
D
D
D
D
 
Fig. 4: Bit configuration to make pin D (Digital I/O) or A (Analog input) in PIC
 
VCGF1-VCGF0: These are voltage configuration bits to select the reference voltage (Vref) for ADC module.
 
Bit
= 1
= 0
VCGF0
Vref+ (RA3)
Vcc
VCGF1
Vref- (RA2)
Vss (GND)
 
 Fig. 5: Voltage configuration bits for reference voltage (Vref) in ADC module
 
3. ADCON2 (A/D CONTROL REGISTER 2)
 
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
ADFM
—
ACQT2
ACQT1
ACQT0
ADCS2
ADCS1
ADCS0
 
 
ADCS2:ADCS0: These bits are used to select the clock option for ADC peripheral. The following table shows the bit configuration to select from different clock options:
 
ADCS2:ADCS0
Clock Option
000
FRC (clock derived from A/D RC oscillator)
001
FOSC/64
010
FOSC/16
011
FOSC/4
100
FRC (clock derived from A/D RC oscillator)
101
FOSC/32
110
FOSC/8
111
FOSC/2
 
Fig. 6:Bit configuration to select clock for ADC peripheral
 
ACQT2:ACQT0:  These bits are used to set the acquisition time of the ADC. The acquisition time is the time required to charge and discharge the holding capacitor of the ADC.  
ADFM: This bit is used to select the format of digital output.
1 = Right justified (LSB to MSB)
0 = Left justified (MSB to LSB)
 
 
4. ADRESL & ADRESH:
Since the ADC module of PIC provides 10-bit digital output after A/D conversion, this output is stored in two 8-bit registers, namely, ADRESL & ADRESH. The lower byte is stored in ADRESL (A/D Result High register) while the higher byte is stored in ADRESH (A/D Result Low register).
 
 
Working with ADC 
Objective: To select an analog channel of PIC18F4550’s in-built ADC and provide an analog input (0 to 5 volt) to it using a variable resistor or preset (20 k)
 
 
Fig. 6: Symbol of Ohm
 
 
(See circuit diagram) The main objective is to read the analog signal and display the corresponding digital value on LCD. (Also see interfacing LCD with PIC)
 
Programming Steps:
1. Set number of analog inputs by setting PCFG3:PCFG0 (ADCON1).
2. Select the analog channel by setting CHS3:CHS0 bits (ADCON0).
3. Select the clock option, acquisition time output format by configuring the ADCON2 register.
4. Enable the ADC by making ADON bit (ADCON0) high.
5. Start the conversion by setting GO/DONE bit (ADCON0).
6. Wait until GO/DONE bits becomes low. This indicates that the A/D conversion is over.
7. Store A/D conversion result from ADRESH:ADRESL into a variable.
8. Covert the resultant value to its corresponding ASCII value and display on LCD.

 

Project Source Code

###

// Program to depict working with inbuilt ADC of PIC18F4550 Microcontroller
// This code uses Channel0 (zero) of PIC's ADC Module

// Configuration bits
/* _CPUDIV_OSC1_PLL2_1L,  // Divide clock by 2
   _FOSC_HS_1H,           // Select High Speed (HS) oscillator
   _WDT_OFF_2H,           // Watchdog Timer off
   MCLRE_ON_3H            // Master Clear on
*/

#define rs LATA.F0
#define rw LATA.F1
#define en LATA.F2
#define lcdport LATB

void lcd_ini();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void adc_con(unsigned int);
void adc_init();

unsigned char data[20]="ADC OUTPUT=";
unsigned int digital_out[10],avg_output=0,temp;
unsigned int i=0;


void main()
{
    TRISA=0x01;        // Configure RA0 as input pin
    LATA=0;
    TRISB=0;        // Configure Port B as output port
    LATB=0;
    TRISD=0;
    LATD=0;
    lcd_ini();        // LCD initialization
    while(data[i]!='')
    {
        lcddata(data[i]);      // Call lcddata function to send character one by from 'data' array
        i++;
    }

    adc_init();        //ADC Initialization

    while(1)
    {
        temp=0;
        for(i=0;i<10;i++)
        {
            ADCON0|=(1<<GO);                              // Start A/D conversion
            while(!(ADCON0 & (1<<GO)));                   // Wait until conversion gets over
            digital_out[i]=((ADRESL)|(ADRESH<<8));        // Store 10-bit output into a 16-bit variable
            Delay_ms(20);
            temp=temp+digital_out[i];
        }
        avg_output=temp/10;                           // Take average of ten digital values for stablity
        adc_con(avg_output);                          // Function to convert the decimal vaule to its corresponding ASCII

    }
}


void adc_init()
{
    ADCON1=0x0E;                            // Make RA0/AN0 pin as analog pin (Other pins remain to be digital I/O)
    ADCON0=0x00;                            // Select Channel0 & ADC off
    ADCON2=0x8A;                            // Left justified, 2TAD acquiciation time, Fosc/32 clock option
    ADCON0.ADON=1;                          // Enable ADC
}


void lcd_ini()
{
    lcdcmd(0x38);        // Configure the LCD in 8-bit mode, 2 line and 5x7 font
    lcdcmd(0x0C);        // Display On and Cursor Off
    lcdcmd(0x01);        // Clear display screen
    lcdcmd(0x06);        // Increment cursor
    lcdcmd(0x80);        // Set cursor position to 1st line, 1st column
}


void adc_con(unsigned int adc_out)
{
    unsigned int adc_out1;
    int i=0;
    char position=0xC3;

    for(i=0;i<=3;i++)
    {
        adc_out1=adc_out%10;                     // To exract the unit position digit
        adc_out=adc_out/10;
        lcdcmd(position);
        lcddata(48+adc_out1);                    // Convert into its corresponding ASCII
        position--;

    }
}


void lcdcmd(unsigned char cmdout)
{
    lcdport=cmdout;        //Send command to lcdport=PORTB
    rs=0;                        
    rw=0;
    en=1;
    Delay_ms(10);
    en=0;
}


void lcddata(unsigned char dataout)
{
    lcdport=dataout;    //Send data to lcdport=PORTB
    rs=1;
    rw=0;
    en=1;
    Delay_ms(10);
    en=0;
}

###

 


Circuit Diagrams

Circuit-Diagram-of-How-to-work-with-inbuilt-ADC-Module-of-PIC-MicrocontrollerPIC18F4550

Project Components

  • LCD
  • PIC18F4550
  • Preset

Project Video


Filed Under: PIC Microcontroller
Tagged With: adc, microcontroller, pic18f4550
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

Log in to leave a comment:

Lost your password?

Don't have an account? Register here

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Wide Bandgap Semiconductors
Moving from silicon to GaN or SiC feels like a whole new ballgame, doesn’t it? It isn’t just about faster switching; it’s about managing the layout parasitics and thermal realities that come with that speed. This month’s Tech Toolbox features a curated eBook that tackles these design hurdles head-on.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS feed: EDABOARD.com Discussions. EDABOARD.com Discussions.

  • A quarter of a century on the EDAboard
  • Ground pour won't go to GND vias in KICAD
  • Request for a 5W transistor controlled LED flash light controller circuit review
  • How to select the pre-charge current for low-current charging solutions?
  • Research Discussion: Detailed Study of Phase and Amplitude Imbalances in Quadrature LC CMOS Oscillators

RSS feed: Electro-Tech-Online.com Discussions Electro-Tech-Online.com Discussions

  • Voltage divider response time - is it relevant?
  • Request for a 5W transistor controlled LED flash light controller circuit review
  • Delco alternator (12SI) AC-tap - what is the mate to this connector called and where to find it?
  • Frustration, single phase, high KW, high voltage battery charge module, rectifier.
  • On off switch hack

Featured Tutorials

VHDL Series (1-24)

  • VHDL Tutorial 17: Design a JK flip-flop (with preset and clear) using VHDL
    VHDL Tutorial 17: Design a JK flip-flop (with preset and clear) using VHDL
  • VHDL Tutorial 18: Design a T flip-flop (with enable and an active high reset input) using VHDL
    VHDL Tutorial 18: Design a T flip-flop (with enable and an active high reset input) using VHDL
  • VHDL Tutorial – 19: Designing a 4-bit binary counter using VHDL
    VHDL Tutorial – 19: Designing a 4-bit binary counter using VHDL
  • VHDL Tutorial – 20: Designing 4-bit binary-to-gray & gray-to-binary code converters
    VHDL Tutorial – 20: Designing 4-bit binary-to-gray & gray-to-binary code converters
  • VHDL Tutorial – 21: Designing an 8-bit, full-adder circuit using VHDL
    VHDL Tutorial – 21: Designing an 8-bit, full-adder circuit using VHDL
  • VHDL Tutorial – 22: Designing a 1-bit & an 8-bit comparator by using VHDL
    VHDL Tutorial – 22: Designing a 1-bit & an 8-bit comparator by using VHDL
More Tutorials >

Recent Articles

  • Stackpole adds aluminum nitride substrates to chip resistors
  • Microchip adds parallel sensing to capacitive-touch controllers
  • Infineon introduces 27 kW three-phase AI server PSU
  • Mini-Circuits releases Butler matrices for MIMO testing
  • OMNIVISION introduces 12MP global shutter image sensor

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2026 Arrowfly LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of Arrowfly
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Complete Sensor Guide
      • Engineering Deep Dives
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Tech Terms
    • Webinars & Digital Events
  • Resources
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • White Papers
  • Guest Post Guidelines
  • Advertise
  • Subscribe