Temperature result in serial monitor

After mapping an LM335 temp sensor (10mv per degree C.) I thought I would see a .5 C resolution. What might I be missing? I'm new to Arduino and C++.
I only see a whole number +.00 C

const int numReadings = 20;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
float average = 0;                // the average

int inputPin = A0;

void setup() {
  // initialize serial communication with computer:
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;

void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(inputPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;

  // calculate the average:
  average = total / numReadings;
  average = map(average, 526, 758, -20, 90);
  // send it to the computer as ASCII digits
  Serial.println(" C");
  delay(1);        // delay in between reads for stability

All your calculations are dealing with integers which then get put into a float. So only integer values will be the result.
You must override the integers with a "float" in that last calculation.

I have defined average as a float. I guess I don't understand what I need to change.

Yes, but nothing in that calculation is being done as a float unless you tell the compiler to assume the other pieces of the calculation are float.

average = (float)total /(float)numReadings;

But then you need to look at the documentation for "map". Doesn't and can't work with float.

average = float (total) / float (numReadings) ;

More: https://stackoverflow.com/questions/16221776/why-dividing-two-integers-doesnt-get-a-float

... and ... this too for deeper understanding:

Added ...
When dealing with casts, either of these two syntax will compile and provide the same answer:

#include <Streaming.h>

int a = 5;
int b = 4;

void setup() {

void loop() {

  float average = (a + b) / 2;
  Serial << "I Average=" << average << "\n";
  average = ((float) a + (float) b) / 2;

  Serial << "F1 Average=" << average << "\n";
  average = (float (a) + float (b)) / 2;
  Serial << "F2 Average=" << average << "\n";


> I Average=4.00
F1 Average=4.50
F2 Average=4.50

Hi @cliffcoulter,

You are using the map() function to generate and print the final value.
The map() function only works with integer numbers.

For the mathematically inclined, here’s the whole function

long map(long x, long in_min, long in_max, long out_min, long out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

Ref: map() - Arduino Reference

try this way

  // calculate the average:
  average = (total / numReadings)*100;
  average = map(average, 52600, 75800, -2000, 9000);
  // send it to the computer as ASCII digits
  Serial.println(" C");

RV - mineirin

You nailed that one. Interesting and informative on the mapping change you made. Probably understanding the map function math helps. I'll take your word on that one. Thanks

Correct form ;

average = (float(total) / float(numReadings))*100;
  average = map(average, 52600, 75800, -2000, 9000);
  // send it to the computer as ASCII digits
  Serial.println(" C");

Better Yet. That's fun.... Very helpful!

Any reason you wouldn't define the variables as floats to begin with?

No, no reason.
you can define them at the beginning yes.
And defining in the beginning does not need to define in the calculation.

RV - mineirin

Thanks for the great explanation!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.