BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

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.