In this tutorial we will set up and read the temperature from LM35 using ADC of Atmega32 from Starter AVR board.

Basic

Initially we will read a raw data from ADC. Atmega32 comes with 10-bit 8-channel inbuilt ADC. We will connect 10k Pot to channel 0, vary it and read the raw voltage. After that we will connect the LM35 temperature sensor to channel 0 and read the temperature as shown in hook up. We will display the converted value on terminal.
Refer Reading Temperature with AVR ADC for basics of Atmega32 ADC and LM35 temperature sensor.

Hookup

0 LM35 Starter AVR.png Instead of LM35 temperature we can use on board pot to read the raw data from ADC.

Code to Read Voltage

This code is for reading a raw data from a ADC0 pot on board, which is connected to PA0

#include "adc.h"
#include "uart.h"
int main()
{
int adcValue;
float volt;
ADC_Init(); /* Initialize the ADC module */
UART_Init(9600); /* Initialize UART at 9600 baud rate */
while(1)
{
adcValue = ADC_GetAdcValue(0); // Read the ADC value of channel zero
volt = (adcValue*5.00)/1023; //10bit resolution, 5vReference
UART_Printf("ADC0 Value:%4d Equivalent Voltage:%f\n\r",adcValue,volt); // Send the value on UART
}
return (0);
}

Code to Read Temperature

This code is to read data from LM35, connected to PA1.

#include "adc.h"
#include "uart.h"
int main()
{
int adcValue;
int temp;
ADC_Init(); /* Initialize the ADC module */
UART_Init(9600); /* Initialize UART at 9600 baud rate */
while(1)
{
adcValue = ADC_GetAdcValue(1); // Read the ADC value of channel zero where the temperature sensor(LM35) is connected
/* Convert the raw ADC value to equivalent temperature with 5v as ADC reference
Step size of AdC= (5v/1023)=4.887mv = 5mv.
for every degree celcius the Lm35 provides 10mv voltage change.
1 step of ADC=5mv=0.5'c, hence the Raw ADC value can be divided by 2 to get equivalent temp*/
temp = adcValue/2.0; // Divide by 2 to get the temp value.
UART_Printf("ADC0 Value:%4d Equivalent Temperature:%dC\n\r",adcValue,temp); // Send the value on UART
}
return (0);
}

Demo

To display the ADC value open the terminal connect the COM port, we will get output as shown in below image. ADC temp.PNG

Downloads

Download the complete project folder from the below link: https://github.com/ExploreEmbedded/ATmega32_ExploreStarterAvr/archive/master.zip


Have a opinion, suggestion , question or feedback about the article let it out here!

Setting Up Starter AVR

In this tutorial we will look at setting up the starter AVR board. Once you have done with this basic set up, you can use on board peripherals as well as many other peripherals which can be connected...

Blinky with Starter AVR

After setting up starter AVR board, we will start with simple LED blinking experiment. Two user LED's are provided on starter AVR board, we will use same for this experiment. Basic ...

Interfacing LCD with Starter AVR

In this tutorial we'll look at how to interface different types of character LCD's to the starter AVR board. Basic Starter AVR board has female connector on board to connect LCD's...

Interfacing Seven Segment Display with Starter AVR

Now we will go further and will interface more complex peripherals. In this tutorial we will interface common anode 7 segment display to the starter AVR board. To get idea about basics of 7 segment...