ATMEGA32U2 and LM35DZ Temperature Responses

Out of curiosity and the difficulty in finding a decent Arduino IDE sketch for measuring the internal temperature sensor in the ATmega32U4, I decided to post it here.  Pretty much the same behavior on the measurement as the ATmega328, just a little different in implementation based on the datasheet section 24.6.

The sketch below has three measurements; the internal ATmega32U4 sensor, the LM35DZ with a Ring Buffer smoothing and the same LM35DZ with no smoothing.

//Internal and External Temperature Sensor Sketch for ATmega32U4

int sample_count;
float val;
const int BUF_SIZE = 8;
int lm35_values[BUF_SIZE]; // Ring buffer
int lm35_index = 0; // Buffer location
int sample_total = 0;
float temp;
float temp2;

void setup()

Serial.println(F(“Internal Temperature Sensor”));

void loop()
// Show the temperature in degrees Celcius.
Serial.print(F(” “));
Serial.print(F(” “));

double GetTemp(void)
double t;

// Set the internal reference and mux for the ATmega32U4.

ADMUX = 0b11000111;
ADCSRB |= (1 << MUX5); // enable the ADC

delay(5); // wait for voltages to become stable.

ADCSRA |= _BV(ADSC); // Start the ADC

// Detect end-of-conversion
while (bit_is_set(ADCSRA,ADSC));


ADCSRA |= _BV(ADSC); // Start the ADC

// Detect end-of-conversion
while (bit_is_set(ADCSRA,ADSC));
byte low = ADCL;
byte high = ADCH;

t = (high << 8) | low;
t = (t – 273 + 5) / 1.00; //Convert from Kelvin to Celcius plus Offset

return (t);

double GetLM35(void)

val = analogRead(0);

// Moving total of samples in buffer
sample_total = sample_total + val;
sample_total = sample_total – lm35_values[lm35_index];
lm35_values[lm35_index] = val;

// Increment index
if (lm35_index >= BUF_SIZE)
lm35_index = 0;

temp=(2.56 * sample_total * 100.0) / (1024.0 * BUF_SIZE);
return (temp);

double GetLM35raw(void)
val = analogRead(0);
temp2=(val * 2.56 * 100.0) / 1024.0;
return (temp2);

And the results:


Some tuning of the gain and offset should adjust to match, but my Arduino Leonardo has a severe dislike of my Windows 8 serial connections, so I will retire from this project for now.


Posted on January 23, 2014, in ATmega32U4. Bookmark the permalink. 2 Comments.

  1. Did you incorporate the power factor correction constant? Without PFCC your results will be inaccurate.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: