Doubt HIGH and LOW status

Hi... I don't have much knowledge in C and Arduino. I'm more of a curious hobbyist. I like to assemble things.

I set up a system to control the temperature of a refrigerator. Inside it has a small heater. All controlled by two relays.

Every time I start the system, both the fridge and the heater turn on. How to make them not call? My guess is to put some instruction in the void setup but since I'm not sure if it's the right place, I thought I'd ask.

Another question... In several ifs, to turn off the fridge and heater, I use the following instruction:

digitalWrite(geladeira, LOW);
digitalWrite(aquecedor, LOW);

It works on some instructions.

But in one of them, it only turns off the fridge and the heater if I do the opposite

digitalWrite(geladeira, HIGH);
digitalWrite(aquecedor, HIGH);

Why does it happen?

The complete sketch:

/************************************************************************************

 Controle da Fermentação
  
************************************************************************************/

#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_DEVICE_NAME "Controle Fermentador"
#define BLYNK_AUTH_TOKEN "xxx"
#define BLYNK_PRINT Serial

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

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "xxx";
char pass[] = "xxx";

#define geladeira D0
#define aquecedor D1
#define ds18b20 D2

BlynkTimer timer;

int estadoGelad = 0;
int estadoAquec = 0;
int desligaGelad = 0;
int desligaAquec = 0;
int tempo = 0;
int tempAlvo;
float histerese;
float refFrio;
float refQuente;
float tempC;
int tempoLiga;
int tempoLigaMin;
char setIntervCont;
int geladLigada = 0;
int aquecLigado = 0;

 
// Objeto que tratará da troca de dados com o sensor DS18B20
OneWire oneWire(ds18b20);
DallasTemperature sensors(&oneWire);

//Atualiza os pinos virtuais com os dados salvos no servidor Blynk. Importante em caso de quedas de energia/conexão
BLYNK_CONNECTED() {
  Blynk.syncVirtual(V2, V3, V9);
}

//Recebe comando do Blynk para ligar ou desligar a geladeira manualmente
BLYNK_WRITE(V0)
{
  estadoGelad = param.asInt(); //Recebe 1 para ligar ou 0 para desligar
  
  if (estadoGelad == 1){
    digitalWrite(geladeira, HIGH); // Envia comando para relê ligar a geladeira
    digitalWrite(aquecedor, LOW); // Envia comando para relê desligar o aquecedor
    Blynk.virtualWrite(V1, LOW); // Envia comando para o Blynk desligar o botão do aquecedor manual, se estiver ligado
    Serial.printf("Geladeira ligada manualmente!");
  }
  
  if (estadoGelad == 0)  {
    digitalWrite(geladeira, LOW); // Envia comando para relê desligar a geladeira
    Serial.printf("Geladeira desligada manualmente!");
   }
}

//Recebe comando do Blynk para ligar ou desligar o aquecedor manualmente
BLYNK_WRITE(V1)
{
  estadoAquec = param.asInt(); //Recebe 1 para ligar ou 0 para desligar
  
  if (estadoAquec == 1){
    digitalWrite(aquecedor, HIGH); // Envia comando para relê ligar o aquecedor
    digitalWrite(geladeira, LOW); // Envia comando para relê desligar a geladeira
    Blynk.virtualWrite(V0, LOW); // Envia comando para o Blynk desligar o botão da geladeira manual, se estiver ligado
    Serial.printf("Aquecedor ligado manualmente!");
  }
  
  if (estadoAquec == 0)  {
    digitalWrite(aquecedor, LOW); // Envia comando para relê desligar a geladeira
    Serial.printf("Aquecedor desligado manualmente!");    
  }
}

BLYNK_WRITE(V2) // Parâmetro de temperatura alvo
  {
    tempAlvo = param.asInt();
  }

BLYNK_WRITE(V3) // Parâmetro de histerese
  {
    histerese = param.asFloat();
  }

//Se clicar no botão V5, que é só pra mostrar se a geladeira está ligada ou não, desliga novamente o botão, ou seja, não serve pra ligar a geladeira
BLYNK_WRITE(V5)
  {
    Blynk.virtualWrite(V5, LOW);
  }

//Se clicar no botao V5, que é só pra mostrar se o aquecedor está ligado ou não, desliga novamente o botao, ou seja, não serve pra ligar o aquecedor  
BLYNK_WRITE(V6)
  {
    Blynk.virtualWrite(V6, LOW);
  } 

BLYNK_WRITE(V9)
  {
    tempoLiga = param.asInt();
    tempoLigaMin = tempoLiga * 60000;
    setIntervCont = tempoLigaMin + 'L';
  }

void temperatura()
  {
    sensors.requestTemperatures();
    tempC = sensors.getTempCByIndex(0);
    Blynk.virtualWrite(V4, tempC);  //Envia temperatura (DS18B20) para o Blynk
    refFrio = tempAlvo - histerese; // Define temperatura alvo mais histerese para frio
    refQuente = tempAlvo + histerese; // Define temperatura alvo mais histerese para quente
    Blynk.virtualWrite(V7, refFrio);  //Envia refencia para o Blynk
    Blynk.virtualWrite(V8, refQuente);  //Envia referencia para o Blynk
  }

void tempoPLiga() // Quando a geladeira ligar, vai mudar tempo para 1. Esse void Vai rodar a cada setInterv minutos para voltar para 0. O void controle é rodado pelo tempo = 0
  {
    if (tempo == 1){
      tempo = 0;
    }
  }  

void controle()
  {
    if (tempo == 0){

    if (tempC > refQuente) { // Se a temperatura estiver maior que refQuente, liga a geladeira e desliga aquecedor, se estiver ligado
      digitalWrite(geladeira, HIGH);
      digitalWrite(aquecedor, LOW);
      Blynk.virtualWrite(V5, HIGH);
      Blynk.virtualWrite(V6, LOW);
      Serial.println("Geladeira Ligada");
      Serial.print("Temperatura atual: ");
      Serial.println(tempC);
      Serial.print("Temperatura referência: ");
      Serial.println(refQuente);
      tempo = 1;
      geladLigada = 1;
  } 
    else if (tempC < refFrio) 
  { // Se a temperatura estiver menor que refFrio, liga o aquecedor e desliga geladeira, se estiver ligada
      digitalWrite(geladeira, LOW);
      digitalWrite(aquecedor, HIGH);
      Blynk.virtualWrite(V5, LOW);
      Blynk.virtualWrite(V6, HIGH);
      Serial.println("Aquecedor Ligado");
      Serial.print("Temperatura atual: ");
      Serial.println(tempC);
      Serial.print("Temperatura referência: ");
      Serial.println(refFrio);
      tempo = 1;
      aquecLigado = 1;
  }
  }
  }

void noAlvo() //Desliga geladeira e aquecedor se temperatura dentro do alvo + histerese
  {
    if ((geladLigada == 0) && (aquecLigado == 0)){
    if ((tempC < refQuente) && (tempC > refFrio)){
      digitalWrite(geladeira, HIGH);
      Blynk.virtualWrite(V5, LOW);
      digitalWrite(aquecedor, HIGH);
      Blynk.virtualWrite(V6, LOW);
      Serial.println("Confirma geladeira e aquecedor desligados!");
  }
  }
  }
  
void controleLigados() //Desliga geladeira e aquecedor quando ligados por ultrapassar faixa e voltarem ao alvo
  {
    if (geladLigada == 1){
    if (tempC < tempAlvo){
      digitalWrite(geladeira, LOW);
      Blynk.virtualWrite(V5, LOW);
      geladLigada = 0;
      Serial.print("Geladeira estava ligada... desligada! Temperatura: ");
      Serial.println(tempC);
      }     
    }
    if (aquecLigado == 1){
    if (tempC > tempAlvo){
      digitalWrite(aquecedor, LOW);
      Blynk.virtualWrite(V6, LOW);
      aquecLigado = 0;
      Serial.print("Aquecedor estava ligado... desligado! Temperatura: ");
      Serial.println(tempC);
     }      
    }
  }
  
void setup()
  {
    pinMode(geladeira,OUTPUT);
    pinMode(aquecedor,OUTPUT);
    Serial.begin(9600);
    Blynk.begin(auth, ssid, pass);
    timer.setInterval(1000L, temperatura);
    timer.setInterval(1000L, controle); //60000L
    timer.setInterval(1000L, noAlvo);
    timer.setInterval(1000L,controleLigados); //60000L
    timer.setInterval(1000L, tempoPLiga); //60000L
  }

void loop()
  {
    Blynk.run();
    timer.run(); 
  }

Use digitalWrite() to set the required initial states immediately after the pinMode() commands in setup()

As to your other problem, if you set an output pin to HIGH or LOW then the relay attached to that pin should behave consistently as long as there is enough current to operate the relay

That brings us to the question of how your project, and in particular the relays, is powered

Please post a schematic of your circuit. A picture of a hand drawn circuit is good enough as long as all of the connections are shown clearly

Because almost all relay modules sold as being "Arduino Compatible" are "Active LOW". The relay coil is energized when the input is LOW.

Is it in only one of them or all of them ?

Isso que eu imaginei....

Entendi a tua questão. A ligação é bem simples mas entendi que meu erro é estar alimentando o relê e o esp com a mesma fonte. Vou comprar uma fonte separada para os relês

Do you want this topic moved to the Portuguese section of the forum ?

Eu coloquei essa questão lá mas um dia e ninguém tinha respondido. Pode ser sim, se você achar melhor. Até porque aqui tem a solução!!

Você tinha razão!!!! Era isso mesmo!!!! Como eu coloquei duas instruções, ligando um e desligado o outro, não percebi que o LOW ligava e o HIGH desligava!!!!! Por isso, quando coloquei instrução LOW nos dois relês, ambos ligavam e eu não estava entendendo!!!!

Obrigado!!!