When restarting the relay remains on.....NodeMCU

I’m using a two-channel relay, I used this following code, I configured the two D5 buttons for channel 1 and D7 for channel two. Everything works well but when you restart the NodeMcu channel 1 (D5) remains accessed. How can I make sure that at both reboots both channels are off ???

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


char auth[] = "xxxxxxxxxx";
 
char ssid[] = "xxxxxxx";
char pass[] = "xxxxxxx";
 
void setup() {
  Serial.begin(115200);
 
  Blynk.begin(auth, ssid, pass);
}
 
void loop() {
  Blynk.run();
}

I suspect you will need to look into the use of virtual pins.

What type of board are you using? Some of the esp dev boards have pins boot in a certain state as to make sure the ESP will boot in the correct configuration.

Or maybe try adding this to your set-up function. You may need to change the LOW to HIGH depending on if your relays are active LOW or active HIGH

void setup() {

  Serial.begin(115200);

pinMode(14, OUTPUT); //D5 is GPIO 14 
pinMode(13, OUTPUT); //D7 is GPIO 13 
digitalWrite(14, LOW); //Set GPIO 14 LOW
digitalWrite(13, LOW); // Set GPIO 13 LOW

Blynk.begin(auth, ssid, pass);

}

Hardware usated

Relay:

NodeMCU

I had to set the command: digitalWrite (14, HIGH) with LOW were accessed. Maybe they work the other way around. Attach code that works for other forum users.

Thank you

PS: For app buttons I used DIGITAL pin

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


char auth[] = "xxxxxxxxxxxxxxxxxx";
 
char ssid[] = "xxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxx";
 
void setup() {
  Serial.begin(115200);

  pinMode(14, OUTPUT); //D5 is GPIO 14 
  pinMode(13, OUTPUT); //D7 is GPIO 13 
  digitalWrite(14, HIGH); //Set GPIO 14 LOW
  digitalWrite(13, HIGH); // Set GPIO 13 LOW
 
  Blynk.begin(auth, ssid, pass);
}
 
void loop() {
  Blynk.run();
}

There are two types of relays. Low lever trigger and high level trigger. Some relays you can change between high/low trigger by moving a jumper plug.

You have a low level trigger relay which is turned on when gpio on your nodemcu is low.

I have exactly the same issue, can someone suggest a link of the High level relay? I looked up alliexpress and was not able to figure out which is the correct relay for this application. Thanks

@anjeel why not just change the widget configuration to 1 and 0 from 0 and 1?

1 Like

Thank you Costas, this worked and solved my problem. The other problem that I noticed only now was that I was connecting the relay power VCC and ground to an “external supply” (Since I thought this was a 5v relay and my NodeMCU was 3.3v). With Relay connected to external power supply nothing was working. I then connected the Relay VCC and ground to the board (3.3v) and reversed the widget as you suggested and the relay started ticking just the way I wanted it to.

The bigger question is now to connect this relay to a 220V AC load where I am going to draw about 5-6 AMPS running a motor. I will add a diode to prevent any damage to my board. Not sure if this will work or not, Any comments on this, thanks in advance.

You need to have a shared Ground wire between your relay PSU and the NodeMCU.

That is not really a question for this forum… Mains power is deadly!! Always seek out a local professional, not a forum, before messing with mains power, particularly 240vac.

If you feel your relay is not sufficiently isolating the AC from the switching MCU, then a diode is not a solution in any way… a better isolated relay is.

If you are referring to using the diode as flyback protection, then be aware that you cannot use a diode on an AC motor anyhow.

Thanks a ton. Your advice is much appreciated. Excuse my knowledge on electronics, I am more of a IT professional.

(1) I now understand that for flyback protection there is no need to add a Diode so long as I am using AC. Thanks

(2) I will test my relay if this works or not, in case not then I will look for a better isolated relay. Would it be right to combine a Solid State Relay with my 5V relay already in place.

Thanks again.

Define combine… as in add a 2nd SS relay to your project to control a 2nd AC device, then sure, why not

Or do you want to switch one relay from another? If so, then NO don’t do that, as there is no need and could lead to other issues if you are unsure of what you are doing.

Just make sure you use a TTL switching compatible relay (electrical/mechanical or SS) that is properly rated for the load (if in doubt, ask that professional… no not the paperboy or the neighbor who borrowed, and still has, your hammer last year… ask a mains power pro)

Be aware that SS “relays” are sometimes capable of variable controlled output (e.g. dimmer or even speed control). If you need that then make sure it is properly rated for it. If you don’t need that, then make sure OFF meens OFF and not allowing a small leakage of current.

https://learn.sparkfun.com/tutorials/logic-levels/ttl-logic-levels

Thanks. I am planning to add 2nd SS relay to the project to control a 2nd AC device.

regds
Anjeel