Problems with outputs getting HIGH (or at least they act like that)

Hi,

I have made a simple controller for my outdoor hot tub that I have been running for a few years without problem. Now I have changed some components and starting to build a new program to get rid of some code that was not used and also need to change the functions. I have just started so there is not much in the code so far.

The arduino is connected to a relay card with 8 relays on it. Pin 2,3 4 is for activating 3 different relays. The relays is used for controlling 3 different contactors.

The problem is, with the code below, that the 3 pins (2,3 and 4) is turning HIGH when the program starts. The 3 used relays (and contactors) is activated. If i put digitalWrite(heater, LOW); in setup or loop there is no difference.

If I put

digitalWrite(heater, LOW);
delay(1000);
digitalWrite(heater, HIGH);
delay(1000);

inside the loop it kinda works but still circulation and drain is HIGH and heater switches between HIGH and LOW.

I don´t get it. Please teach me if this is normal or help me to solve it if not :slight_smile:

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

const int heater = 2;
const int circulation = 3;
const int drain = 4;

int heaterState = 0;
int circulationState = 0;
int drainState = 0;



//Temperatursensorer
#define ONE_WIRE_BUS 10
#define TEMPERATURE_PRECISION 9
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


DeviceAddress lufttemp = { 0x28, 0xFF, 0x62, 0x89, 0x53, 0x15, 0x01, 0x42 };

void setup()
{
  pinMode(heater, OUTPUT);          // sets the digital pin 13 as output
  pinMode(circulation, OUTPUT);
  pinMode(drain, OUTPUT);

  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(lufttemp, 10);
}

void printTemperature(DeviceAddress deviceAddress)
{
 float tempC = sensors.getTempC(deviceAddress);
 Serial.print("Temp C: ");
 Serial.print(tempC);
 Serial.print(" Temp F: ");
 Serial.print(DallasTemperature::toFahrenheit(tempC));
}

void loop()
{

sensors.requestTemperatures();
Serial.print("Lufttemperaturen ar: ");
printTemperature(lufttemp);
Serial.print("\n\r");
  
float lufttemperatur;
lufttemperatur = sensors.getTempC(lufttemp);



}

Please post a wiring diagram and a link to the relay module. Pins default to INPUT on restart, so they are not presenting a HIGH output level, unless you have pullup resistors on them.

Try adding a line in setup() to set the output low:

  pinMode(heater, OUTPUT);          // sets the digital pin 13 as output
  digitalWrite(heater, LOW);

Why the comment about pin 13?

If your relay card is the type that energizes the relay on a LOW input, try this in setup:

byte q = 2;
for(q = 2;q <= 4; q++)
{
  digitalWrite(q,HIGH);
}
for(q = 2;q <= 4; q++)
{
  pinMode(q,OUTPUT);
}

Delta_G:
Do you mean that the pins are HIGH or that the relays are on. Lots of relay boards turn the relay on with a LOW and off with a HIGH.

Hi,

Pins seems to be HIGH. If I disconnect one of the 3 pins the relay is deactivated.

The relay board has both NC and NO connections and I use NC. So when triggered it should open/turn relays on.

jremington:
Please post a wiring diagram and a link to the relay module. Pins default to INPUT on restart, so they are not presenting a HIGH output level, unless you have pullup resistors on them.

Try adding a line in setup() to set the output low:

  pinMode(heater, OUTPUT);          // sets the digital pin 13 as output

digitalWrite(heater, LOW);



Why the comment about pin 13?

It was just standard code from an example. Forgot to remove it.

There is no difference if I add the digitalWrite in setup. Still all 3 pins are HIGH.

If I put // before all three pinMode the pins stay as Inputs and all relays are off. As soon as I remove the comment, // on one of the pinMode the pin get HIGH and relay is activated.

Ill try to make a wiring diagram after work.

This is the same kind of relay board:

You have the relay contacts wired backward, I usually connect power to NO and load to COM, post your current code, we will help fix it.

mlindeloef:
Pins seems to be HIGH. If I disconnect one of the 3 pins the relay is deactivated.

Relay inputs MUST BE HIGH for the relays to be OFF.
This is normal behaviour. It's called "active LOW".

To stop the relays briefly activating during bootup, you must write a HIGH to the pin BEFORE you set the pin to OUTPUT with pinMode.

digitalWrite(heater, HIGH);
pinMode(heater, OUTPUT);

Same for the other relay pins.

Post#3 shows you how to do that with a 'for' loop.

Leo..

Did you see this: :slight_smile:
Screenshot from 2018-06-15 01-32-37.png

I did...

outsider:
Did you see this: :slight_smile:
Screenshot from 2018-06-15 01-32-37.png

I had no instructions with my board. And I googeled and found the link with this board that seems to be the same kind.

Then I wil just change the cables on the board to NO instead of NC and problem is solved without code. Then when the pins are LOW. The relays will be ON but the contactors will be OFF.

Thanks for fast response and lot of knowledge.

Why that.
Relays draw ~75mA all the time, and that might give overheating problems if they are on 24/7.
They should be off most of the time, and only active if the pump/heater is ON.
Coding active LOW is just as easy as active HIGH.

if(tempIsTooLow)
{
 digitalWrite(relay1Pin, LOW); // heater on
}
else
{
 digitalWrite(relay1Pin, HIGH); // heater off
}

Leo..