Previous
Base
Add a board to your machine’s configuration so other components can use your single-board computer’s GPIO pins, analog readers, and digital interrupts.
A board component exposes the low-level I/O on your single-board computer:
The board doesn’t represent external hardware. It represents the I/O capabilities of the computer itself. Motors, encoders, and servos reference the board by name to access the pins they’re wired to. Browse all available board models in the Viam registry.
Go to app.viam.com and navigate to your machine. Confirm it shows as Live in the upper left.
my-board) and click Create.For most single-board computers, the board works with no additional configuration. The model auto-detects available pins.
If you need analog readers or digital interrupts, add them in the attributes:
Analog readers:
{
"analogs": [
{
"name": "my-analog",
"pin": "32",
"spi_bus": "main",
"chip_select": "0",
"channel": 0
}
]
}
Digital interrupts:
{
"digital_interrupts": [
{
"name": "my-interrupt",
"pin": "37"
}
]
}
Click Save in the upper right. viam-server initializes the board
immediately. No restart needed.
If you have an LED wired to a GPIO pin, setting the pin high should light it up.
Toggle a GPIO pin and read its state 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. If you have an LED wired to pin 11, you’ll see it turn on and off when you run the code below.
pip install viam-sdk
Save this as board_test.py:
import asyncio
from viam.robot.client import RobotClient
from viam.components.board import Board
async def main():
opts = RobotClient.Options.with_api_key(
api_key="YOUR-API-KEY",
api_key_id="YOUR-API-KEY-ID"
)
robot = await RobotClient.at_address("YOUR-MACHINE-ADDRESS", opts)
board = Board.from_robot(robot, "my-board")
pin = await board.gpio_pin_by_name("11")
await pin.set(True)
state = await pin.get()
print(f"Pin 11 is {'high' if state else 'low'}")
await pin.set(False)
state = await pin.get()
print(f"Pin 11 is {'high' if state else 'low'}")
await robot.close()
if __name__ == "__main__":
asyncio.run(main())
Run it:
python board_test.py
You should see:
Pin 11 is high
Pin 11 is low
mkdir board-test && cd board-test
go mod init board-test
go get go.viam.com/rdk
Save this as main.go:
package main
import (
"context"
"fmt"
"go.viam.com/rdk/components/board"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/robot/client"
"go.viam.com/rdk/utils"
)
func main() {
ctx := context.Background()
logger := logging.NewLogger("board-test")
robot, err := client.New(ctx, "YOUR-MACHINE-ADDRESS", logger,
client.WithCredentials(utils.Credentials{
Type: utils.CredentialsTypeAPIKey,
Payload: "YOUR-API-KEY",
}),
client.WithAPIKeyID("YOUR-API-KEY-ID"),
)
if err != nil {
logger.Fatal(err)
}
defer robot.Close(ctx)
myBoard, err := board.FromProvider(robot, "my-board")
if err != nil {
logger.Fatal(err)
}
pin, err := myBoard.GPIOPinByName("11")
if err != nil {
logger.Fatal(err)
}
if err := pin.Set(ctx, true, nil); err != nil {
logger.Fatal(err)
}
state, err := pin.Get(ctx, nil)
if err != nil {
logger.Fatal(err)
}
fmt.Printf("Pin 11 is high: %v\n", state)
if err := pin.Set(ctx, false, nil); err != nil {
logger.Fatal(err)
}
state, err = pin.Get(ctx, nil)
if err != nil {
logger.Fatal(err)
}
fmt.Printf("Pin 11 is high: %v\n", state)
}
Run it:
go run main.go
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!