Relay on after start

Hi all,

I am doing a small project with an esp32 and a two relay module.
The module must be able to activate / deactivate both from Blynk and from two push buttons.
Everything works fine, except that the relays are activated at startup and at startup, they should be turned off.
I have tried many things, but I do not know why when starting the relays they start on.
Except for this, the operation of the buttons and the blynk app are correct.
What do I do to make the app start with the relay turned off?

I pass you my code ā€¦

#define BLYNK_PRINT Serial
 
 
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
 
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "4b5tvAXXXXXXXXXXfOf";
 
// Your WiFi credentials.
// Set password to "" for open networks.
 
char ssid[] = "proeus";
char pass[] = "7654321";
 
 
// Set your LED and physical button pins here
const int relePin1 = 14;
const int relePin2 = 12;
 
const int btnPin1 = 4;
const int btnPin2 = 5;
 
BlynkTimer timer;
void checkPhysicalButton();
 
int releState1 = LOW;
int releState2 = LOW;
int btnState1 = LOW;
int btnState2 = LOW;
 
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
 
  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, releState1);
}
 
// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
//  releState1 = param.asInt();
  subir ();   
 
}
BLYNK_WRITE(V3) {
// releState2 = param.asInt();
  bajar();
 
}
 
void checkPhysicalButton()
{
  if (digitalRead(btnPin1) == LOW) {
    // btnState1 is used to avoid sequential toggles
    if (btnState1 != LOW) {
 
      // Toggle LED state
      //releState1 = !releState1;
      subir();
      // Update Button Widget
      Blynk.virtualWrite(V2, LOW);
    }
    btnState1 = LOW;
  } else {
    btnState1 = HIGH;
  }
 
  if (digitalRead(btnPin2) == LOW) {
    // btnState2 is used to avoid sequential toggles
    if (btnState2 != LOW) {
 
      // Toggle LED state
      //releState2 = !releState2;
      bajar();
      // Update Button Widget
      Blynk.virtualWrite(V3, LOW);
    }
    btnState2 = LOW;
  } else {
    btnState2 = HIGH;
  }
 
}
 
void setup()
{
  // Debug console
  Serial.begin(9600);
 
 
  pinMode(relePin1, OUTPUT);
  pinMode(btnPin1, INPUT);
  digitalWrite(relePin1, LOW);
 
  pinMode(relePin2, OUTPUT);
  pinMode(btnPin2, INPUT);
  digitalWrite(relePin2, LOW);
 
  Blynk.begin(auth, ssid, pass);
 
  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);
}
 
void loop()
{
  Blynk.run();
  timer.run();
}
 
void subir()
{
  digitalWrite(relePin2, HIGH);
  digitalWrite(relePin1, HIGH);      
  delay (500);
  digitalWrite(relePin1, LOW);
  digitalWrite(relePin2, LOW);
}
 
void bajar()
{
  digitalWrite(relePin2, HIGH);      
  delay (500);
  digitalWrite(relePin2, LOW);
}

Try to send something to the port in the function. If the relay switches on before this, then this can only be influenced by hardware.
But if this is not the case, then you can try this option:
Replace:

with an:

pinMode(relePin2, INPUT);

Switch on the relay like this:

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

Turn off the relay like this:

pinMode(relePin2, INPUT);

You also have a weak wifi password.

1 Like

If it was me, Iā€™d initialise your btnPins to the desired states in your void setup.

I assume that your relays are active HIGH ?

Pete.

1 Like

I used this because HIGH was relay off.
void setup() {

pinMode(D1, OUTPUT); // replace PINx with the pin number
digitalWrite(D1, HIGH); //makes PINx high almost immediately (few uS)