You are not logged in

Log in into our community

Log in so you acess some hidden content

* Fields are required.

Register into our community!

Sign up now for some good content

* Fields are required.

Lost something?

Enter your username or email to reset your password.

* Fields are required.

Analog To Digital Convertor

Posted on : Sun , 02 2014 by : virusi


As you know from the GPIO tutorial some pin can have two or three functions and one of that function is the conversion from Analog Voltage to Digital Voltage and this is very important because microcontrollers are digital devices, they can only differentiate between HIGH (VDD) or LOW (GND) levels on input pins. One of the most important specification of an ADC is the resolution, this is how accurately ADC measures the analog input signal.Most used ADC are 8 bit, 10 bit and 12 bit. For an 8 bit ADC, the reference voltage (5v almost all the time) will be broken in 256 divisions ( 2^8 ). 1 division will mean 19.53 mV this means that if our input voltage will change very slightly we want observe it with a high accuracy. What will be the accuracy of a 12 bit ADC for the reference voltage of 5V?

Another important feature of the ADC is the sampling rate, this means how fast your ADC can read the input data. If you want to know your ADC sampling rate then you should consult microcontroller’s datasheet. For NXP LPC1768 the conversion rate is 200 KHz.

What is the voltage reference? Every ADC should have minimum (Vref-) and the maximum (Vref+) voltage range which is the total range of the input. The Vref- specifies the minimum voltage input and the Vref+ specifies the maximum voltage input.

Fig 1. ADC Vref

As you can see in our case we have connected Vref+ to 3.3V and Vref- to ground. When ADC pins for LPC1768 microcontroller are selected to be Analog to Digital converter inputs are no longer 5V tolerant and must be limited to the voltage at the Vref+(3.3V) so before connecting a input to an ADC pin you should check microcontroller’s datasheet. But what is the minimum voltage for Vref- ? The minimum Vref- for the LPC1768 is Vss which is 0v. In our case our ADC will measure from 0V to 3.3V. What will be the accuracy of a 12 bit ADC for this schematic?

ADC module is connected to several pins via a multiplexer this will allows to connect many analog inputs to the MCU. If you are interested, how many ADC pins are available than again you can check microcontrollers datasheet. For LPC1768 they are available 8 ADC pins.

When the specific channel is selected the voltage from that input channel is stored in an internal holding capacitor. It takes some time for the capacitor to get fully charged and become equal to the applied voltage. This time is called acquisition time. Once acquisition time is over the input channel is disconnected from the source and the conversion begins. The acquisition times depends on several factors like the source impedance, Vdd of the system and temperature.

ADC Requires a clock source to do its conversion, this is called ADC Clock. The time period of the ADC Clock is called TAD. It is also the time required to generate 1 bit of conversion. The ADC requires 11 TAD to do a 10 bit conversion. It can be derived from the CPU clock (called TOSC) by dividing it by a suitable division factor.

If you are interested in getting the data immediate after the conversion was done you can an interrupt can be generated and you can get the data from the ADC register.

LPC1768 Practical Example :

Let’s take a look at a simple example. Will connect to an ADC input a potentiometer and depending on the input voltage (that will be red from input) will turn on different amount of led’s.

Fig.2 ADC schematic

In order to work with the ADC we need to make some essential steps :


1. Configure the MCU clock

SystemInit();  /* initialize clocks */

2. Configure the ADC

void AdcInit(void)
 	/* select ADC function for  */	
	/* P1.31 is selected as GPIO */
	LPC_PINCON->PINSEL3 &= ~(3UL<<30);	
	/* P1.31 is selected as AD0.5 */
  	LPC_PINCON->PINSEL3 |=  (3UL<<30);  
	/* configure ADC */
	/* Enable power to ADC block */
	LPC_SC->PCONP       |=  (1UL<<12);  
                            /* select AD0.5 pin */	
	LPC_ADC->ADCR	    = (1UL<< 5)| 	
                              /* ADC clock is 25MHz/1 */
			      (1UL<< 8) | 	
                              /* enable ADC */
			      (1UL<<21) |
			      /* Start conversion now. */
	/* enable ADC interrupt */
	/* enable interrupt generation for AD0.5  */ 
	LPC_ADC->ADINTEN	|= (1UL<<5); 

3. Configure P 2.00... P 2.07 as output


1. In the ADC interrupt we need to check if the 5 channel conversion has finished and read the ADDR5 register and update ADCval variable

void ADC_IRQHandler(void)
   uint32_t adGdr = 0UL;
   uint32_t channel = 0UL;
   /* disable ADC interrupt */
   channel = LPC_ADC->ADSTAT>>5;
   /* check if conversion on the 5 channel is done */
   if(channel & 0x01)
	adGdr = (LPC_ADC->ADDR5>>4);
	ADCval  = (adGdr&ADC_MAX_VALUE);
  /* enable ADC interrupt */
  /* start a new conversion */
  LPC_ADC->ADCR |= (1UL<<24);

2. Depending on ADCval turn different numbers of led’s

	/* if Voltage is btween 0 V and 400mV turn one LED*/
	if( ADCval < 496 )
		LPC_GPIO2->FIOPIN = (1UL<<0);
	/* if Voltage is btween 400 mV and 800 mv turn on 2 LED's  */
	else if( (ADCval>992) && (ADCval<1488) )
		LPC_GPIO2->FIOPIN = (1UL<<0)|(1UL<<1);
	/* if Voltage is btween 800 mV and 1.2 V turn on 3 LED's  */
	else if( (ADCval>1488) && (ADCval<1984) )
		LPC_GPIO2->FIOPIN = (1UL<<0)|(1UL<<1)|(1UL<<2);
	/* if Voltage is btween 1.2 V and 1.6 V turn on 4 LED's  */
	else if( (ADCval>1984) && (ADCval<2480) )
		LPC_GPIO2->FIOPIN = (1UL<<0)|(1UL<<1)|(1UL<<2)|(1UL<<3);
	/* if Voltage is btween 1.6 V and 2.0 V turn on 5 LED's  */
	else if( (ADCval>2480) && (ADCval<2976) )
		LPC_GPIO2->FIOPIN = (1UL<<0)|(1UL<<1)|(1UL<<2)|(1UL<<3)
	/* if Voltage is btween 2.0 V and 2.4 V turn on 6 LED's  */
	else if( (ADCval>2976) && (ADCval<3472) )
		LPC_GPIO2->FIOPIN = (1UL<<0)|(1UL<<1)|(1UL<<2)|(1UL<<3)
	/* if Voltage is btween 2.4 V and 2.8 V turn on 7 LED's  */
	else if( (ADCval>3472) && (ADCval<3968) )
		LPC_GPIO2->FIOPIN = (1UL<<0)|(1UL<<1)|(1UL<<2)|(1UL<<3)
	/* if Voltage is greater than 2.8 V turn on all LED's  */
	else if( ADCval> 3968 )
		LPC_GPIO2->FIOPIN = (1UL<<0)|(1UL<<1)|(1UL<<2)|(1UL<<3)


If you are interested in the full example then download ADC example.
Last updated on Sat , 07 2015

This page is waiting for your comment.

Share and Leave a comment.

You must be logged in to post a comment.

Back to Top