Add a button
Add a button to your machine’s configuration so you can trigger button presses programmatically and represent a physical button in your machine config.
Concepts
A button component represents a momentary push button. The API is intentionally simple: a single Push method that simulates pressing the button. The button API does not listen for or report incoming presses; a module can still react to real presses internally, but from SDK code you only initiate presses.
Physical button hardware typically comes from a module in the Viam registry that watches a GPIO pin.
The fake built-in model is useful for testing code without physical hardware.
Built-in models
Registry modules
For hardware the built-in models don’t cover, browse the Viam registry. Each module’s configuration is documented on its registry page.
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 API keys. Copy the API key and API key ID. Copy the machine address from the Connection details section on the same tab. 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
Related
- Button API reference: full method documentation.
- What is a module?: write a module that responds to button presses.
- Add a component: 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!