How do I use virtual pins as buttons?

Hey, I am working on a project on Arduino in which a button widget on virtual pin 0 acts as a switch, how can I write my code?

I need something like this:

 if (digitalRead(V0) == HIGH) {
    digitalWrite(13, HIGH)
    digitalWrite(12, LOW)
}
else {
     digitalWrite(12, HIGH)
     digitalWrite(13, LOW)
}

Hi,you may try this code:

BLYNK_WRITE(0) // At global scope (not inside of the function)
{
    int i=param.asInt();
    if (i==1) 
    {
        digitalWrite(13, HIGH);
        digitalWrite(12, LOW);
    }
    else 
    {
        digitalWrite(12, HIGH);
        digitalWrite(13, LOW);
    }
}
1 Like

I tried with that but it shows this error:

test-blynk.ino: In function ‘void loop()’:
test-blynk:122: error: ‘param’ was not declared in this scope
‘param’ was not declared in this scope

Hi try this code

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “***********************”;

void setup()
{
Serial.begin(9600);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
Blynk.begin(auth, “yourSSID”, “yourPassWord”);
}

BLYNK_WRITE(0)
{
int i=param.asInt();
if (i==1)
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
}
else
{
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
}
}

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

Hi, did you check this doc? https://github.com/blynkkk/blynk-library/blob/master/docs/Basics.md#virtual-pins
Hope this helps.

Hi! Did this work? :slight_smile:

I have beginner question. How can i use buttons on off if button is on fan will on else fan will off can you give codes of this example but all parts of the code for example i attached the fan pin to digital write 6 and how can i on or off this pin by using blynk button

@Taner_Kopya take a look at these 2 threads and see if it helps explain how virtual pins work and how flexible they are.

If you still need a sketch for your project after reading the threads let us know and we will see what we can do to help.

is this okey? when button connected to v10 and when it is on fan is working that connects to digital pin 4 of arduino and when is button off fan stop

is this code correct?

BLYNK_WRITE(V10)
{
int y = param.asInt();

if (y == 1)
{
digitalWrite(4, HIGH);
}
else if (y == 0)

{
digitalWrite(4, LOW);

}
}

@Taner_Kopya looks perfect.

Technically the else if (y == 0) can be simply an else because with Blynk button widgets the functions are only called when you press or release the button.

i have also problems with using virtual pins. I am using the code below and my outputs a low constantly.

i am trying to let it act as button to put the ledstrip off but on on the value i set on zebra application

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200
ESP8266 wifi(&Serial);

void setup()
{
  // Debug console
  Serial.begin(9600);

  delay(10);

  // Set ESP8266 baud rate
  Serial.begin(ESP8266_BAUD);
  delay(20);

  Blynk.begin(auth, wifi, ssid, pass);
}
BLYNK_WRITE(0)
{
int i=param.asInt();
if (i==0)
{
analogWrite(6,0);
analogWrite(9,0);
analogWrite(11,0);
}
else
{
  Blynk.run();
}
}
void loop()
{ 
  Blynk.run();

}

This topic is over 4 years old. You may be better off creating a new topic.

In regards to your question. It should be BLYNK_WRITE(V0). You also do not need the Blynk.run(); in the function. The virtual button by default will output a 1 when pressed, and a 0 when not pressed/released. So if your virtual button has the default settings the analogWrite commands will be executed upon release of the button.

BLYNK_WRITE(V0)
{
 int i=param.asInt();
 if (i==0)
 {
  analogWrite(6,0);
  analogWrite(9,0);
  analogWrite(11,0);
 }
else
 {
   //do nothing
 }
}

don that still only low voltage

and I will create a new one tomorrow