Interfacing of Temperature Sensor with Atmega 16-ADC

Interfacing of Temperature Sensor with Atmega 16


In the previous article, I have written about the basic concepts of an ADC and some essential features of the ADC in Atmega16/32. In this article, I am going to tell you about the ADC initialization, taking the value through the ADC from a temperature sensor and printing that value to the LCD. So, in this article, we will interface a temperature sensor with Atmega 16 microcontroller. You can buy a temperature sensor directly from here by

temperature sensor

*Please note that the above buying link is an Affiliate link, therefore you will be helping us big time if you buy the sensor from the above link.

ADC of an AVR

There are various things needed to know about ADC of an AVR, these are:

  • ADC Prescaler- The ADC needs clock pulse to do its conversion. This clock is provided by the system clock by dividing it into smaller frequency. You can choose between 50 KHz to 200 KHz but there is a trade-off between the speed and the accuracy. At higher frequency, the conversion is fast while a lower frequency the conversion is more accurate. So, we need to choose prescaler by keeping two things in mind-
  1.          Accuracy
  2.          It should lie within the acceptable range of frequencies.

The system clock can be divided by 2, 4,16,32,64,128 by setting the Prescaler.

  • ADC Channels– There are eight channels in the ADC of Atmega16/32. That means you can connect the sensor in any of the eight pins of PORTA. The maximum number of devices that can be connected to the ADC is 8.
  • ADC Registers– By far, you have already understood that any process in the microcontroller (like a timer), there is set of registers that are used to configure and control the process. Same is the case with ADC. The ADC of the Atmega 16 has four registers.
  • ADC Multiplexer Selection Register – ADMUX- For the selection of the reference voltage and the input channel.
  • ADC Control and Status Register A – ADCSRA –As the name says it has the status of ADC and is also used for controlling it.
  • The ADC Data Register – ADCL and ADCH –The final result of the conversion is stored here.

Setting up and using the ADC

Here, we will set up and use the ADC in single conversion mode. I am going to interface a temperature sensor with the Atmega 16 and print its value in 16×2 LCD.  For this, we have to configure the ADC, and this is done by setting up the ADMUX and ADCSRA registers.

THE ADMUX Register

learn adc in avr

The bit REFS1 and REFS0 set the reference voltage. The values of these bits decide the reference voltage of the ADC.  I am going to 2nd option, i.e., AVcc. Therefore, we have to set REFS0 bit with ’1’.

The ADCSRA Register-

  • ADEN – Set this to 1 to enable ADC
  • ADSC – We need to set this to one whenever we need ADC to do a conversion. This starts the conversion.
  • ADIF – This is the interrupt bit this is set to 1 by the hardware when the conversion is complete. So we can wait until conversion is complete by polling this bit like

//Wait for conversion to complete
while(!(ADCSRA & (1<<ADIF)));

The loop does nothing while ADIF is set to 0, it exists as soon as ADIF is set to one, i.e. conversion is complete.

  • ADPS2-ADPS0 – These bits are used to select the prescaler. As I have already mentioned earlier, the frequency could be between 50 KHz to 200 KHz. We can reach there by choosing the appropriate prescaler. We can select the prescaler from 2,4,16,32,64,128. As we know already that our microcontroller is fed with the frequency of 4 MHz, therefore, we can select __ as a prescaler because on dividing the frequency with this factor, the frequency comes in the range supported by the ADC.

ADCL and ADCH – ADC Data Registers

data register in adc

The result of the ADC conversion is stored here. Since the ADC has a resolution of 10 bits, it requires 10 bits to store the result. Hence one single 8-bit register is not sufficient. We need two registers – ADCL and ADCH (ADC Low byte and ADC High byte) as follows. The two can be called together as ADC.

Reading the value-

Now we have everything set up. The steps that we have to done in the program is-

  • Configure the ADMUX
  • Start and Monitoring the conversion from ADCSRA.
  • Reading the data from the ADC data register.

The Program:

Connect the sensor output pin to the first pin of PORTA. connect Vcc to 5v of your supply and ground to ground.

#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
int i; // for storing 10 bit data
int main(void)
	ADMUX = (1<<REFS0);
	ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1);
	LCDCommands(0x01);// Clear Display Screen
	LCDCommands(0x06);// Entry mode set, Sets the cursor move direction(left to right)
	LCDCommands(0x0c);// Display on, Cursor Off
	LCDCommands(0x38);// Function set:8-bit, 2 line, 5x7 Dots
	while (1)
		LCDCommands(0x80);// Force cursor to begin with 1st row
                ADCSRA=(1<<ADSC);// Starting the conversion
		while(ADCSRA&(1<<ADIF)==0);// Waiting till conversion ends
		//ADCSRA=(1<<ADIF);//clearing the flag for next conversion
		i=ADC;			//taking the data
		LCDdisplay("Reading-%d ",i);//Printing the display
void LCDCommands(char a)
	PORTD=0x01;// 0 for RS, 0 for RW, 1 for EN makes 00000001 equals 1.
void LCDdisplay( char a[] )
	for( j = 0; j <strlen(a) ;j++ )
		PORTB = a[j];
		PORTD = 0x05;// 1 for Rs, 0 for RW, 1 for EN(for high to low pulse)
		PORTD = 0x04;// 1 for RS, 0 for RW, 0 for EN makes 0100=0x04.
		if( j == 12 )
			LCDCommands(0xC0);// force the display to move in 2nd line

Processing the Reading:

Now, it depends on you and how are you going to use the reading. You can give various conditions by using IF ELSE statement and make your controller do a variety of things. For example, you can interface a relay along with it and control the behavior of your AIR Conditioner system or you can calibrate it’s reading and make a thermometer of your own!

*please note that I have tried to make my program error-free. But my programmer is broken, so I was not able to check it. Please do comment me, if you encountered any error. I will update this post as soon as I replaced my programmer.

Leave a Comment

Your email address will not be published. Required fields are marked *

Share This
Scroll to Top