Thermostat

So lately I've been playing with a new toy: The Arduino. It's a microcontroller prototyping board. Quite fun. I bought a kit which came with all sorts of sensors and LEDs/motors/etc.

So the first real project (other than making lights blink in fun patterns) was a thermostat for our air conditioning. Here's a little description of the layout:

Sensors

  • Temperature Sensor - basically a thermal resistor. Every degree Celsius translates to a 5mV change in voltage.
  • Potentiometer - a variable resistor, most frequently used in stereos for volume control. (Think walkman wheel control)

Output

  • 8 LEDS (4 Green, 4 Red) - This is my crude display for binary/ hex data. The red lights display one hex digit, the green the other. This displays the current value the potentiometer is set at (the triggering temperature);
  • Servo Motor - I have this motor set to turn 100 degrees in the event that the temperature is too high (turning on the A/C) and turn back if it drops down again.

The code is pretty simple. Read in the values, tweak the numbers a bit so they are in the range we want them to be. Then there's a simple if/else statement dealing with the temperature. I have it polling every second, but in reality I could probably lower it to once a minute.

Enjoy

EDIT: after leaving it to run overnight, I found it not working in the morning. My wife suggested that it drained the battery. After looking at it, I think having it poll so often uses way too much power. I switched it to be powered off of USB instead, and changed the polling interval to 5 minutes.

#include <servo.h>

Servo myservo;  
int leds[] = {2,3,4,5,6,7,8,9};  
int control = 1;  
int temp = 0;  
int pos = 0;  
int sensorValue = 0;

void setup(){  
  myservo.attach(10);
  for (int i = 0; i < 8; i++){
    pinMode(leds[i],OUTPUT);
  }
}

void loop(){  
  sensorValue = analogRead(controlc);
  int maxTemp = sensorValue / 10 + 10; //(use F values between 10 and 112)
  writeFF(maxTemp);
  float temperature = analogRead(temp) * .004882814;
  temperature = (temperature - 0.5) * 100 * 1.8 + 32; // convert to F
  if (temperature > maxTemp){
    myservo.write(100);
  } else {
    myservo.write(0);
  }
  delay(1000);
}

void writeFF(int a){  
  int values[8];
  for(int i = 0; i < 8; i++){
    values[i] = a % 2;
    a = a / 2;
  }
  for (int i = 0; i < 8; i++){
    digitalWrite(leds[i],values[i]);
  }
}
comments powered by Disqus