Display Temperature

In this project we display the temperature on the display. We use the LM335 temperature sensor to read the temperature. The sensor gives the temperature in degrees Kelvin. The output of the sensor is via a resistor connected to the ADC0 input of the ATMEGA328 microcontroller. The analog signal is converted to a digital number, then this number is converted to the temperature in degrees Celcius. Below you find the schematic and the C-code.

 // project : atm328 - led display HDSP2111 - LM335

// Version : 1.0
// Website : www,avrprojects.net
// Date Created : 07.05.2014
// Target : ATmega328P microcontroller
// Language : C WINAVR
// Hardware requirements: LED display HDSP2111, ATMEGA328 board
// Description : read the temperature of the LM335

// INCLUDES
#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include <string.h>

// used pins on port B
#define LCD_DB4 0 // PORTD.0 pin 2 -> LCD DATA4 pin 11
#define LCD_DB5 1 // PORTD.1 pin 3 -> LCD DATA5 pin 12
#define LCD_DB6 2 // PORTD.2 pin 4 -> LCD DATA6 pin 13
#define LCD_DB7 3 // PORTD.3 pin 5 -> LCD DATA7 pin 14
#define CE 3 // PB3 pin 12 -> Chip Enable


// pulse to clock the data in the display
void pulse (void)
{
_delay_us(100);
PORTB |= 1<<CE;
_delay_us(100);
PORTB |= 0<<CE;
_delay_us(100);
}

// init LCD
void LED_init(void)
{
DDRD = 0xFF; // PORTD as output
PORTD = 0x00;
DDRB = 0xFF; // PORTB as output
PORTB = 0xFF;
}

//wriet a charecter to the display
void writechar(char address, char input)
{
PORTB = address;
PORTD = input;
pulse();
}

// write text to the display
void writetxt(char *word)
{
char blank = 0;
for (char i=0;i<8;i++)
{
if (!blank && *word == 0)
writechar(i, ' ');
else
writechar(i, *word++);
}
}

// this function read the value of ADC channel 0 (PORTC0)
unsigned int read_adc(void)
{
ADCSRA |= 1<<ADSC; //start conversion;
while (ADCSRA&(1<<ADSC)); //wait conversion end
{
return ADCW;
}
}

int main(void)

{
//adc init
ADMUX = 0b00000000; // set ADC0
ADCSRA = 0b10000110; //set ADEN, precale by 64
LED_init();
char stra[2];
char strb[8];
char mystr[2];

while(1)
{
writetxt(" ");
int rawdata = read_adc();
float millivolt = (rawdata/1024.0)*5000;
float celcius = (millivolt/10)-273;
itoa(celcius,mystr,10);
strcpy(strb,""); // initialize strb!
strcpy(stra, mystr);
strcat(strb, "+");
strcat(strb, stra); // add stra to strb
strcat(strb, "C"); // add stra to strb
writetxt(strb);
_delay_ms(1000);
}

}