Relays turn on when board restarts

hello I am having some trouble, I am trying to control my relays from my IOS device, everything is working fine. The only problem is that in my country the electricity go some times so when the nodemcu restart the relays turn on. My question is how can I do it so when the board restart the relays stays off.

-arduino nodemcu esp8266
-blynk server
this is my code

#define BLYNK_PRINT Serial


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


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "...";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "...";
char pass[] = "...";



 
void setup()
{
  
  

  // Debug console
{  Serial.begin(9600);


   Blynk.begin(auth, ssid, pass);
  
}

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


}

then I tried using this code

#define BLYNK_PRINT Serial


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


#define relay_pin 2
#define relay_pin2 8

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "...";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "...";
char pass[] = "...";



 
void setup()
{
  
   pinMode(relay_pin, OUTPUT);
  pinMode(relay_pin2, OUTPUT);

  digitalWrite(relay_pin, LOW);
digitalWrite(relay_pin2, LOW);
}

  // Debug console
{  Serial.begin(9600);


   Blynk.begin(auth, ssid, pass);
  
}


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


}

and it didn’t work

What board are you using, how are your relays connected, and are the relays active HIGH or LOW ?

Pete.

I am using a node MCU ESP8266 board, my relays are connected like this:
vin to 5v from power supply and ground too
and when I send a 0 signal the relay turn on so i think they are active low
here is the link: https://www.aliexpress.com/item/4000763217712.html?spm=a2g0o.productlist.0.0.30a555288HrKDy&algo_pvid=4f008f05-f06b-4adb-8b5b-dcbcec5d6488&algo_expid=4f008f05-f06b-4adb-8b5b-dcbcec5d6488-16&btsid=0bb0623216060618243478926e5682&ws_ab_test=searchweb0_0,searchweb201602_,searchweb201603_

I just want that when the arduino restart the relays stay off

But which pins on the board are the relays connected to?

If the relays are active LOW then don’t you think you should be setting them HIGH in your void setup?

Pete.

I’ve tried setting them high but nothing happens and I have 2 relays one on D8 and one on D2

Okay, as I suspected, you are telling the NodeMCU to set GPIO2 and GPIO8 to LOW/HIGH

This is very different to D2 and D8

You should either change the 2 and 8 to D2 and D8, or change them to 4 and 15 in your code as these are the GPIO numbers for D2 and D8

Pete.

so this is the right code ?

#define BLYNK_PRINT Serial


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


#define relay_pin 4
#define relay_pin2 15


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";



 
void setup()
{
  
  // Debug console
{  Serial.begin(9600);



   Blynk.begin(auth, ssid, pass);
  Blynk.syncAll();

  
}
   {
   pinMode(relay_pin, OUTPUT);
  pinMode(relay_pin2, OUTPUT);

  digitalWrite(relay_pin, HIGH);
digitalWrite(relay_pin2, HIGH);
}
}


void loop()
{


 {
  
  Blynk.syncAll();
  Blynk.run();
}


}

Yes, if your relays are active LOW

Pete.


this is what I am getting when I plug my board in a 5 v power supply

If you’re referring to the characters highlighted in yellow then this is because the NodeMCU is outputting some boot-up information at its native baud rate, usually 74880, and you have your serial monitor running at 9600, so the NodeMCU output is corrupted.

If you set your serial monitor to 74880 and reboot the board then the NodeMCU messages will make sense, but your serial output from your sketch will be garbled.

The solution is to change this line:

to this:

Serial.begin(74880);

and your serial monitor to 74880 as well, then both the NodeMCU and the debug output from your sketch will make sense.

Do your relays go out when you boot your board now?

Pete.

okay finally its working , now my only problem is that the relay connected to D8 stay active until blynk has connected.

So move your pinMode and digitalWrite commands to before Blynk.begin.

Pete.

THANK YOU SIR its working

1 Like