Button
The button is made to turn something on and off on your device. However, it can do much more

Settings

Most of the settings are self-explanatory. This document only covers unique ones.

Datastream

Select the Datastream to send values from the button to. If you would like to change properties of the widget, you would need Datastream as well.

Mode

Button can operate in these modes:
  1. 1.
    Push: when finger released - button will switch to OFF state
  2. 2.
    Switch: when finger is released - the button will stay in the pressed state
  3. 3.
    Page: button will open a specified page
  4. 4.
    QR: button will open the code scanner. Read below:

How the QR code scanner works.

End users will tap on the widget and a code scanner (camera) will open. The camera should be pointed to the code.
Once the code is successfully scanned, its contents will be sent to the hardware in a String format to the specified Datastream. The scanner screen will be closed automatically. A String Datastream should be added to the Device Template to accept these values.
Value of the code is sent as is (not pre-processed or post-processed by Blynk).
In case of scanning an unsupported format, no error will be shown. User would need to close the scanner view (camera) manually.
End-users would need to grant permissions to use the camera on the smartphone OS level. If permission wasn’t granted, a message will be shown to go to settings to enable access to the camera.

Scanner works with these code formats

  • QR
  • AZTEC
  • CODE_39
  • CODE_39 mod 43
  • CODE_93
  • CODE_128
  • DATA_MATRIX
  • EAN_8
  • EAN-13
  • ITF
  • PDF_417
  • UPC_E
Android app can additionally support these formats:
  • CODABAR
  • MAXICODE
  • RSS_14
  • RSS_EXPANDED
  • UPC_A
  • UPC_EAN_EXTENSION

How to process button input on the device

When button is pressed, value is sent and stored into the Blynk.Cloud. After that it's sent to your device.

Reading the button value

For example, if Button Widget is set to Datastream with Virtual Pin V1, you can use such code:
1
BLYNK_WRITE(V1) // this command is listening when something is written to V1
2
{
3
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
4
5
if (pinValue == 1){
6
// do something when button is pressed;
7
} else if {
8
(pinValue == 0)
9
// do something when button is released;
10
}
11
12
Serial.print("V1 button value is: "); // printing value to serial monitor
13
Serial.println(pinValue);
14
}
Copied!
Find a full code example for your hardware here.

Changing button state

You can also update the state of the button from hardware.
1
Blynk.virtualWrite(vPin, HIGH);
Copied!
Don't put Blynk.virtualWrite()into the void loop() as it can cause a flood of messages and your hardware will be disconnected. Send such updates only when necessary, or use timers.

Change Button Properties

You can change certain properties of the Widget from your hardware. For that, use this command:
1
Blynk.setProperty(vPin, "widgetProperty", "propertyValue");
Copied!
Where:
  • vPin is: virtual pin number the widget is assigned to
  • widgetProperty: property you want to change
  • propertyValue: value of the property you want to change
Don't put Blynk.setProperty()into the void loop() as it can cause a flood of messages and your hardware will be disconnected. Send such updates only when necessary, or use timers.

Supported properties

Examples below use Virtual Pin V01. Change to your parameters.
Change On/Off labels:
1
Blynk.setProperty(V1, "onLabel", "ON");
2
Blynk.setProperty(V1, "offLabel", "OFF");
Copied!
Set Button Color:
1
//#D3435C - Blynk RED
2
Blynk.setProperty(V1, "color", "#D3435C");
Copied!
Disable/Enable. Widget will be greyed out on UI and users won't be able to tap on it.
1
Blynk.setProperty(V1, "isDisabled", true);
Copied!
Show/Hide. Widget will be hidden from dashboard. Design your UI so that it doesn't look weird when there is no widget.
1
Blynk.setProperty(V1, "isHidden", true);
Copied!
Change Page Target. This command will set which page should open when the button is pressed. PageId can be found in the mobile app in developer mode: Toolbox -> Pages
1
Blynk.setProperty(V1, "page", "pageId");
Copied!
Sync to the latest known state
Get the latest known value from the server. For example, after your hardware went offline and then became online again.
1
BLYNK_CONNECTED() { // checks if there is a connection to Blynk.Cloud
2
Blynk.syncVirtual(V1); // get the latest value
3
}
4
5
BLYNK_WRITE(V1) // this command is listening when something is written to V1
6
{
7
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
8
9
if (pinValue = 1){
10
// do something when button is pressed;
11
} else if {
12
(pinValue = 0)
13
// do something when button is released;
14
}
15
}
Copied!
Last modified 14d ago