Problem with blynk app's virtual pin trying to control a digital pin in Arduino IDE

Hi all,
I am a newbie with using the Blynk app in conjunction with the Arduino IDE. The context of my question is based around the blynk app and the ESP8266 based Wemos D1 Mini board.

I am trying to control the board’s digital pin D2’s state using virtual pin V5 using the Button widget (button used in switch mode) in the Blynk app. I have an external LED connected to D2. Here is my sketch. The button presses get registered in the serial monitor since I have print statements for debug purposes. This means that the Virtual pin’s state data is getting transmitted from Blynk app to the Arduino Serial Monitor. However, digitalWrite to D2 is not happening. I do not see the LED glow.

What am I missing? I have attached below the sample sketch, the serial monitor output and a mapping between wemos d1 mini, blynk and ESP8266 pins (may not be useful for this question but just sticking it in there).

Is there a sensitivity to sequence of actions such as uploading sketch, opening blynk app, playing project or something along the lines? I just did the conventional way ie. upload sketch, open blynk app, open project, play the app, push the button, check for LED response.

To conclude, as an FYI, if I use the digital pin D2 directly from Blynk app to control the LED and just run the blynk server from the arduino sketch, then the LED seems to respond without issues. The issue shows up only when I use a virtual pin state to control a digital pin.

Arduino IDE Sketch:

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.

// Go to the Project Settings (nut icon).

char auth[] = "XXX";

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

 int LED = D2;
// Define LED as an Integer (whole numbers) and pin D8 on Wemos D1 Mini Pro

void setup()
{
  // Debug console
  Serial.begin(115200);
 // pinMode(LED, OUTPUT); //Set the LED (D1) as an output
  Blynk.begin(auth, ssid, pass);

}

void loop()
{
  Blynk.run();
 
}
 
// This function will be called every time button Widget
// in Blynk app writes values to the Virtual Pin V5

BLYNK_WRITE(V5) {
 int pinValue = param.asInt(); // Assigning incoming value from pin V5 to a variable
 Serial.print("Pin number: ");
 Serial.println(LED);
 Serial.print("Value read from Blynk ButtonPress: "); 
Serial.println(pinValue);
 if (pinValue == 1) {
    digitalWrite(LED, HIGH); // Turn LED on.
  } else {
    digitalWrite(LED, LOW); // Turn LED off.
 }
}

Serial Monitor Output:

21:19:12.743 -> $⸮e⸮d`⸮⸮n⸮[68] Connecting to XXX
21:19:13.291 -> [572] Connected to WiFi
21:19:13.291 -> [572] IP: A.B.C.D
21:19:13.291 -> [572]
21:19:13.291 ->     ___  __          __
21:19:13.291 ->    / _ )/ /_ _____  / /__
21:19:13.291 ->   / _  / / // / _ \/  '_/
21:19:13.291 ->  /____/_/\_, /_//_/_/\_\
21:19:13.291 ->         /___/ v0.6.1 on ESP8266
21:19:13.291 ->
21:19:13.291 -> [578] Connecting to blynk-cloud.com:80
21:19:13.495 -> [769] Ready (ping: 98ms).
21:19:24.399 -> Pin number: 4
21:19:24.399 -> Value read from Blynk ButtonPress: 1
21:19:25.279 -> Pin number: 4
21:19:25.279 -> Value read from Blynk ButtonPress: 0

Try adding:

pinMode(LED, OUTPUT);

into void setup() :wink:

Edit: or just uncomment the one that is there…

1 Like

@JustBertC, this is another proof of me doing a lot of troubleshooting only to realize that I have missed a simple thing. I may need to fix a magnifying glass to my eyeglasses. Thanks a lot for pointing out this really simple/silly mistake. I am truly embarrassed. Great catch. It worked right off the bat after uncommenting it.

Just a follow up to reinforce my understanding, if I use the Digital pin D2 directly from the Blynk app, I assume I do not need to add the pinmode statement in the void setup right? (Since the idea is to have blynk directly control the hardware pins without the conventional arduino sketch setup statements) ?

1 Like

Yes, correct :slightly_smiling_face:

Perfect, thanks.

1 Like