Sunday, October 26, 2008

Standalone temperature logger with Arduino


As I mentioned in the previous post, I am in the process of insulating the attic. Of course, that couldn't go without some sort of measurement to check the effect of said insulation. Therefore, I decided to check the temperature delta from the inside of the attic to the outside before applying the stuff, and after.
So yesterday I finally decided to connect an LM35 to an arduino board, and get it to log the temperatures. Of course, I needed 2 LM35 connected to it. Luckily, I had 2 around, from Joao (sorry for the lack of the tilde), and one of them was even already on a long lead, this from my initial attempts at reading temperature with the computer - Joao then built a parallel port temperature sensor interface.
I built the circuit in the figure using the arduino. The diode is to enable the setup to read negative temperatures, which was not yet necessary, but will be soon.
Since I didn't want to have a computer running all the time just for that I made the arduino store the values into the EPROM. There are 512 bytes in the EPROM, so, for 2 sensors I could have 256 measurements. I decided to get a measurement every 30 minutes, which would allow the setup to run for about 5 days before running out of place in the memory.
To read the contents of the EPROM, I had initially thought of having a switch which would indicate the mode to run in - log or report - , but went for the easier solution of dumping all the contents of the EPROM upon starting the arduino.

So, yesterday I placed the thing in the attic, tried to get the long sensor outside, didn't quite managed, but it was pretty close to be out in the cold.

This morning I couldn't resist: went to get it, downloaded the data and plotted it in Excel. I get a constant 2 degrees difference between the inside and the outside, which I think is very small, might explain why the house gets cold so quickly.

Here is the arduino code. It is not clean nor pretty, but seams to do the job:





/*
* temperature logger
*
* reads and logs 2 temperature sensors to the EPROM
*

*/
#include

int refPin = 1; // select the input pin for the potentiometer
int potPin = 2; // select the input pin for the potentiometer
int potPin2 = 3; // select the input pin for the potentiometer
int val = 0; // variable to store the value coming from the sensor
int ref;
int out;
int val2 = 0; // variable to store the value coming from the sensor
int out2;

int avout, avin;
int addr = 0;
int index = 0;
int outtemps[10];
int intemps[10];

int ledPin = 13; // LED connected to digital pin 13
int value = LOW; // previous value of the LED
long previousMillis = 0; // will store last time LED was updated
long logpreviousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to blink (milliseconds)
long loginterval = 1800000; // interval at which to log (milliseconds)

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600);

Serial.println("ready to dump! data every 30 minutes");
Serial.println("address \t out \t in");

for (int in = 0; in<256; in++)
{

value = EEPROM.read(in*2);

Serial.print(in);
Serial.print("\t");
Serial.print(value, DEC);
Serial.print("\t");
value = EEPROM.read(in*2 + 1);
Serial.print(value, DEC);
Serial.println();

}


}

void loop() {
ref = analogRead(refPin); // read the value from the sensor
val = analogRead(potPin); // read the value from the sensor
val = val - ref;
out=val*500/1023;
val2 = analogRead(potPin2); // read the value from the sensor
val2 = val2 - ref;
out2=val2*500/1023;

outtemps[index] = out;
intemps[index] = out2;
index++;
if (index == 10)
{
index = 0;
}

delay(100);

// check to see if it's time to blink the LED; that is, is the difference
// between the current time and last time we blinked the LED bigger than
// the interval at which we want to blink the LED.
if (millis() - previousMillis > interval) {
previousMillis = millis(); // remember the last time we blinked the LED

// if the LED is off turn it on and vice-versa.
if (value == LOW)
value = HIGH;
else
value = LOW;

digitalWrite(ledPin, value);
}

//log the temperatures to the eprom
if (millis() - logpreviousMillis > loginterval) {
logpreviousMillis = millis(); // remember the last time we blinked the LED

avout = 0;
avin = 0;
for (int aa = 0; aa<10 ; aa++)
{
avout += outtemps[aa];
avin += intemps[aa];
}
avout = avout/10;
avin = avin/10;


EEPROM.write(addr*2, avout);
EEPROM.write(addr*2+1, avin);
addr = addr + 1;
if (addr == 256)
addr = 0;

Serial.print(avout);
Serial.print(" - ");
Serial.println(avin);

// if the LED is off turn it on and vice-versa.
if (value == LOW)
value = HIGH;
else
value = LOW;

digitalWrite(ledPin, value);
}


}

1 comment:

João Silva said...

Fotos, Fotos, Fotos...
queremos fotos. se for necessário ir lá a casa tirar-te umas fotos a colocar o isolamento avisa :-)