Need to attach a virtual pin to a digital pin

I would like to have a button widget that uses a virtual pin also control a digital pin. Sounds simple, but I can’t find the code to do this. Please help.

In void setup, declare the digital pin as an OUTPUT device in a pinMode statement.

In your BLYNK_WRITE(vPin) callback function, obtain the value from the switch widget using the para.asInt() command.

if you want the digital pin to be HIGH when the switch widget is on then do a digitalWrite in that situation, like this bit of pseudo code…

BLYNK_WRITE(vPin)
{
  if(param.asInt())
 {  
    digitalWrite(my_digital_pin,HIGH);
  }
  else
  {
    digitalWrite(my_digital_pin,LOW);
  }
}

Pete.

I did this, but no luck

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

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

BLYNK_WRITE(V0){
   int pinValue = param.asInt();
   if(param.asInt())
 {  
    digitalWrite(13,HIGH);
  }
  else
  {
    digitalWrite(13,LOW);
  }
}

Are you sure you are looking at the correct pin?
Screenshot(1)

How is your button widget configured in the app?

BTW, the int pinValue = param.asInt(); line in your code is redundant. You are capturing the state of the widget button to a local variable called pinValue, but then you aren’t using this in your if test.

A good way to debug issues like this is to add some debug messages and see what happens in your serial monitor…

BLYNK_WRITE(V0)
{
  Serial.println("Button V0 has changed value");
  Serial.print("GPIO13 state is currently ");
  Serial.println(digitalRead(13));

  if(param.asInt())
  {  
    digitalWrite(13,HIGH);
    Serial.println("The button value was 1. so GPIO13 set to HIGH (1)");     
  }
  else
  {
    digitalWrite(13,LOW);
    Serial.println("The button value was 0 so GPIO13 set to LOW (0)");     
  }
}

Pete.

maybe you can try this code, its worked for me (im useing node mcu to control led)

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

char auth[] = "your auth";
char ssid[] = "your ssid";
char pass[] = "your pass";

int led = D0; //led pin

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(led, OUTPUT); //define D0 as output 
}

BLYNK_WRITE(V0){
  int state = param.asInt(); //looking for V0 value and store it to state variable
  if (state == 1){ //when satet value is 1 the led is on, vice-versa
  digitalWrite(led, HIGH);
  }
  else{
    digitalWrite(led, LOW);
  }
}

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

I am using an ESP32 dev board and have used pin 13 successfully before.

the button widget is V0 and 0-1

tried this but no luck

the frustrating part is I have done this with sliders with not problem.


Servo throttle;

void setup() {
  Serial.begin(115200);  
  Blynk.begin(auth, ssid, pass);
  throttle.attach(18);
}
BLYNK_WRITE(V3)
{
  throttle.write(param.asInt());  
}

void loop() {
 Blynk.run();

}

Push or Switch setting?

Did you try the serial print messages?

My guess is that the switch widget is set to Push and your GPIO is going HIGH then immediately LOW and you’re not seeing it, but the serial prints would tell you this.

Pete.

Thank you all for the help. I resolved an issue with the app. This is working.

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
}
BLYNK_WRITE(V0) {
 int pinValue = param.asInt();
 if (pinValue == 1)
 {
    digitalWrite(2, HIGH);
  } 
 else
 {
    digitalWrite(2, LOW);
 }
}

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

So it looks like the original code and a different pin. Did you move the pin on the hardware or were you looking at the wrong pin?

It was an authentification issue.