Julian Rogers Home Electronic thermostat

Under Construction

Cooling fan software


// fan_stat3_car

// 9.7.18

// Prog for Trinket and tmp102 as stat for car bottom hose fan sensor

// Temp range low as bottom hose: 40 - 80 degC

// Ambition is to include rate of change function to smooth operation (not yet)

// Hysteresis and delay in the loop are factors to be considered

// Probably not enough time in the Universe for testing to determine optimum values!


#include <TinyWireM.h>

#define TMP102_I2C_ADDRESS1 0x48 // I2C address TMP102 A0 to GND (0x48 = 72 = 1001000 for GND, 73 for vcc)

#define micPin  1                // connected to Trinket LED

#define micPin2  3               // output to mic4022


int potPin = 2;                  // aka pin4 (for digital)!

int potVal;

int waterTemp;

int setTemp;  

int hysteresis = 1;             // hysteresis initiall tested at 5 - too much?


//////////////////////////////////////////////////////////////////


void setup() {

 TinyWireM.begin();

 pinMode(micPin, OUTPUT);

 pinMode(micPin2, OUTPUT);

 digitalWrite(micPin, LOW);

 digitalWrite(micPin2, LOW);


}


/////////////////////////////////////////////////////////////


// function to read temperature

int getTemp102(byte tmp102Add){

  byte firstbyte, secondbyte; //these are the bytes we read from the TMP102 temperature registers

  int val; /* an int is capable of storing two bytes, this is where we "chuck" the two bytes together. */


  float convertedtemp; /* We then need to multiply our two bytes by a scaling factor, mentioned in the datasheet. */


  //float correctedtemp;

  // The sensor overreads? I don't think it does!



  /* Reset the register pointer (by default it is ready to read temperatures)

   You can alter it to a writeable register and alter some of the configuration -

   the sensor is capable of alerting you if the temperature is above or below a specified threshold. */


  TinyWireM.beginTransmission(tmp102Add); // start talking to sensor

  TinyWireM.send(0x00);

  TinyWireM.endTransmission();

  TinyWireM.requestFrom(tmp102Add, 2);

  TinyWireM.endTransmission();

  

  firstbyte      = (TinyWireM.receive());

  /*read the TMP102 datasheet - here we read one byte from

   each of the temperature registers on the TMP102*/

  secondbyte     = (TinyWireM.receive());

  /*The first byte contains the most significant bits, and

   the second the less significant */

  val = firstbyte;

  if ((firstbyte & 0x80) > 0) {

    val |= 0x0F00;

  }

  val <<= 4;

  /* MSB */

  val |= (secondbyte >> 4);    

  // LSB is ORed into the second 4 bits of our byte.

   

  convertedtemp = val*0.625; // temp x 10


  int temp = (int)convertedtemp;

  return temp;

}


//////////////////////////////////////////////////////////////


void loop() {

  potVal = analogRead(potPin);

  setTemp = map(potVal, 0, 1023, 40, 80);

  setTemp = setTemp * 10;

  waterTemp = getTemp102(TMP102_I2C_ADDRESS1);


  if(waterTemp >= (setTemp + hysteresis)){

   digitalWrite(micPin, HIGH);

   digitalWrite(micPin2, HIGH);

  }


  if(waterTemp <= (setTemp - hysteresis)){

    digitalWrite(micPin, LOW);

    digitalWrite(micPin2, LOW);

  }


  delay(1000);



}

/////////////////////////////////////////////////////////////


This is a pretty straightforward program for an electronic thermostat. Future versions could involve more code to prevent the temperature undershooting then overshooting too much. Initial testing suggests this will not be necessary.