Thermostat relay output with Dallas temp sensor ds18b20 sensor, arduino,

Hello,

I have some code here that works great. I am trying to figure out how to make it more efficient though, eventually i would like to be able to shoot the sensor data over to labview, then have that communicate back and turn the relays on and off but i feel like the way we went about this code is not going to be a good foundation for that.

This is to run thermal test on electronics i have in an enclosed aluminum box, that has an electric AC 120v heater heating and Thermal electric coolers for cooling. There are 6 temp sensors, i am looking at the one called IAT and doing the thermostat switching off of that. There is no hysteresis algorithm.

The heater comes on under 50°C, and the cooling comes on over 55°C.

The arduino is outputting to 5 volt relays, which then drive 12 volt rails for my AC relays.

Dallas onewire.h library was used as well as example code found on arduino.cc to pull the serial numbers from the temp sensors (when using more than one you need to do this).

any input is appreciated!

#include "OneWire.h"
#include <avr/pgmspace.h>

//last update august 5th 2013 JSA
// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire  ds(3);  // on pin 10 (a 4.7K resistor is necessary)
const int ledPin12 = 12;       // pin that the LED is attached to
const int ledPin11 = 11; 
const byte array1[] PROGMEM = {1,2,3,4,5};
const byte array2[] PROGMEM = {6,7,8,9,10};

const byte RearAmbient[] PROGMEM = { 0x28, 0xBD, 0x55, 0x4B, 0x04, 0x00, 0x00, 0xFE };
const byte FrontAmbient[] PROGMEM = { 0x28, 0x11, 0x75, 0x4B, 0x04, 0x00, 0x00, 0x27 };
const byte IAT[] PROGMEM = { 0x28, 0x94, 0x2E, 0x4B, 0x04, 0x00, 0x00, 0xC3 };
const byte EGT[] PROGMEM = { 0x28, 0x71, 0x60, 0x4B, 0x04, 0x00, 0x00, 0x4A };
const byte RearProc[] PROGMEM = { 0x28, 0x13, 0x6C, 0x4B, 0x04, 0x00, 0x00, 0xC6 };
const byte RAM[] PROGMEM = { 0x28, 0x39, 0x30, 0x4B, 0x04, 0x00, 0x00, 0x0B };

const byte ben_temp1[] PROGMEM = { 0x28, 0xB8, 0x31, 0x4B, 0x04, 0x00, 0x00, 0x1B};
const byte ben_temp2[] PROGMEM = { 0x28, 0x35, 0x6E, 0x4B, 0x04, 0x00, 0x00, 0x41};


void setup(void) {
  Serial.begin(9600);
  pinMode(ledPin12, OUTPUT);
  pinMode(ledPin11, OUTPUT);
}

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr)) 
  {
   // Serial.println("No more addresses.");
  //  Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  
/*  Serial.print("ROM =");
  for( i = 0; i < 8; i++) 
  {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) 
  {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();
 */
  // the first ROM byte indicates which chip
 
 /* switch (addr[0]) 
  {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 
 */

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

 // Serial.print("  Data = ");
 // Serial.print(present, HEX);
//  Serial.print(" ");
  for ( i = 0; i < 9; i++) 
  {           // we need 9 bytes
    data[i] = ds.read();
   // Serial.print(data[i], HEX);
    //Serial.print(" ");
  }
 // Serial.print(" CRC=");
 // Serial.print(OneWire::crc8(data, 8), HEX);
 // Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  
if(0 == (memcmp_P(addr, IAT, 8))) 
  {
        if (24 > celsius) 
     {
      digitalWrite(ledPin12, HIGH);
      digitalWrite(ledPin11, LOW);
      Serial.println("IAT");
     }
     
    else if (26 < celsius)
     {
      digitalWrite(ledPin11, HIGH);
      digitalWrite(ledPin12, LOW);
      Serial.println("IAT");
     }
      else
     {
      digitalWrite(ledPin11, LOW);
      digitalWrite(ledPin12, LOW);
      Serial.println("IAT");
     }

  }
  
   else if(0 == (memcmp_P(addr, FrontAmbient, 8))) 
  {
    Serial.println("Front Ambient");
  }
   else if(0 == (memcmp_P(addr, EGT, 8))) 
  {
    Serial.println("EGT          ");
  }
   else if(0 == (memcmp_P(addr, RearAmbient, 8))) 
  {
    Serial.println("Rear Ambient");
  }
     else if(0 == (memcmp_P(addr, RearProc, 8))) 
  {
    Serial.println("Rear Proc    ");
  }
   else if(0 == (memcmp_P(addr, RAM, 8))) 
  {
    Serial.println("RAM          ");
  }
  
  Serial.print(celsius);
  Serial.print(" C");
    Serial.print("\n\r");
//  Serial.print(fahrenheit);
 // Serial.println(" Fahrenheit");
}

Have you considered using the PID library?

http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/

ok this code seems to work a lot better, modified from the doghouse temp example online. i think before i was having problems with this code when temps went negative, but i cant recall why i didnt want to use it. im using it now and it works good.

I havent added PID yet pwillard, but good idea i am going to try that next.

// This Arduino sketch reads DS18B20 "1-Wire" digital
// temperature sensors.
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

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

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

// 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);


OneWire  ds(3);  // on pin 10 (a 4.7K resistor is necessary)
const int ledPin9 = 9;       // pin that the LED is attached to
const int ledPin8 = 8; 


// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress RearAmbient = { 
  0x28, 0xBD, 0x55, 0x4B, 0x04, 0x00, 0x00, 0xFE };
DeviceAddress FrontAmbient = { 
  0x28, 0x11, 0x75, 0x4B, 0x04, 0x00, 0x00, 0x27 };
DeviceAddress IAT = { 
  0x28, 0x94, 0x2E, 0x4B, 0x04, 0x00, 0x00, 0xC3 };
DeviceAddress EGT = { 
  0x28, 0x71, 0x60, 0x4B, 0x04, 0x00, 0x00, 0x4A };
DeviceAddress RearProc = { 
  0x28, 0x13, 0x6C, 0x4B, 0x04, 0x00, 0x00, 0xC6 };
DeviceAddress RAM = { 
  0x28, 0x39, 0x30, 0x4B, 0x04, 0x00, 0x00, 0x0B };
void setup(void)
{

  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin8, OUTPUT);
  // start serial port
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(RearAmbient, 10);
  sensors.setResolution(FrontAmbient, 10);
  sensors.setResolution(IAT, 10);
  sensors.setResolution(EGT, 10);
  sensors.setResolution(RearProc, 10);
  sensors.setResolution(RAM, 10);

}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } 
  else {

    Serial.print(tempC);
    Serial.print(" C:");
    // Serial.print(" F: ");
    // Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop(void)
{ 




  delay(1000);
  // Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();

  float tempIAT = sensors.getTempC(IAT);
  // Serial.print("test:     ");
  // Serial.print(tempIAT);
  if (24 > tempIAT) 
  {
    digitalWrite(ledPin9, HIGH);
    digitalWrite(ledPin8, LOW);
    Serial.println("test 1");
  }

  else if (26 < tempIAT)
  {
    digitalWrite(ledPin8, HIGH);
    digitalWrite(ledPin9, LOW);
    Serial.println("test 2");
  }
  else
  {
    digitalWrite(ledPin8, LOW);
    digitalWrite(ledPin9, LOW);
    Serial.println("test 3");
  }  




  Serial.print("Rear Amb:     ");
  printTemperature(RearAmbient);
  Serial.print("\n\r");
  Serial.print("Front Amb:    ");
  printTemperature(FrontAmbient);
  Serial.print("\n\r");
  Serial.print("IAT:          ");
  printTemperature(IAT);
  Serial.print("\n\r");
  Serial.print("EGT:          ");
  printTemperature(EGT);
  Serial.print("\n\r");
  Serial.print("Rear Proc:    ");
  printTemperature(RearProc);
  Serial.print("\n\r");
  Serial.print("RAM:          ");
  printTemperature(RAM);
  Serial.print("\n\r\n\r");

}