Board Component

A board is the signal wire hub of a robot that provides access to general purpose input/output (GPIO) pins: a collection of pins on the motherboard of a computer that can receive electrical signals.

You can control the flow of electricity to these pins to change their state between “high” (active) and “low” (inactive), and wire them to send digital signals to and from other hardware.

This control is simplified with viam-server. When Viam’s software is running on a computer with GPIO pins accessible to external hardware components, it manages GPIO signaling to abstract control to resource APIs.

Figure: Two different board options: a single-board computer with GPIO pins running `viam-server` locally, or a GPIO peripheral plugged into a desktop computer's USB port, with the computer running `viam-server`.
Image showing two board options: First, running viam-server locally and second, running via a peripheral plugged into the USB port of a computer that is running the viam-server.

The RDK also provides the GPIOPin interface for direct control and monitoring of the state of GPIO pins.

Configuration

Configure a board component on your robot to communicate with the other components of the robot. Signaling is overseen by a computer running viam-server.

A board can be:

  • A single-board computer (SBC) with GPIO pins and a CPU capable of running viam-server.
  • A GPIO peripheral device that must connect to an external computer.
  • A PWM peripheral device that must connect to an SBC that has a CPU and GPIO pins.

For model-specific configuration information, click on one of the following models:

ModelDescription
piRaspberry Pi 4, Raspberry Pi 3 or Raspberry Pi Zero 2 W
tiTexas Instruments TDA4VM
beagleboneBeagleBoard’s BeagleBone AI-64
jetsonNVIDIA Jetson AGX Orin, NVIDIA Jetson Orin Nano, NVIDIA Jetson Xavier NX, NVIDIA Jetson Nano
numatoNumato GPIO Modules, peripherals for adding GPIO pins
pca9685PCA9685 Arduino I2C Interface, a 16-channel I2C PWM/servo driver peripheral
upboardAn Intel-based board like the UP4000
fakeA model used for testing, with no physical hardware
otherYou can use other boards with modular components such as periph_board

Attribute Configuration

The following configuration attributes are available for every board model besides the numato and pca9685 peripherals and fake.

Configuring these attributes on your board allows you to integrate analog-to-digital converters, digital interrupts, and components that must communicate through the SPI and I2C protocols into your robot.

analogs

An analog-to-digital converter (ADC) takes a continuous voltage input (analog signal) and converts it to an discrete integer output (digital signal).

ADCs are useful when building a robot, as they enable your board to read the analog signal output by most types of sensors and other hardware components.

To integrate an ADC into your robot, you must first physically connect the pins on your ADC to your board. If your ADC communicates with your board using SPI, you need to wire and configure the SPI bus in addition to the analogs.

Then, integrate analogs into the attributes of your board by adding the following to your board’s JSON configuration:

// "attributes": { ... ,
"analogs": [
  {
    "chip_select": "<chip-select-pin-number-on-board>",
    "name": "<your-analog-reader-name>",
    "pin": "<pin-number-on-adc>",
    "spi_bus": "<your-spi-bus-name>"
  }
]
{
  "components": [
    {
      "model": "pi",
      "name": "your-board",
      "type": "board",
      "attributes": {
        "analogs": [
          {
            "chip_select": "24",
            "name": "current",
            "pin": "1",
            "spi_bus": "main"
          },
          {
            "chip_select": "24",
            "name": "pressure",
            "pin": "0",
            "spi_bus": "main"
          }
        ],
        "spis": [
          {
            "bus_select": "0",
            "name": "main"
          }
        ]
      }
    }
  ]
}

The following properties are available for analogs:

NameTypeInclusionDescription
namestringRequiredYour name for the analog reader.
pinstringRequiredThe pin number of the ADC’s connection pin, wired to the board. This should be labeled as the physical index of the pin on the ADC.
chip_selectstringRequiredThe pin number of the board’s connection pin, wired to the ADC.
spi_busstringOptionalThe name of the SPI bus connecting the ADC and board. Required if your board must communicate with the ADC with the SPI protocol.
average_over_msintOptionalDuration in milliseconds over which the rolling average of the analog input should be taken.
samples_per_secintOptionalSampling rate of the analog input in samples per second.

digital_interrupts

Interrupts are a method of signaling precise state changes. Configuring digital_interrupts to monitor GPIO pins on your board is useful when your application needs to know precisely when there is a change in GPIO value between high and low.

  • When an interrupt configured on your board processes a change in the state of the GPIO pin it is configured to monitor, it calls Tick() to record the state change and notify any interested callbacks to “interrupt” the program.
  • Calling Get() on a GPIO pin, which you can do without configuring interrupts, is useful when you want to know a pin’s value at specific points in your program, but is less precise and convenient than using an interrupt.

Integrate digital_interrupts into your robot in the attributes of your board by adding the following to your board’s JSON configuration:

// "attributes": { ... ,
"digital_interrupts": [
  {
    "name": "<your-digital-interrupt-name>",
    "pin": "<pin-number>"
  }
]
{
  "components": [
    {
      "model": "pi",
      "name": "your-board",
      "type": "board",
      "attributes": {
        "digital_interrupts": [
          {
            "name": "your-interrupt-1",
            "pin": "15"
          },
          {
            "name": "your-interrupt-2",
            "pin": "16"
          }
        ]
      }
    }
  ]
}

The following properties are available for digital_interrupts:

NameTypeInclusionDescription
namestringRequiredYour name for the digital interrupt.
pinstringRequiredThe pin number of the board’s GPIO pin that you wish to configure the digital interrupt for.
typestringOptional
  • basic: Recommended. Tracks interrupt count.
  • servo: For interrupts configured for a pin controlling a servo. Tracks pulse width value.

spis

Serial Peripheral Interface (SPI) is a serial communication protocol that uses four signal wires to exchange information between a controller and peripheral devices:

  • Main Out/Secondary In: MOSI
  • Main In/Secondary Out: MISO
  • Clock, an oscillating signal line: SCLK
  • Chip Select, with 1 line for each peripheral connected to controller: CS*

To connect your board (controller) and a component that requires SPI communication (peripheral device), wire a connection between CS and MOSI/MISO/SLCK pins on the board and component.

As supported boards have CS pins internally configured to correspond with SPI bus indices, you can enable this connection in your board’s configuration by specifying the index of the bus at your CS pin’s location and giving it a name.

Integrate spis into your robot in the attributes of your board by adding the following to your board’s JSON configuration:

// "attributes": { ... ,
"spis": [
  {
    "name": "<your-bus-name>",
    "bus_select": "<your-bus-index>"
  }
]
"spis": [
  {
    "name": "main",
    "bus_select": "0"
  }
]

The following properties are available for spis:

NameTypeInclusionDescription
namestringRequiredThe name of the SPI bus.
bus_selectstringRequiredThe index of the SPI bus.

i2cs

The Inter-Integrated circuit (I2C) serial communication protocol is similar to SPI, but requires two signal wires to exchange information between a controller and a peripheral device:

  • Serial Data: SDA
  • Serial Clock: SCL

To connect your board (controller) and a component that requires I2C communication (peripheral device), wire a connection between SDA and SCL pins on the board and component.

As supported boards have SDA and SCL pins internally configured to correspond with I2C bus indices, you can enable this connection in your board’s configuration by specifying the index of the bus and giving it a name.

Integrate i2cs into your robot in the attributes of your board as follows:

// "attributes": { ... ,
{
  "i2cs": [
    {
      "name": "<your-bus-name>",
      "bus": "<your-bus-index>"
    }
  ]
}
// "attributes": { ... ,
{
  "i2cs": [
    {
      "name": "bus1",
      "bus": "1"
    }
  ]
}

The following properties are available for i2cs:

NameTypeInclusionDescription
namestringRequiredname of the I2C bus.
bus_selectintRequiredThe index of the I2C bus.

Control your board with Viam’s client SDK libraries

To get started using Viam’s SDKs to connect to and control your robot, go to your robot’s page on the Viam app, navigate to the Code sample tab, select your preferred programming language, and copy the sample code generated.

When executed, this sample code will create a connection to your robot as a client. Then control your robot programmatically by getting your board component from the robot with FromRobot and adding API method calls, as shown in the following examples.

These examples assume you have a board called “my_board” configured as a component of your robot. If your board has a different name, change the name in the code.

Be sure to import the board package for the SDK you are using:

from viam.components.board import Board
import (
  "go.viam.com/rdk/components/board"
)

API

The board component supports the following methods:

Method NameDescription
AnalogReaderByNameGet an AnalogReader by name.
DigitalInterruptByNameGet a DigitalInterrupt by name.
GPIOPinByNameGet a GPIOPin by its pin number.
AnalogReaderNamesGet the name of every AnalogReader.
DigitalInterruptNamesGet the name of every DigitalInterrupt.
StatusGet the current status of this board.
ModelAttributesGet the attributes related to the model of this board.
SetPowerModeSet the board to the indicated power mode.
DoCommandSend or receive model-specific commands.

Additionally, the nested GPIOPin, AnalogReader, and DigitalInterrupt interfaces support the following methods:

GPIOPin API:

Method NameDescription
SetSet the output of this pin to high/low.
GetGet if this pin is active (high).
GetPWMGet the pin’s pulse-width modulation duty cycle.
SetPWMSet the pin’s pulse-width modulation duty cycle.
PWMFreqGet the pulse-width modulation frequency of this pin.
SetPWMFreqSet the pulse-width modulation frequency of this pin.

AnalogReader API:

Method NameDescription
ReadRead the current integer value of the digital signal output by the ADC.

DigitalInterrupt API:

Method NameDescription
ValueGet the current value of this interrupt.
TickRecord an interrupt.
AddCallbackAdd a channel as a callback for Tick().
AddPostProcessorAdd a PostProcessor function for Value().

AnalogReaderByName

Get an AnalogReader by name.

Parameters:

  • name (str): Name of the analog reader you want to retrieve.

Returns:

  • (AnalogReader): An interface representing an analog reader configured and residing on the board.

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the AnalogReader "my_example_analog_reader".
reader = await my_board.analog_reader_by_name(name="my_example_analog_reader")

Parameters:

Returns:

  • (AnalogReader): An interface representing an analog reader configured and residing on the board.
  • (bool): True if there was an analog reader of this name found on your board.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the AnalogReader "my_example_analog_reader".
reader, err := myBoard.AnalogReaderByName("my_example_analog_reader")

DigitalInterruptByName

Get an DigitalInterrupt by name.

Parameters:

  • name (str): Name of the digital interrupt you want to retrieve. Set as the "name" property in configuration.

Returns:

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt = await my_board.digital_interrupt_by_name(
    name="my_example_digital_interrupt")

Parameters:

Returns:

  • (DigitalInterrupt): An interface representing a configured interrupt on the board.
  • (bool): True if there was a digital interrupt of this name found on your board.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt, ok := myBoard.DigitalInterruptByName("my_example_digital_interrupt")

GPIOPinByName

Get a GPIOPin by pin number.

Parameters:

  • name (str): Pin number of the GPIO pin you want to retrieve as a GPIOPin interface. Refer to the pinout diagram and data sheet of your board model for pin numbers.

Returns:

  • (GPIOPin): An interface representing an individual GPIO pin on the board.

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.GPIO_pin_by_name(name="15")

Parameters:

Returns:

  • (GPIOPin): An interface representing an individual GPIO pin on the board.
  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the GPIOPin with pin number 15.
pin, err := myBoard.GPIOPinByName("15")

AnalogReaderNames

Get the name of every AnalogReader configured and residing on the board.

Parameters:

  • None

Returns:

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the name of every AnalogReader configured on the board.
names = await my_board.analog_reader_names()

Parameters:

  • None

Returns:

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the name of every AnalogReader configured on the board.
names := myBoard.AnalogReaderNames()

DigitalInterruptNames

Get the name of every DigitalInterrupt configured on the board.

Parameters:

  • None

Returns:

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the name of every DigitalInterrupt configured on the board.
names = await my_board.digital_interrupt_names()

Parameters:

  • None

Returns:

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the name of every DigitalInterrupt configured on the board.
names := myBoard.DigitalInterruptNames()

Status

Get the current status of the board as a BoardStatus.

Parameters:

  • extra (Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
  • timeout (Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the current status of the board.
status = await my_board.status()

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the current status of the board.
err := myBoard.Status(context.Background(), nil)

ModelAttributes

Get the attributes related to the model of this board.

Parameters:

  • None

Returns:

  • (Attributes): Attributes related to the model of this board. Will include the board’s innate remote attribute, which is not specified in configuration and is a bool indicating whether this model of board is accessed over a remote connection like gRPC.

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the attributes related to the model of this board.
attributes = await my_board.model_attributes()

Parameters:

  • None

Returns:

  • (ModelAttributes): Attributes related to the model of this board. Will include the board’s innate remote attribute, which is not specified in configuration and is a bool indicating whether this model of board is accessed over a remote connection like gRPC.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the attributes related to the model of this board.
attributes := myBoard.ModelAttributes()

SetPowerMode

Set the board to the indicated PowerMode.

Parameters:

  • mode (PowerMode): Options to specify power usage of the board: PowerMode.POWER_MODE_UNSPECIFIED, PowerMode.POWER_MODE_NORMAL, and PowerMode.POWER_MODE_OFFLINE_DEEP.
  • extra (Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
  • duration (Optional[datetime.timedelta]): If provided, the board will exit the given power mode after the specified duration.
  • timeout (Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • None

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Set the power mode of the board to OFFLINE_DEEP.
status = await my_board.set_power_mode(mode=PowerMode.POWER_MODE_OFFLINE_DEEP)

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • mode (PowerMode): Options to specify power usage of the board: boardpb.PowerMode_POWER_MODE_UNSPECIFIED, boardpb.PowerMode_POWER_MODE_NORMAL, and boardpb.PowerMode_POWER_MODE_OFFLINE_DEEP.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.
  • duration (*time.Duration): If provided, the board will exit the given power mode after the specified duration.

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Set the power mode of the board to OFFLINE_DEEP.
err := myBoard.Status(context.Background(), nil)
myBoard.SetPowerMode(context.Background(), boardpb.PowerMode_POWER_MODE_OFFLINE_DEEP, nil)

DoCommand

Execute model-specific commands that are not otherwise defined by the component API. For built-in models, model-specific commands are covered with each model’s documentation. If you are implementing your own board and add features that have no built-in API method, you can access them with DoCommand.

Parameters:

Returns:

my_board = Board.from_robot(robot=robot, name="my_board")

my_command = {
  "command": "dosomething",
  "someparameter": 52
}

await my_board.do_command(my_command)

For more information, see the Python SDK Docs.

Parameters:

Returns:

myBoard, err := board.FromRobot(robot, "my_board")

resp, err := myBoard.DoCommand(ctx, map[string]interface{}{"command": "dosomething", "someparameter": 52})

For more information, see the Go SDK Code.

GPIOPin API

Set

Set the digital signal output of this pin to low (0V) or high (active, >0V).

Parameters:

  • high (bool): If true, set the state of the pin to high. If false, set the state of the pin to low.
  • extra (Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
  • timeout (Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • None

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.GPIO_pin_by_name(name="15")

# Set the pin to high.
await pin.set(high="true")

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • high (bool): If true, set the state of the pin to high. If false, set the state of the pin to low.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the GPIOPin with pin number 15.
pin, err := myBoard.GPIOPinByName("15")

// Set the pin to high.
err := pin.Set(context.Background(), "true", nil)

Get

Get if the digital signal output of this pin is high (active, >0V).

Parameters:

  • extra (Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
  • timeout (Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (bool): If true, the state of the pin is high. If false, the state of the pin is low.

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.GPIO_pin_by_name(name="15")

# Get if it is true or false that the state of the pin is high.
high = await pin.get()

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (bool): If true, the state of the pin is high. If false, the state of the pin is low.
  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the GPIOPin with pin number 15.
pin, err := myBoard.GPIOPinByName("15")

// Get if it is true or false that the state of the pin is high.
high := pin.Get(context.Background(), nil)

GetPWM

Get the pin’s pulse-width modulation (PWM) duty cycle: a float [0.0, 1.0] representing the percentage of time the digital signal output by this pin is in the high state (active, >0V) relative to the interval period of the PWM signal (interval period being the mathematical inverse of the PWM frequency).

Parameters:

  • extra (Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
  • timeout (Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (float): A float [0.0, 1.0] representing the percentage of time the digital signal output by this pin is in the high state relative to the interval period of the PWM signal.

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.GPIO_pin_by_name(name="15")

# Get if it is true or false that the state of the pin is high.
duty_cycle = await pin.get_pwm()

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (float64): A float [0.0, 1.0] representing the percentage of time the digital signal output by this pin is in the high state relative to the interval period of the PWM signal.
  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the GPIOPin with pin number 15.
pin, err := myBoard.GPIOPinByName("15")

// Get if it is true or false that the state of the pin is high.
duty_cycle := pin.PWM(context.Background(), nil)

SetPWM

Set the pin’s Pulse-width modulation (PWM) duty cycle: a float [0.0, 1.0] indicating the percentage of time the digital signal output of this pin is in the high state (active, >0V) relative to the interval period of the PWM signal (interval period being the mathematical inverse of the PWM frequency).

Parameters:

  • cycle (float64): A float [0.0, 1.0] representing the percentage of time the digital signal output by this pin is in the high state relative to the interval period of the PWM signal.
  • extra (Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
  • timeout (Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (bool): If true, the state of the pin is high. If false, the state of the pin is low.

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.GPIO_pin_by_name(name="15")

# Set the duty cycle to .6, meaning that this pin will be in the high state for
# 60% of the duration of the PWM interval period.
await pin.set_pwm(cycle=.6)

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • cycle (float64): A float [0.0, 1.0] representing the percentage of time the digital signal output by this pin is in the high state relative to the interval period of the PWM signal.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the GPIOPin with pin number 15.
pin, err := myBoard.GPIOPinByName("15")

// Set the duty cycle to .6, meaning that this pin will be in the high state for 60% of the duration of the PWM interval period.
err := pin.SetPWM(context.Background(), .6, nil)

PWMFreq

Get the Pulse-width modulation (PWM) frequency in Hertz (Hz) of this pin, the count of PWM interval periods per second. The optimal value for PWM Frequency depends on the type and model of component you control with the signal output by this pin. Refer to your device’s data sheet for PWM Frequency specifications.

Parameters:

  • extra (Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
  • timeout (Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (int): The PWM Frequency in Hertz (Hz) (the count of PWM interval periods per second) the digital signal output by this pin is set to.

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.GPIO_pin_by_name(name="15")

# Get the PWM frequency of this pin.
freq = await pin.get_pwm_frequency()

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (unit): The PWM Frequency in Hertz (Hz) (the count of PWM interval periods per second) the digital signal output by this pin is set to.
  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the GPIOPin with pin number 15.
pin, err := myBoard.GPIOPinByName("15")

// Get the PWM frequency of this pin.
freqHz, err := pin.PWMFreq(context.Background(), nil)

SetPWMFreq

Set the Pulse-width modulation (PWM) frequency in Hertz (Hz) of this pin, the count of PWM interval periods per second. The optimal value for PWM Frequency depends on the type and model of component you control with the PWM signal output by this pin. Refer to your device’s data sheet for PWM Frequency specifications.

Parameters:

  • extra (Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
  • timeout (Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (int): The PWM Frequency in Hertz (Hz), the count of PWM interval periods per second, to set the digital signal output by this pin to.

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.GPIO_pin_by_name(name="15")

# Set the PWM frequency of this pin to 1600 Hz.
high = await pin.set_pwm_frequency(frequency=1600)

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • freqHz (unit): The PWM Frequency in Hertz (Hz), the count of PWM interval periods per second, to set the digital signal output by this pin to.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the GPIOPin with pin number 15.
pin, err := myBoard.GPIOPinByName("15")

// Set the PWM frequency of this pin to 1600 Hz.
high := pin.SetPWMFreq(context.Background(), 1600, nil)

AnalogReader API

Read

Read the current integer value of the digital signal output by the ADC.

Parameters:

  • extra (Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
  • timeout (Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (int): The value of the digital signal output by the analog reader.

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.GPIO_pin_by_name(name="15")

# Get if it is true or false that the pin is set to high.
duty_cycle = await pin.get_pwm()

# Get the AnalogReader "my_example_analog_reader".
reader = await my_board.analog_reader_by_name(
    name="my_example_analog_reader")

# Get the value of the digital signal "my_example_analog_reader" has most
# recently measured.
reading = reader.read()

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (int): The value of the digital signal output by the analog reader.
  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the AnalogReader "my_example_analog_reader".
reader, err := myBoard.AnalogReaderByName("my_example_analog_reader")

// Get the value of the digital signal "my_example_analog_reader" has most recently measured.
reading := reader.Read(context.Background(), nil)

DigitalInterrupt API

Value

Get the current value of this interrupt. If a post processor function has been added with AddPostProcessor(), it will be applied to this value before it is returned.

Calculation of value differs between the "type" of interrupt configured:

Parameters:

  • extra (Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
  • timeout (Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt = await my_board.digital_interrupt_by_name(
    name="my_example_digital_interrupt")

# Get the amount of times this DigitalInterrupt has been interrupted with a
# tick.
count = await interrupt.value()

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt, ok := myBoard.DigitalInterruptByName("my_example_digital_interrupt")

// Get the amount of times this DigitalInterrupt has been interrupted with a tick.
count, err := interrupt.Value(context.Background(), nil)

Parameters:

  • extra (Optional[Dict[str, Any]]): Extra options to pass to the underlying RPC call.
  • timeout (Optional[float]): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (int): The RollingAverage of the time in nanoseconds between two successive low signals (pulse width) recorded by Tick(), computed over a window of size 10.

For more information, see the Python SDK Docs.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt = await my_board.digital_interrupt_by_name(
    name="my_example_digital_interrupt")

# Get the rolling average of the pulse width across each time the
# DigitalInterrupt is interrupted with a tick.
rolling_avg = await interrupt.value()

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (int64): The RollingAverage of the time in nanoseconds between two successive low signals (pulse width) recorded by Tick(), computed over a window of size 10.
  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt, ok := myBoard.DigitalInterruptByName("my_example_digital_interrupt")

// Get the rolling average of the pulse width across each time the DigitalInterrupt is interrupted with a tick.
rolling_avg, err := interrupt.Value(context.Background(), nil)

Tick

Record an interrupt and notify any channels that have been added with AddCallback().

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • high (bool): If true, the state of the pin is set to high. If false, the state of the pin is set to low.
  • now (uint64): The time that has elapsed in nanoseconds since the last time the interrupt was ticked.

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt, ok := myBoard.DigitalInterruptByName("my_example_digital_interrupt")

// Record an interrupt and notify any interested callbacks.
err := interrupt.Tick(context.Background(), true, 12345)

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • high (bool): If true, the state of the pin is set to high. If false, the state of the pin is set to low.
  • nanoseconds (uint64): The time in nanoseconds between two successive low signals (pulse width).

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt, ok := myBoard.DigitalInterruptByName("my_example_digital_interrupt")

// Record an interrupt and notify any interested callbacks.
err := interrupt.Tick(context.Background(), true, 12345)

AddCallback

Add a channel as a listener for when the state of the configured GPIO pin changes. When Tick() is called, callbacks added to an interrupt will be sent the returned value high.

Parameters:

  • callback (chan Tick): The channel to add as a listener for when the state of the GPIO pin this interrupt is configured for changes between high and low.

Returns:

  • None

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt, ok := myBoard.DigitalInterruptByName("my_example_digital_interrupt")

// Make the channel for Tick().
ch := make(chan Tick)

// Add the channel to "my_example_digital_interrupt" as a callback.
interrupt.AddCallback(ch)

AddPostProcessor

Add a PostProcessor function that takes an integer input and transforms it into a new integer value. Functions added to an interrupt will be used to modify values before they are returned by Value().

Parameters:

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

myBoard, err := board.FromRobot(robot, "my_board")

// Get the GPIOPin with pin number 15.
pin, err := myBoard.GPIOPinByName("15")

// Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt, ok := myBoard.DigitalInterruptByName("my_example_digital_interrupt")

// Create a simple post processing function calculating absolute value of integers.
MySimplePP := int64(math.Abs)

// Add "MySimplePP" as a post processor to "my_example_digital_interrupt".
interrupt.AddPostProcessor(MySimplePP)

Troubleshooting

You can find additional assistance in the Troubleshooting section.

You can also ask questions in the Community Discord and we will be happy to help.

Next Steps