Wemos D1 Mini pin stays high on Blynk

Hello to everyone! I need your help with a simple project. I’m trying to build a smart plug that I can both control with a button and programme to start at a certain time. Through Blynk, of course. I’m using a simple 10A 220V relay. Everything is all wired up and working accordingly on the hardware side.

And after combing through the internet (this forum included), I am at my wits end!!! I’m using a Wemos D1 Mini and every time I try to trigger a digital pin, the PIN goes high and stays that way.

And lets be clear: I’ve tried every form of sketck I could find and think of myself. The exact behaviour is this:

  1. If I map to virtual pin, it does nothing. If I just set a digital pin (no matter what digital pin), the pin goes high, the relay starts and stays that way.
  2. The pin goes high the moment I start the project in the Blynk app. And that means that THE BUTTON IS NOT EVEN PUSHED YET.
  3. Pushing the button (either digital, or virtual) does absolutetly nothing. Event when the pin is high, pushing the button twice does not turn the relay off, for example.
  4. If the project is already started in the Blynk app, the pin goes high automatically and stays that way.
  5. The same behaviour applies to other types of widgets in Blynk, like timers, for example. The moment I start the project, even if the timers has not reached the set time yet, the pin goes high and the relay turns on and stays that way, no matter what I do.

The latest sketch I’ve used is below. And again, I’ve tried everything under the sun. And please don’t say that it’s the hardware. It might be a Chinese clone, but it works fine. I’ve used the exact same board with a DHT22 sensor and it works great.


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

char auth[] = "c4ccbaea97c8464f831f2920f96f3403";
char ssid[] = "HTC Portable HotsTpot 51A8";
char pass[] = "086756123t6g4";

int RelayTrigger = 20;                   // Define relay trigger pin as pin 20 (also known as D1)
int pinValue = 0;                        // Start Wemos with D1 pin in low state.

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt();         // Assign value from pin V1 to a variable
  if (pinValue == 1) {                  // If value of pin V1 is 1, run the following command
    digitalWrite(RelayTrigger, HIGH);             // Output of pin D1
  }
  else {                               // If value of pin V1 is 0, run the following command
    digitalWrite(RelayTrigger, LOW);
  }
  Serial.print("V1 Button value is: ");
  Serial.println(pinValue);
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
}

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

The serial monitor shows that Blynk is comunicating correctly and that V1 does send commands to the Wemos:

02:57:17.687 -> [8051] Connected to WiFi
02:57:17.687 -> [8051] IP: 192.168.1.24
02:57:17.687 -> [8051] 
02:57:17.687 ->     ___  __          __
02:57:17.687 ->    / _ )/ /_ _____  / /__
02:57:17.687 ->   / _  / / // / _ \/  '_/
02:57:17.687 ->  /____/_/\_, /_//_/_/\_\
02:57:17.687 ->         /___/ v0.6.1 on ESP8266
02:57:17.687 -> 
02:57:17.687 -> [8057] Connecting to blynk-cloud.com:80
02:57:18.796 -> [9161] Ready (ping: 72ms).
02:57:34.277 -> V1 Button value is: 1
02:57:47.387 -> V1 Button value is: 0
02:57:57.520 -> V1 Button value is: 1
02:58:09.725 -> V1 Button value is: 0

D1 is actually GPIO 5… there is no GPIO 20 on an ESP8266 :wink:

FYI, many pins on an ESP8266 will boot HIGH (but GPIO 5 should boot LOW). Here is a handy chart…

And it is still best practice to use pinMode() in your void setup() for any used GPIO…

pinMode(RelayTrigger, OUTPUT);
1 Like

Hi and thanks for the suggestions. But unfortunately that’s not the issue. The pin doesn’t boot high just by itself. Only it is set in Blynk and when the project is started. Even though the button in Blynk is not pushed. If I just leave the project in Blynk unstarted, nothing happens. Also, if I just set the V1 pin in Blynk, again, nothing happens.

By the way, I’ve tried both Push and Switch.

What’s not the issue… the fact that you are using pin 20? (which doesn’t exist on Wemos D1 Mini)

Or the fact that you are not using pinMode() (it is needed)

Or are you pursuing the “boots high” issue when all the other things need fixing first

@Gunner, as I’ve said before, I’ve already done what you suggested. I’ve gone through all possible variations of code before posting.

You can find the modified code bellow. I’ve tried this option again, as you suggested. Same behaviour. The pin starts high, even though I tell it to start low. Pushing the button in Blynk (set as V1) does absolutety nothing.

I’ve also reinstalled the Blynk library and ESP8266 core library with the latest stable versions. And I’ve set the Arduino IDE to erase all flash content. Still the same problem.

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

char auth[] = "83c1a4bf62e846f09072e4590737f555";
char ssid[] = "HTC Portable HotsTpot 51A8";
char pass[] = "086756123t6g4";

int RelayTrigger = 5;                   // Define relay trigger pin as pin GPIO5 (also known as D1)
int pinValue = 0;                        // Start Wemos with D1 pin in low state (D1 = GPIO5)

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt();         // Assign value from pin V1 to a variable
  if (pinValue == 1) {                  // If value of pin V1 is 1, run the following command
    digitalWrite(RelayTrigger, HIGH);             // Output of pin D1
  }
  else {                               // If value of pin V1 is 0, run the following command
    digitalWrite(RelayTrigger, LOW);
  }
  Serial.print("V1 Button value is: ");
  Serial.println(pinValue);
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(RelayTrigger, OUTPUT);
  digitalWrite(RelayTrigger, LOW);
}

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

I don’t recall reading this anywhere, but nice to know now :stuck_out_tongue_winking_eye:

Since that pin (D1 = GPIO 5) defaults to LOW on ESP8266 boot, then there is either a fault in the board, or the code.

Are you sure your relay isn’t triggered on a LOW signal? Some are.

I would put these before Blynk.begin()… as that is blocking command and no further processing happens until connected to the server.

I recommend using global variables, not local

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope

And just for kicks… your entire function can be condensed like this…

BLYNK_WRITE(V1)
{
  digitalWrite(RelayTrigger, param.asInt());  // Output of pin D1 with state of V1
  Serial.print("V1 Button value is: ");
  Serial.println(param.asInt());
}

And on that note… what does the serial monitor say about the value of the Button when you press it?

Sorry! I might of been a bit harsh. I didn’t mention that I used your solution in particular. It’s just that I’ve been struggling with this for more than two days.

And you are right. The relay does trigger on a LOW signal. That was very stupid of me. However, I still can’t get it to work. The relay triggers on boot. So, you’re saying that I need to set the trigger pin to high, right? Here’s the updates code:

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

char auth[] = "2c913a9efc3248109c1ef4d1bb9b9172";
char ssid[] = "HTC Portable HotsTpot 51A8";
char pass[] = "086756123t6g4";

int RelayTrigger = 5;                                   // Define relay trigger pin as pin GPIO5 (also known as D1)

BLYNK_WRITE(V1)
{
  digitalWrite(RelayTrigger, param.asInt());  // Output of pin D1 with state of V1
  Serial.print("V1 Button value is: ");
  Serial.println(param.asInt());
}

void setup()
{
  Serial.begin(115200);
  pinMode(RelayTrigger, OUTPUT);
  digitalWrite(RelayTrigger, HIGH);
  Blynk.begin(auth, ssid, pass);
}

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

Yes… however, I do not know if your relay is sensitive and quick enough to fully trigger on the initial LOW pulse.

You could try GPIO pins 12, 13 or 14 as they do boot HIGH for a short time (100ms)… by which time your code should have confirmed that state with the PinMode() and digitalWrite() in the setup()

Still no dice. I’ve tried all the GPIOs you suggested. And I even tried an ESP-01 module with no results. I’m not sure it’s the relay module. It works just fine with an Arduino UNO and even connected directly to a PIR sensor.

Could it be a bug in the Blynk library.

Do you not think that if this were the case then thousands of other Blynk users might have noticed?

Your code works fine, except that if your relay is On when a LOW signal is applied then you need to either change your Blynk switch widget to send 1/0 rather than 0/1, or add a ‘not’ to your digitalWrite like this:

  digitalWrite(RelayTrigger, !param.asInt()); // Invert the value coming from the switch widget

that way, your relay will be on when your Blynk switch widget says on. You obviously also need to set the widget to switch mode not the default push mode, unless you only want the relay on when the switch button widget is pressed.

My guess is that the problem is how you are powering your relay, or the way that you’ve wired your relay.
Post a schematic of how your relay is wired.

Pete.

I understand. All Im saying is that it might be a bug in the latest Blynk library. I’m not blaming someone. Adding the “not” to digitalWrite did nothing. I moved to a safe pin, namely D5. Same beahviour. Here is the schematic. I’m pretty sure I wired everything correctly. The NO conector is the one that I’m using. I should also mention that Ive used both a modified phone charger and a beardboard power module (exactly like the one in the schematic below). The relay is not exactly like the one in the schematic, but rather the one in the photo.


s-l300

Trust me, there isn’t, so move on from that idea.

I really hope you haven’t wired your 5v breadboard PSU to 240VAC like you’ve shown here:

What happens if you disconnect the green wire from D5 and apply 5v+ to it, does the relay actuate? What happens if you connect it to negative?

Pete.

As I’ve told Grubber, I’m terribly sorry if I’m coming out as harsh. I don’t mean to offend anybody. Just trying every possible explanation.

As for the wiring, of course I haven’t wired it as above. That wouldn’t have been very good, would it? :slight_smile: It was the only way to connect the part in Fritzing. In reality I have a 9 V adapter connected to the power module.

I’ve tried your suggestion and everything works accordingly: shorting it to ground triggers the relay and connecting to 5 V keeps it open. So the problem is that I can’t get any of the pins on the Wemos to have a set state when the device powers up.

I’ve measured the voltages on the Wemos pins. When their low they measure around 3.3 V. When their HIGH they measure around 4.3 V. But here’s the intereting part. When I push V1 in the Blynk app, the volatge goes down to 0.12 V. That’s with the states reversed from 1 to 0. From 0 to 1 it goes from 0.12 V o 3.3 V, obviously. :slight_smile:

So, this is weird. How come when it’s HIGH, the voltage goes lower, not higher?

It’s a problem with your relay.

I just dug out some relays from my box of bits. I found one that’s identical to the one you’re using, in an unopened packet, so it’s brand new.

It doesn’t work with your code on a D1 Mini, but a better quality two channel opto-isolated relay that I also had works fine.

The relay on the left works fine, the on on the right doesn’t.

I would have spent time messing around with pull-up or pull-down resistors to see if that helped, but TBH I have other things to do.

Get some decent opto-isolated relays and you should be fine.

You might also be interested in this thread:

If you’re interested in hacking existing mains power switches then take a look at this:

Pete.

1 Like

Most people seem to say that 3.3 V should be enough for the trigger, as long as I have 5 V on the VCC. Does not seem to be the case, though. :frowning: So it’s probably the relay. Although it’s very weird that it worked connected directly to a PIR sensor in the past. I’ve just tested it with a completely new PIR sensor and it no longer triggers.

Sorry for wasting everybody’s time. It was all a hardware issue, but I was sure it worked. Will try a new relay the next few days and see if it works.

I think you meant @Gunner.

Grubber is a hedgehog in My Little Pony…

@Gunner will need to say how good a likeness it is :rofl:

Pete.

2 Likes

I have less hair and am a bit paler… but otherwise we must have been separated at birth :rofl::rofl:

@PeteKnight should we be concerned that you even knew about this My Little Pony identity :stuck_out_tongue_closed_eyes:

1 Like

I have to admit that I couldn’t remember exactly which one Grubber was, but please don’t let my granddaughter know that or I’ll be booked-in for an MLP re-familiarisation session :roll_eyes:

Pete.

1 Like

Thankyou! I was using the exact same crappy Chinese relay (pictured on the right), and my problem was fixed by using a different relay… Searched everywhere for this answer.

1 Like