Digital output getting active at powering up ESP

Hi
I used a simple from library to run relay module with D7 output and a push button on Blynk app.
when I connect the USB cable to the ESP card and run the project on Blynk app the D7 getting active and make the relay module to be energized while the bush button on OFF mode .I also changd the output from 1 to 0 and still same.
please see screenshot


any advice please

The first thing to do is to work-out if your relay is activated when a LOW signal or a HIGH signal is applied to it. Most relay boards that you buy on the internet seem to be active LOW.

You could add a pinMode statement followed by a a digitalWrite statement in your void setup, to set your relay pin to the correct status. I would also suggest using virtual pins and modifying your code to change the status of your relay when the virtual pin state changes.

Pete.

1 Like

any link to show example since I’m new in coding

Lots of examples if you search.
This probably isn’t the best example, but it’s one of the first I came across…

In your example you are using pin D7, which is GPIO13
This example uses GPIO2, which is pin D4

Change the sketch or your wiring to suit.

Pete.

Hi
With is modification I’m getting now at connecting the ESP to power bank it is switched ON then OFF
please see the code and advice if I miss something :slight_smile:

#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[] = "";

BLYNK_CONNECTED()
{
  // if your relay is HIGH triggering 
  if (digitalRead(2) == 1) Blynk.virtualWrite(V7, digitalRead(2));
  else Blynk.syncVirtual(V7);

/* uncomment this section if your relay is LOW triggering
 * if (digitalRead(2) == 0) Blynk.virtualWrite(V2, digitalRead(2));
  else Blynk.syncVirtual(V2);
 */
  
}

BLYNK_WRITE(V7)
{
  int pinData = param.asInt();
  digitalWrite(2, pinData);
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH); // if your relay is HIGH triggering
  // digitalWrite(2, HIGH); // uncomment this if your relay is LOW triggering
  
  //Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);
}

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

So what is the answer to this question?

Pete.

Sorry,I didn’t got your point
I want the relay to be OFF at startup and to be ON when push the button ON Blynk app.
I dont want from relay to be energized once plug the cable to power bank.

Okay let’s take a few steps back…
Are you using a bare relay, or one on a board with some other components?
If it’s the latter, how are the connecting pins on the board labelled?
How are you wiring these pins up to power the relay?

Pete.

Please see this picture


Relay VCC and GND connect to ESP 5V and GND
As code in previous comment V7 the In1 of relay

Nothing connects to V7 other than the switch widget. V7 is a Virtual pin - it doesn’t physically exist, so you can’t connect a relay to it.
When the status of the widget attached to pin Vt changes, it triggers the BL6NK_WRITE(V7) callback function.
In that function you have code which writes a value to GPIO2, and as I said earlier, GPIO2 is the pin that’s labelled D4 on your NodeMCU.

So In1 on your relay should connect to pin D4 on your NodeMCU.

You still need to know if your relay activates on a HIGH or LOW signal. If connecting In1 to GND energises the relay then it’s active LOW. If connecting In1 to VCC energises the relay then it’s active HIGH.

Pete.

In to GND energies the relay

So your relays are Active LOW, or “LOW triggering” as described in the coe, which means that you need to follow these instructions…

and of course comment-out the “High triggering” section too.

There is also a section of code in void setup which needs to be changed in the same way.

Pete.

the relay still ON and OFF at the moment of plugging the USB cable to PC or power bank even I did the changes as described and hope it is correct ,please see the new code and advise if there is any correction needed

#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[] = "";

BLYNK_CONNECTED()
{
  // if your relay is HIGH triggering 
  if (digitalRead(2) == 0) Blynk.virtualWrite(V7, digitalRead(2));
  else Blynk.syncVirtual(V7);

/* uncomment this section if your relay is LOW triggering
 * if (digitalRead(2) == 0) Blynk.virtualWrite(V2, digitalRead(2));
  else Blynk.syncVirtual(V2);
 */
  
}

BLYNK_WRITE(V7)
{
  int pinData = param.asInt();
  digitalWrite(2, pinData);
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH); // if your relay is HIGH triggering
  // digitalWrite(2, HIGH); // uncomment this if your relay is LOW triggering
  
  //Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);
}

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

Maybe you should choose a better pin than GPIO2 (D4)…

Pete.

1 Like

Thank you Pete, that’s very helpful excel sheet ,it is work without blinking in the powering up but since I’m new in coding I think I have destroyed the whole code after embedding the code of triggering to the code of water level because the Blynk app now controlling only the push button but not readig the rest of the code for water level measurement , please advise what is the mistake in the code below in order to execute the overall steps

#define TRIGGER D0
#define ECHO    D2

// NodeMCU Pin D0 > TRIGGER | Pin D2 > ECHO

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#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[] = ";// 
#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).



BLYNK_CONNECTED()
{
  // if your relay is HIGH triggering 
  if (digitalRead(5) == 0) Blynk.virtualWrite(V7, digitalRead(5));
  else Blynk.syncVirtual(V7);

/* uncomment this section if your relay is LOW triggering
 * if (digitalRead(2) == 0) Blynk.virtualWrite(V7, digitalRead(5));
  else Blynk.syncVirtual(V7);
 */
  
}

BLYNK_WRITE(V7)
{
  int pinData = param.asInt();
  digitalWrite(5, pinData);
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH); // if your relay is HIGH triggering
  // digitalWrite(5, HIGH); // uncomment this if your relay is LOW triggering
  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH); // if your relay is HIGH triggering
  // digitalWrite(2, HIGH); // uncomment this if your relay is LOW triggering
  //Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);
  


}
  
  


void loop() {
  
  long duration, distance;
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(2); 
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10); 
  
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;

   if (distance <=100) {
    Blynk.virtualWrite(V0, 80);
}
  else {
    Blynk.virtualWrite(V0, 0);
  }

 if (distance <= 75) {
    Blynk.virtualWrite(V1, 100);
}
  else {
    Blynk.virtualWrite(V1, 0);
  }

   if (distance <= 50) {
    Blynk.virtualWrite(V2, 100);
}
  else {
    Blynk.virtualWrite(V2, 0);
  }

   if (distance <= 25) {
    Blynk.virtualWrite(V3, 100);
}
  else {
    Blynk.virtualWrite(V3, 0);
  }

   if (distance <= 5) {
    Blynk.virtualWrite(V4, 100);
}
  else {
    Blynk.virtualWrite(V4, 0);
  }

  
  
  Serial.print(distance);
  Serial.println("Centimeter:");
  Blynk.virtualWrite(V5, distance);
  delay(100);
  Blynk.run();

 
}

You need to read this and follow the example of how to use a timer to call a function - which in this case will take readings from your sensor and display the results in Blynk.

It’s also not a good idea to mix the way that you refer to your pins. I would encourage you to stop using the “D” numbers that are screen-printed on your NodeMCU and instead use GPIO numbers. That way it’s easy to see when you’ve referenced the same pin twice for two different uses.

Pete.

1 Like

I changed it as per your advice and it is work perfectly
thank you for your time and help
still one more question how can I make the distance between the the water top level read 100 % in the Gauge of the Blynk app when the distance is around 20 cm between the HCR04 and watewr level when it is in maximum level.

shall I post this question here or in another post ??

Keep it in this topic.
The series of if statements that were in your void loop are where the percentage full numbers come from for each distance.
At the moment, they don’t make much sense, as the options are 0%, 75% and 100%

You should start by working out what distance reading you want to translate into which percentages, and how many steps you want 0, 25, 50, 75, 100 for example, or 0, 10, 20, 30, 40 etc.

Pete.

1 Like

You could show the level in the gauge widget without having to use if statements.

Just set the values in the gauge widget to match the values of full and empty. You should have them reversed in the widget, that is the larger value (tank empty) first, and the smaller value (tank full) second. This would make it so the smaller value (tank full) would show the gauge as full.

2 Likes