Previous
Board
Add a button to your machine’s configuration so you can detect and respond to button presses from the Viam app and from code.
A button component represents a momentary push button. The API is intentionally
simple. It exposes a single Push method that triggers the button. Physical button
hardware typically comes from a module in the Viam registry that reads a GPIO pin
and exposes it as a button component.
The fake built-in model is useful for testing code without physical hardware.
my-button) and click Create.Attributes vary by module. For the fake model, no attributes are needed:
{}
For a GPIO-connected button with a registry module, you’ll typically configure the board and pin:
{
"board": "my-board",
"pin": "22"
}
Check your module’s documentation in the registry for the full list of attributes.
Click Save, then expand the TEST section.
Push the button programmatically.
To get the credentials for the code below, go to your machine’s page in the Viam app, click the CONNECT tab, and select SDK code sample. Toggle Include API key on. Copy the machine address, API key, and API key ID from the code sample. When you run the code below, the button’s Push method fires. With a physical button connected with a module, this triggers whatever action the module defines.
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!