[SOLVED] how to let blynk app's virtual pin to control a dig 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 famous blynk app and the ESP8266 based Wemos D1 Mini board. Granted I could contact the blynk forum or ESP8266 forum for my question but I find this forum to be the culmination of knowledge-base of all these intertwined topics and with plenty of helpful folks.

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.

#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.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

Mapping between Wemos D1 Mini, Blynk app and the ESP8266 chip pin:
Wemos pin Blynk Widget Button ESP8266 pin


D0 D2 16
D1 D3 5
D2 D4 4
D3 D8 0
D4 D9 2 (Built-in LED)
D5 D5 14
D6 D6 12
D7 D7 13
D8 D10 15

Guys, we can close this thread. The root cause of the issue was not uncommenting the pinmode statement in the void setup section of the code. A simple mistake but slipped out of my radar somehow.

we can close this thread.

If you edit your original post you can add [SOLVED] to the description to indicate that you have found a solution which may help others searching for problems

Thanks for the pointer. I have marked on the subject as "SOLVED".