Add a button
Add a button to your machine’s configuration so you can detect and respond to button presses from the Viam app and from code.
Concepts
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.
Steps
1. Add a button component
- Click the + button.
- Select Configuration block.
- Search for the model that matches your button hardware. Search by manufacturer name, chip, or device type.
- Name your button (for example,
my-button) and click Create.
2. Configure button attributes
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.
3. Save and test
Click Save, then expand the Test section.
- Click Push to simulate pressing the button.
Try it
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.
Troubleshooting
What’s next
- Button API reference: full method documentation.
- What is a module?: write a module that responds to button presses.
- Component types: find the right type for your hardware.
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!