Reading Temperature with Starter AVR
In this tutorial we will set up and read the temperature from LM35 using ADC of Atmega32 from Starter AVR board.
Contents
[hide]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
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.
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!