Teensy ++2.0 Control 1 relay with 2 Temp Sensors

Status
Not open for further replies.
Code update2

Here is code so far. And some pics of the whole assembly in the enclosure. (3D CAD) I think i got it the way i want . Will soon be machining all cutouts and such in the enclosure. Will post when i get updates.
Thanks again,
Troy

Code:
//This version has all Serial Prints and GLCD Prints at begining of loop()
//Last Updated: 3/26/2015 @ 9:41PM
//This code will control 2 pumps and a Peltier Cooler with 4 analog 10k thermisitor sensors. For
//a Solar Hot Water system consisting of Solar Collectors, Solar Collector Tank and a potable
//hot water tank.
//AnalogRead(0) is sensor at HotWaterTank.
//AnalogRead(1) is sensor at SolarCollectorTank.
//AnalogRead(2) is sensor at SollarCollectors.
//AnalogRead(3) is sensor inside of electronics enclosure.
//PIN_D0 is pin D0 on Teensy++2.0 board. It controls On/Off of HotWaterTank pump relay.
//PIN_D1 is pin D1 on Teensy++2.0 board. It controls On/Off of SolarCollectors pump relay.
//PIN_D2 is pin D2 on Teensy++2.0 board. It controls On/Off of Peltier Cooler & Inside fan relay.
//PIN_D3 is pin D3 on Teensy++2.0 board. It controls On/Off of Peltier outside fan relay.

#include <Time.h>  
#include <Wire.h>  
#include <openGLCD.h>

unsigned long interval1 =5000;  // the time we need to wait
unsigned long previousMillis1=0; // millis() returns an unsigned long.

unsigned long interval2 =1000;  // the time we need to wait
unsigned long previousMillis2=0; // millis() returns an unsigned long.

unsigned long interval3 =1000;  // the time we need to wait
unsigned long previousMillis3=0; // millis() returns an unsigned long.

unsigned long interval4 =2000;  // the time we need to wait
unsigned long previousMillis4=0; // millis() returns an unsigned long.

unsigned long interval5 =100000;  // the time we need to wait
unsigned long previousMillis5=0; // millis() returns an unsigned long.

float TemperatureSensorInput;
float fahrenheit = 0;

void setup()
{
  delay(500);
  GLCD.Init(NON_INVERTED); // initialise the library, non inverted writes pixels onto a clear screen
  GLCD.ClearScreen();
  GLCD.ClearScreen();

  Serial.begin(38400);
  pinMode(PIN_D0, OUTPUT);
  pinMode(PIN_D1, OUTPUT);
  pinMode(PIN_D2, OUTPUT);
  pinMode(PIN_D3, OUTPUT); 
}

void loop()                     
{
  //This is the Time and Date print out. It uses both Wire.h and Time.h files.
  GLCD.SelectFont(TimesNewRoman9);
  GLCD.CursorTo(0,0);
  GLCD.Puts("UpTime ");
  GLCD.PrintNumber(hour());
  GLCD.Puts("h");
  GLCD.PrintNumber(minute());
  GLCD.Puts("m");
  GLCD.PrintNumber(second());
  GLCD.Puts("s ");
  GLCD.Puts("D");
  GLCD.PrintNumber(day());
  GLCD.Puts("/");
  GLCD.Puts("M");
  GLCD.PrintNumber(month());
  GLCD.Puts(" ");

  if((millis() - previousMillis1) >= interval1)
  {                               //Here is where
    previousMillis1 = millis();  //the code waits for the set time before code executes again. 

    TemperatureSensorInput = analogRead(0);  //Hot Water Tank temperature sensor.
    float celsius0 = ((TemperatureSensorInput - 512) / 9.73f + 25); //Math for 10KOhm Thermistor.
    fahrenheit = celsius0 * 1.8 + 32; 

    GLCD.SelectFont(Tahoma9B);
    GLCD.CursorTo(1,1);
    GLCD.Puts("HotWaterTank: ");
    //GLCD.PrintNumber(celsius0);
    //GLCD.Puts("C/");
    GLCD.PrintNumber(fahrenheit);
    GLCD.Puts("F");
    GLCD.Puts(" ");

    Serial.print("HotWaterTank: ");
    Serial.print(celsius0);
    Serial.print(" C/");
    Serial.print(fahrenheit);
    Serial.println("F");


    TemperatureSensorInput = analogRead(1);  //Collector Tank temperature sensor.
    float celsius1 = ((TemperatureSensorInput - 512) / 11.0f + 25); //Math for 10KOhm Thermistor. 
    fahrenheit = celsius1 * 1.8 + 32;

    GLCD.CursorTo(1,2);
    GLCD.Puts("CollectorTank: ");
    //GLCD.PrintNumber(celsius1);
    //GLCD.Puts("C/");
    GLCD.PrintNumber(fahrenheit);
    GLCD.Puts("F");

    Serial.print("CollectorTank: ");
    Serial.print(celsius1);
    Serial.print(" C/");
    Serial.print(fahrenheit);
    Serial.println("F");

    TemperatureSensorInput = analogRead(2); //Solar Collector Temperature Sensor
    float celsius2 = ((TemperatureSensorInput - 512) / 11.0f + 25); //Math for 10KOhm Thermistor.
    fahrenheit = celsius2 * 1.8 + 32;

    GLCD.CursorTo(1,3);
    GLCD.Puts("SolarCollector: ");
    //GLCD.PrintNumber(celsius2);
    //GLCD.Puts("C/");
    GLCD.PrintNumber(fahrenheit);
    GLCD.Puts("F");
    GLCD.Puts(" ");

    Serial.print("SolarCollector: ");
    Serial.print(celsius2);
    Serial.print("C/");
    Serial.print(fahrenheit);
    Serial.println("F");

    TemperatureSensorInput = analogRead(3); //Enclosure temperature sensor.
    float celsius3 = ((TemperatureSensorInput - 512) / 11.0f + 25); //Math for 10KOhm Thermistor.
    fahrenheit = celsius3 * 1.8 + 32;


    GLCD.CursorTo(1,4);
    GLCD.Puts("ControlBox: ");
    //GLCD.PrintNumber(celsius3);
    //GLCD.Puts("C/");
    GLCD.PrintNumber(fahrenheit);
    GLCD.Puts("F");
    GLCD.Puts(" ");

    Serial.print("ControlBox: ");
    Serial.print(celsius3);
    Serial.print("C/");
    Serial.print(fahrenheit);
    Serial.println("F ");
    Serial.println();

// Below is Hot Water Tank pump control.
    if((millis() - previousMillis2) >= interval2)
    {                               //Here is where
      previousMillis2 = millis();  //the code waits for the set time before code executes again.  


      if ((celsius0 < 25) && (celsius1 > 27)) //If Hot Water Tank is below 25C and Collector Tank
        //is greater than 27C then turn on pump/relay1.
      {
        digitalWrite(PIN_D0, HIGH); //Relay1 HotWaterTank Pump ON
      } 
      else if ((celsius0 >= 30)|| celsius1 < 25) // 1st part: turn off 5C differential, for Hot Water Tank OR,2nd part:  
        // turn off 2C differential, for CollectorTank.
      {
        digitalWrite(PIN_D0, LOW); //Relay1 HotWaterTank Pump OFF
      }
    }
    
    // Below is Solar Collectors and Collector Tank control.
    if ((((celsius2 > 27) && (celsius2 > celsius1 + 10))|| celsius2 < -2)|| celsius2 >= 94) // If Solar Collector is at least 27C AND 
      //10C greater than Collector Tank,turn on relay2 
      //SolarCollector pump, OR freezing temperature turn on, OR extreme hot turn on.
      //System is closed loop with 50/50 glycol/water mixture.                                               
    {
      digitalWrite(PIN_D1, HIGH);  //Relay2 SollarCollector Pump ON
    }
    else if ((celsius2 <= 24)|| celsius2 < celsius1 - 5) // 1st part: low turn off differential, Solar Collectors are less than or equal to 24C turn off relay2 OR, 2nd part: normal 
      //turn off differential, Solar Collectors are 5C below Collector Tank temperature turn off relay2.
    {
      digitalWrite(PIN_D1, LOW);  //Relay SollarCollector Pump OFF
    }
  }

    // Below is Petier Cooler control.
    if ((millis() - previousMillis4) >= interval4)
    {
    previousMillis4 = millis();

    TemperatureSensorInput = analogRead(3); //Enclosure temperature sensor.
    float celsius3 = ((TemperatureSensorInput - 512) / 11.0f + 25); //Math for 10KOhm Thermistor.

    if (celsius3 > 28) // If Temperature inside of enclosure is greater than or equal to 27C turn on Peltier cooler.
    {
      digitalWrite(PIN_D2, HIGH);  // Relay3 Peltier cooler and Inside Fan ON.
      digitalWrite(PIN_D3, HIGH);  // Relay3 Peltier Outside Fan ON.
      previousMillis5 = millis(); // constantly update the timout because both fans are on
    }
    else if(celsius3 <= 27) // This is a 2 degree differential. When sensor is 2C cooler, relay will turn OFF.
    {
      digitalWrite(PIN_D2, LOW);  // Relay3 Peltier cooler and Inside Fan OFF.

      if ((millis() - previousMillis5) >= interval5) 
      {
        // Set time for Peltier Outside fan to keep running after Peltier is shut off.This
        // will help prevent hot side of Peltier from transfering heat back into inside of Enclosure.
        digitalWrite(PIN_D3, LOW);  // Relay4 Peltier Outside Fan OFF.}
      }
    }
  }
}


// To Do:
// 1. Adjust sensor temperature calculation for higher value refer to this chart at bottom of page.
//pjrc.com/teensy/tutorial4.html

// 2. 
// 3. Add a 0.1uF cap on each analog signal input to GND.
// 4. 
// 5. 
// 6. Wire LCD power & Relay board directly to power supply. 

// Notes: 
// Current operating temperatures are for easy bench testing of project. Will need to adjust
// for actual system. Including the amount of differential. And the amount of long intervals delay.

// The if statement for SollarCollector pump should cover times of Hot days with sudden cloud cover
// when hot water is NOT being used and when hot water IS being used.
// Will cover short days and long periods of cold and no sun.
// Main reason for differentials is to keep relays from quickly cycling on and off and to allow
// for energy/temperature loss when transfering heat from Collectors to Collector Tank to Hot Water Tank.
 

Attachments

  • TemperatureControlAssembly1.jpg
    TemperatureControlAssembly1.jpg
    75.3 KB · Views: 142
  • TemperatureControlAssembly2.jpg
    TemperatureControlAssembly2.jpg
    98.5 KB · Views: 147
  • TemperatureControlAssembly3.jpg
    TemperatureControlAssembly3.jpg
    100 KB · Views: 144
Last edited:
Version 4 of code

Here is my latest version of code. I changed the math code for the thermistor sensors. It dawned on me that the example for the math would only be accurate for the temperature value i set the math to do. So i did some searching and came across the Steinhart-Hart equation. Wow...what a formula. :)
Here is code....
Code:
//This version uses the Steinhart-Hart equation to calculate temperature from a varying voltage of 
//0.0 to 5.0V using a 10K thermosistor and 10k resistor.
//Previous version AnalogTemp_3Relays_4SensorsLCD3 uses a fixed math that will only give an accurate
//temperature reading for a certain defined temperature.

//Last Updated: 4/16/2015 @ 6:27PM
//This code will control 2 pumps and a Peltier Cooler with 4 analog 10k thermisitor sensors. For
//a Solar Hot Water system consisting of Solar Collectors, Solar Collector Tank and a potable
//hot water tank.
//AnalogRead(0) is sensor at HotWaterTank.
//AnalogRead(1) is sensor at SolarCollectorTank.
//AnalogRead(2) is sensor at SollarCollectors.
//AnalogRead(3) is sensor inside of electronics enclosure.
//PIN_D0 is pin D0 on Teensy++2.0 board. It controls On/Off of HotWaterTank pump relay.
//PIN_D1 is pin D1 on Teensy++2.0 board. It controls On/Off of SolarCollectors pump relay.
//PIN_D2 is pin D2 on Teensy++2.0 board. It controls On/Off of Peltier Cooler & Inside fan relay.
//PIN_D3 is pin D3 on Teensy++2.0 board. It controls On/Off of Peltier outside fan relay.

#include <math.h> 
#include <Time.h>  
#include <Wire.h>  
#include <openGLCD.h>

unsigned long interval1 =5000;  // the time we need to wait
unsigned long previousMillis1=0; // millis() returns an unsigned long.

unsigned long interval2 =3000;  // the time we need to wait
unsigned long previousMillis2=0; // millis() returns an unsigned long.

//unsigned long interval3 =1000;  // the time we need to wait
//unsigned long previousMillis3=0; // millis() returns an unsigned long.

unsigned long interval4 =7000;  // the time we need to wait
unsigned long previousMillis4=0; // millis() returns an unsigned long.

unsigned long interval5 =65000;  // the time we need to wait
unsigned long previousMillis5=0; // millis() returns an unsigned long.

void setup() {   
  delay(500); 
  GLCD.Init(NON_INVERTED);
  GLCD.ClearScreen(); 
  GLCD.ClearScreen();

  Serial.begin(38400);
  pinMode(PIN_D0, OUTPUT);
  pinMode(PIN_D1, OUTPUT);
  pinMode(PIN_D2, OUTPUT);
  pinMode(PIN_D3, OUTPUT);

}

double Thermister(int RawADC) {  //Function to perform the fancy math of the Steinhart-Hart equation.
  double Temp;
  Temp = log(((10240000/RawADC) - 10500));
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
  Temp = Temp - 273.15;   // Convert Kelvin to Celsius.
  Temp = (Temp * 9.0)/ 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius.
  return Temp;

}

void loop()                     
{
  //This is the Time and Date print out. It uses both Wire.h and Time.h files.
  GLCD.SelectFont(TimesNewRoman9);
  GLCD.CursorTo(0,0);
  GLCD.Puts("UpTime ");
  GLCD.PrintNumber(hour());
  GLCD.Puts("h");
  GLCD.PrintNumber(minute());
  GLCD.Puts("m");
  GLCD.PrintNumber(second());
  GLCD.Puts("s ");
  GLCD.Puts("D");
  GLCD.PrintNumber(day());
  GLCD.Puts("/");
  GLCD.Puts("M");
  GLCD.PrintNumber(month());
  GLCD.Puts(" ");

  if ((unsigned long )(millis() - previousMillis1) >= interval1) {
    previousMillis1 = millis(); //Here is where the code waits for the set time before code executes again. 


    int val; //Create an integer variable.
    double HotWaterTankTemp;  //Variable to hold a HotWaterTankTemp value.
    val=analogRead(0); //Read the analog port 0 and store the value in val.Hot Water Tank temperature sensor.
    HotWaterTankTemp=Thermister(val); //Runs the fancy math on the raw analog value.


    GLCD.SelectFont(Tahoma9B);
    GLCD.CursorTo(1,1);
    GLCD.Puts("HotWaterTank: ");
    GLCD.PrintNumber(HotWaterTankTemp);
    GLCD.Puts("F");
    GLCD.Puts(" ");

    Serial.print("HotWaterTank: ");
    Serial.print(HotWaterTankTemp);
    Serial.println(" F");

    double CollectorTankTemp; //Variable to hold a CollectorTankTemp value.
    val=analogRead(1); //Read the analog port 0 and store the value in val.Collector Tank temperature sensor.
    CollectorTankTemp=Thermister(val);   //Runs the fancy math on the raw analog value.

    GLCD.CursorTo(1,2);
    GLCD.Puts("CollectorTank: ");
    GLCD.PrintNumber(CollectorTankTemp);
    GLCD.Puts("F");
    GLCD.Puts(" ");

    Serial.print("CollectorTank: ");
    Serial.print(CollectorTankTemp);
    Serial.println("F");

    double SolarCollectorTemp; //Variable to hold a SolarCollectorTemp value.
    val=analogRead(2);  //Read the analog port 0 and store the value in val.Solar Collector Temperature Sensor.
    SolarCollectorTemp=Thermister(val);  //Runs the fancy math on the raw analog value.

    GLCD.CursorTo(1,3);
    GLCD.Puts("SolarCollector: ");
    GLCD.PrintNumber(SolarCollectorTemp);
    GLCD.Puts("F");
    GLCD.Puts(" ");

    Serial.print("SolarCollector: ");
    Serial.print(SolarCollectorTemp);
    Serial.println("F");

    double ControlBoxTemp; //Variable to hold a ControlBoxTemp value.
    val=analogRead(3); //Read the analog port 0 and store the value in val.Enclosure temperature sensor.
    ControlBoxTemp=Thermister(val); //Runs the fancy math on the raw analog value.

    GLCD.CursorTo(1,4);
    GLCD.Puts("ControlBox: ");
    GLCD.PrintNumber(ControlBoxTemp);
    GLCD.Puts("F");
    GLCD.Puts(" ");

    Serial.print("ControlBox: ");
    Serial.print(ControlBoxTemp);
    Serial.println("F ");
    Serial.println();

    // Below is Hot Water Tank pump control.
    if((millis() - previousMillis2) >= interval2)
    {                               //Here is where
      previousMillis2 = millis();  //the code waits for the set time before code executes again.  


      if ((HotWaterTankTemp < 111) && (CollectorTankTemp > 116)) //If Hot Water Tank is below 114*F and Collector Tank
        //is greater than 116*F then turn on pump/relay1.
      {
        digitalWrite(PIN_D0, HIGH); //Relay1 HotWaterTank Pump ON.
      } 
      else if ((HotWaterTankTemp >= 140)|| CollectorTankTemp < 113) // 1st part: turn off differential, for Hot Water Tank OR,2nd part:  
        // low temperature turn off differential, for CollectorTank.
      {
        digitalWrite(PIN_D0, LOW); //Relay1 HotWaterTank Pump OFF.
      }


      // Below is Solar Collectors and Collector Tank control.
      if ((((SolarCollectorTemp > 119) && (SolarCollectorTemp > CollectorTankTemp + 10))|| SolarCollectorTemp < 15)|| SolarCollectorTemp >= 200) // If Solar Collector is at greater than 119*F AND 
        //10*F greater than Collector Tank,turn on relay2 SolarCollector pump,
        // OR 15*F freezing temperature turn on, OR extreme hot 200*F turn on.
        //System is closed loop with 50/50 glycol/water mixture.                                               
      {
        digitalWrite(PIN_D1, HIGH);  //Relay2 SollarCollector Pump ON.
      }
      else if ((SolarCollectorTemp <= 109)|| SolarCollectorTemp < CollectorTankTemp - 5) // 1st part: low operating temperature turn off differential, Solar Collectors are less than or equal to 109*F turn off relay2 OR, 
         //2nd part: normal operating temperature turn off differential, Solar Collectors are 5*F below Collector Tank temperature, turn off relay2.
      {
        digitalWrite(PIN_D1, LOW);  //Relay SollarCollector Pump OFF.
      }


      // Below is Petier Cooler control.
      if ((millis() - previousMillis4) >= interval4)
      {
        previousMillis4 = millis();


        if (ControlBoxTemp > 95) // If Temperature inside of enclosure is greater than or equal to 95*F turn on Peltier cooler.
        {
          digitalWrite(PIN_D2, HIGH); // Relay3 Peltier cooler and Inside Fan ON.
          digitalWrite(PIN_D3, HIGH); // Relay4 Peltier Outside Fan ON.
          previousMillis5 = millis(); // Constantly update the timout because both fans are on.
        }
        else if(ControlBoxTemp <= 91) // This is a 4*F differential. When sensor is 4*F cooler, relay will turn OFF.
        {
          digitalWrite(PIN_D2, LOW);  // Relay3 Peltier cooler and Inside Fan OFF.

          if ((millis() - previousMillis5) >= interval5) 
          {
            // Set time for Peltier Outside fan to keep running after Peltier is shut off.This
            // will help prevent hot side of Peltier from transfering heat back into inside of Enclosure.
            digitalWrite(PIN_D3, LOW);  // Relay4 Peltier Outside Fan OFF.}
          }
        }
      }
    }
  }
}




// To Do:
// 

// Notes: 

// The if statement for SollarCollector pump should cover times of Hot days with sudden cloud cover
// when hot water is NOT being used and when hot water IS being used.
// Will cover short days and long periods of cold and no sun.
// Main reason for differentials is to keep relays from quickly cycling on and off and to allow
// for energy/temperature loss when transfering heat from Collectors to Collector Tank to Hot Water Tank.

I also got everything in the small enclosure. Will post pics of it soon.

Troy
 
Ok, here is some pictures of everything in the box.
 

Attachments

  • Enclosure1.jpg
    Enclosure1.jpg
    115.5 KB · Views: 160
  • Enclosure2.jpg
    Enclosure2.jpg
    94.5 KB · Views: 141
  • Enclosure3.jpg
    Enclosure3.jpg
    150.6 KB · Views: 135
Nice work on the physical assembly part there. I like how you have carefully minimized the wires traveling across the hinge, and then routed them across with plenty of flex. Would suggest actually positively anchoring the cable on both halves of the shell though, since any solder join that is asked to handle mechanical flexing will eventually fail, though if it's working at this point maybe leave that for the MkII version.
 
I think the next one will be in a larger enclosure. :) This project almost outgrew it quickly.
Thanks again for your help.
 
Status
Not open for further replies.
Back
Top