Wednesday, May 4, 2011

Dual water tank heater thermostat


I am just finishing up a project at work, which requires two water tanks to be maintained at 180F. Each tank has twin 1500 watt, 240vac heating elements, each controlled by a SSR (Solid State Relay). My Arduino Mega 2560 reads two DS18B20 temp sensors (one in each tank), and maintains the temperature with a 5 degree window. I display both tank temperatures on a LCD, and control the color of two RGB LED's, blue for under temp, green for correct temp, red for over temp. The photo's are here. The following is the working code for the project.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

int tank1BLED = 37;
int tank1GLED = 39;
int tank1RLED = 41;
int tank2BLED = 43;
int tank2GLED = 45;
int tank2RLED = 47;

// Data wire is plugged into pin 8 on the Arduino
#define ONE_WIRE_BUS 8

// Setup a oneWire instance to 
//communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.

DeviceAddress tank2Thermometer = { 0x28, 0x46, 0x3C, 0x16, 0x03, 0x00, 0x00, 0xA9 };
DeviceAddress tank1Thermometer = { 0x28, 0xDF, 0x21, 0x16, 0x03, 0x00, 0x00, 0x1E };

int tank1 = 31; // heater control pins
int tank2 = 33; // heater control pins

float tank1temp = 0;
float tank2temp = 0;

void setup(void)
{
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(tank1Thermometer, 10);
  sensors.setResolution(tank2Thermometer, 10);
  
  
  
  pinMode(tank1BLED, OUTPUT); // Tank LED's
  pinMode(tank1GLED, OUTPUT);
  pinMode(tank1RLED, OUTPUT);
  pinMode(tank2BLED, OUTPUT);
  pinMode(tank2GLED, OUTPUT);
  pinMode(tank2RLED, OUTPUT);
  
  digitalWrite(tank1BLED, HIGH); // set Tank LED's off
  digitalWrite(tank1GLED, HIGH);
  digitalWrite(tank1RLED, HIGH);
  digitalWrite(tank2BLED, HIGH);
  digitalWrite(tank2GLED, HIGH);
  digitalWrite(tank2RLED, HIGH);
  

lcd.begin(20,4); 
// columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear(); 
// start with a blank screen

pinMode(tank1, OUTPUT); // Tank heaters
pinMode(tank2, OUTPUT);

}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  float t1tempC = sensors.getTempC(tank1Thermometer);
  float t2tempC = sensors.getTempC(tank2Thermometer);
  if (tempC == -127.00) {
lcd.print("Error");
} else {
// lcd.print(tempC);
// lcd.print("/");
tank1temp = (DallasTemperature::toFahrenheit(t1tempC));
tank2temp = (DallasTemperature::toFahrenheit(t2tempC));
lcd.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop(void)
{ 
  delay(2000);

  sensors.requestTemperatures();
  
lcd.setCursor(0,0);
lcd.print("Tank 1: ");

printTemperature(tank1Thermometer);

lcd.setCursor(0,1);
lcd.print("Tank 2: ");

printTemperature(tank2Thermometer);



if (tank1temp <=178.9)
{
  digitalWrite(tank1, HIGH);
  digitalWrite(tank1BLED, LOW);
  digitalWrite(tank1GLED, HIGH);
  digitalWrite(tank1RLED, HIGH);
}

if (tank1temp >= 179 && tank1temp <= 181.9)

{
  digitalWrite(tank1GLED, LOW);
  digitalWrite(tank1RLED, HIGH);
  digitalWrite(tank1BLED, HIGH);
}

if (tank1temp >= 182)
{
  digitalWrite(tank1, LOW);
  digitalWrite(tank1RLED, LOW);
  digitalWrite(tank1GLED, HIGH);
  digitalWrite(tank1BLED, HIGH);
}




if (tank2temp <=178.9)
{
  digitalWrite(tank2, HIGH);
  digitalWrite(tank2BLED, LOW);
  digitalWrite(tank2GLED, HIGH);
  digitalWrite(tank2RLED, HIGH);
}

if (tank2temp >= 179 && tank2temp <= 181.9)

{
  digitalWrite(tank2GLED, LOW);
  digitalWrite(tank2RLED, HIGH);
  digitalWrite(tank2BLED, HIGH);
}

if (tank2temp >= 182)
{
  digitalWrite(tank2, LOW);
  digitalWrite(tank2RLED, LOW);
  digitalWrite(tank2GLED, HIGH);
  digitalWrite(tank2BLED, HIGH);
}




}


Sunday, May 1, 2011

Sine, cosine, and tangent (the dreaded T word)

Why does trigonometry sound so scary? Often in the world of physics, and when the real world comes crashing into the lab, we need to use sine, cosine, and tangent in our calculations for having a way of relating the angles in a triangle to the lengths of the sides. Here is a simple tutorial that will act as a refresher if you have forgotten, or a brand new look at a useful set of equations if you never took trig.

http://www.physics.uoguelph.ca/tutorials/trig/trigonom.html