Here I describe a class for reading out the LM335 temperature sensor. This class is based on Chiptemp. See here for a detailed description of the software: Chip temperature measurement
LM335.pde
#include "LM335.h"
LM335::LM335(int _ADCpin)
{ ADCpin = _ADCpin;
}
inline void LM335::initialize()
{ delay(10); // wait for the analog reference to stabilize
readAdc(); // discard first sample (never hurts to be safe)
}
inline int LM335::readAdc()
{ return analogRead(ADCpin);
}
int LM335::deciCelsius()
{ long averageTemp=0;
initialize(); // must be done everytime
for (int i=0; i<lm335_samples; i++) averageTemp += readAdc();
averageTemp -= lm335_offsetFactor;
return averageTemp / lm335_divideFactor; // return deci degree Celsius
}
int LM335::celsius()
{ return deciCelsius()/10;
}
int LM335::deciFahrenheit()
{ return (9 * deciCelsius()+1600) / 5;
}
int LM335::fahrenheit()
{ return (9 * deciCelsius()+1600) / 50; // do not use deciFahrenheit()/10;
}
LM335.h
#ifndef LM335_H
#define LM335_H
// LM335 temperature sensor interface
// Rev 1.0 Albert van Dalen www.avdweb.nl
// Resolution 0.1 degree
// Calibration values, set in decimals
static const float lm335_offset = 559; // change this!
static const float lm335_gain = 2.048;
static const int lm335_samples = 1000; // must be >= 1000, else the gain setting has no effect
// Compile time calculations
static const long lm335_offsetFactor = lm335_offset * lm335_samples;
static const int lm335_divideFactor = lm335_gain * lm335_samples/10; // deci = 1/10
class LM335
{
public:
LM335(int _ADCpin);
int deciCelsius();
int celsius();
int deciFahrenheit();
int fahrenheit();
private:
inline void initialize();
inline int readAdc();
int ADCpin;
};