Getting relay to work

Well, after MUCH reading and trying to find the answers myself, I'm still stumped. This is my first project outside of the simple ones that come with the starter kit.

I have 2 DS18B20 waterproof sensors reading two different temperatures in my wood kiln. Wet Bulb and Dry Bulb. I have a relay wired to an outlet I am trying to control based on one of the temperature values. When i manually feed 5v to the switch pin the relay works. I can't get it to work thru the code though. Even when I set the value of it to HIGH it does not come on BUT the red LED lights up faintly. (digitalWrite(heaterpin, HIGH)) I have my temperature settings at 60 for now in the sketch just to test manually. Any pointers on where to look, what to read would be greatly appreciated. Thank you.

#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 backLight = 13; // pin 13 will control the backlight

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

// 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.
// See the tutorial on how to obtain these addresses:
// Arduino 1-Wire Address Finder

DeviceAddress insideThermometer = { 0x28, 0x38, 0xA3, 0x7D, 0x08, 0x00, 0x00, 0x3F };
DeviceAddress outsideThermometer = { 0x28, 0x0F, 0xC6, 0x7D, 0x08, 0x00, 0x00, 0x94 };

void setup(void)
{
// Start up the library
sensors.begin();
// set the resolution to 9 bit
sensors.setResolution(insideThermometer, 9);
sensors.setResolution(outsideThermometer, 9);
pinMode(backLight, OUTPUT);
digitalWrite(backLight, LOW); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear(); // start with a blank screen
}

void printTemperature(DeviceAddress deviceAddress)
{
int tempC = sensors.getTempC(deviceAddress);
int heaterpin = 6;

digitalWrite(heaterpin, LOW);
if (tempC == -127.00) {
lcd.print("Error");
} else {
//lcd.print(tempC);
//lcd.print("/");
lcd.print(DallasTemperature::toFahrenheit(tempC));
if (insideThermometer > 60) digitalWrite(heaterpin, LOW);
if (insideThermometer < 60) digitalWrite(heaterpin, HIGH);
}
}
void loop(void)
{
delay(2000);
sensors.requestTemperatures();
lcd.setCursor(0,0);
lcd.print("DryB: ");
printTemperature(insideThermometer);
lcd.setCursor(0,1);
lcd.print("WetB: ");
printTemperature(outsideThermometer);
//if (insideThermometer > 60) digitalWrite(heaterpin, LOW);
//if (insideThermometer < 60) digitalWrite(heaterpin, HIGH);
}

Where do you do a pinMode() call to setup heaterpin as an output?

Are you actually reading the temperatures in your code ?

BTW, when you do get it working, consider adding some "hysteresis" to your code. This can help prevent the relay chattering as can happen when the temperature is very near the switchpoint. So, for example, instead of:

if (insideThermometer > 60) digitalWrite(heaterpin, LOW);
if (insideThermometer < 60) digitalWrite(heaterpin, HIGH);

consider

if (insideThermometer > 62) 
    digitalWrite(heaterpin, LOW);
if (insideThermometer < 58) 
    digitalWrite(heaterpin, HIGH);

You can narrow the deadzone a bit to suit but shouldn't use the same temperature for both turn-on and turn-off points.

I added the PinMode - Thanks

void printTemperature(DeviceAddress deviceAddress)
{
int tempC = sensors.getTempC(deviceAddress);
int heaterpin = 6;

pinMode(6, OUTPUT);
digitalWrite(heaterpin, LOW);
if (tempC == -127.00) {
lcd.print("Error");

It is reading the temp and it is displaying on the LCD.
As far as "hysteresis" - yes, I already had that in mind but again, just trying to get it to switch on and off.

It compiled with the addition of Pinmode but nothing has changed. Thank you

BadgerStateSawyer:
I added the PinMode - Thanks
...

It is reading the temp and it is displaying on the LCD.
As far as "hysteresis" - yes, I already had that in mind but again, just trying to get it to switch on and off.

It compiled with the addition of Pinmode but nothing has changed. Thank you

Looking more closely at your original code, you have:

void printTemperature(DeviceAddress deviceAddress)
{
    int tempC = sensors.getTempC(deviceAddress);
    int heaterpin = 6;

    digitalWrite(heaterpin, LOW);
    if (tempC == -127.00) 
    {
        lcd.print("Error");
    } 
    else 
    {
        //lcd.print(tempC);
        //lcd.print("/");
        lcd.print(DallasTemperature::toFahrenheit(tempC));
        if (insideThermometer > 60) 
            digitalWrite(heaterpin, LOW);
        if (insideThermometer < 60) 
            digitalWrite(heaterpin, HIGH);
    }
}

Why are you comparing "insideThermometer" against a temperature value? That is your DeviceAddress...

Perhaps you want to compare against tempC when the device being checked is the insideThermometer?

It is reading the temp and it is displaying on the LCD.

Yes, but does the code ever update the value of insideThermometer and outsideThermometer ?

Blackfin, I see what you mean. I have both sensors sharing 1 pin, so I would need to assign a variable to each reading, or a command in the IF statement that would reference the value.

This line sends the value I want to reference to the LCD
printTemperature(insideThermometer)

so how can I capture or reference the Temperature(insideThermometer)

Thank you.

Just whipped this up based on your code. Stuff is missing (variable decs, setup() etc) but it might give you a different perspective on how to do what you're trying.

.
.
.

int
    nOutsideTemp,
    nInsideTemp;
uint8_t
    bFlags;

#define IN_ERROR    0b00000001
#define OUT_ERROR   0b00000010
#define TARGET_TEMP 60
#define HYST_UP     2
#define HYST_DOWN   2
.
.
.

void GetTemps( void )
{
    bFlags = 0x00;
    sensors.requestTemperatures();
    nOutsideTemp = sensors.getTempC(outsideThermometer);
    if( nOutsideTemp == -127 )
        bFlags |= IN_ERROR;
    nInsideTemp = sensors.getTempC(insideThermometer);
        bFlags |= OUT_ERROR;
    
}//GetTemps


void printTemperature( void )
{     
    lcd.setCursor(0,0);
    lcd.print("DryB: ");
    lcd.print( (bFlags & IN_ERROR)?"ERR":nInsideTemp );
    lcd.setCursor(0,1);
    lcd.print("WetB: ");
    lcd.print( (bFlags & OUT_ERROR)?"ERR":nOutsideTemp );
           
}//printTemperature

void ServiceHeaterRelay( void )
{
    if( bFlags )
    {
        digitalWrite(heaterpin, LOW);
        return;
    }//if

    if( nInsideTemp > TARGET_TEMP+HYST_UP )
        digitalWrite(heaterpin, LOW);
    if( nInsideTemp < TARGET_TEMP-HYST_DOWN )
        digitalWrite(heaterpin, HIGH);
    
}//ServiceHeaterRelay

void loop( void )
{
    GetTemps();
    printTemperature();
    ServiceHeaterRelay();

    delay(2000);

    
}//loop