I want measure temperature with arduino uno and SMT160-30,
specification of SMT160 :
http://blogs.epfl.ch/document/3748
I used this code :
//Sample code to fetch temperature using SMT16030 sensor
//I'd suggest you to use powerline noise filtering and polarity damage protection RC kit with sensor.
//
//
int SensorPin= 7; //Digital pin
float DutyCycle = 0; //DutyCycle that need to calculate
unsigned long SquareWaveHighTime = 0; //High time for the square wave
unsigned long SquareWaveLowTime = 0; //Low time for the square wave
unsigned long Temperature = 0; //Calculated temperature using dutycycle
void setup()
{
Serial.begin(115200); //Initialized serial according to your wish
pinMode(SensorPin, INPUT); //Sets the specified digital pin as an input
}
void loop()
{
DutyCycle = 0; //Initialize to zero to avoid wrong reading
Temperature = 0; //due to incorrect sampling etc
//Read high time for square wave
SquareWaveHighTime = pulseIn(SensorPin, HIGH);
//Read low time for square wave
SquareWaveLowTime = pulseIn(SensorPin, LOW);
DutyCycle = SquareWaveHighTime; //Calculate Duty Cycle for the square wave
DutyCycle /= (SquareWaveHighTime + SquareWaveLowTime);
//Compute temperature reading according to D.C.
// where D.C. = 0.320 + 0.00470 * T
Temperature = (DutyCycle - 0.320) / 0.00470;
Serial.print("\nDuty Cycle: "); //Print for debugging if needed
Serial.print(DutyCycle);
Serial.print(" Calculated Temperature (*C): ");
Serial.println(Temperature);
//Wait for a while, I'm a micro guy. Can't compute as fast as you!
delay(1000);
}
but results are very unstable and measured temperature is jumping +-2°C.
What's wrong ? Is code ok ? How to improve ?
Thanks for help
Alda