Power Sensor Component

A power sensor is a device that reports measurements of the voltage, current and power consumption in your robot’s system. Integrate this component to monitor your power levels.

Configuration

For configuration information, click on your sensor’s model:

ModelDescription
fakea digital power sensor for testing
ina219INA219 power sensor; current and power monitor
ina226INA226 power sensor; current and power monitor
renogysolar charge controller

Control your power sensor 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. Once connected, you can control your robot programmatically by adding API method calls as shown in the following examples.

These examples assume you have a power sensor called "my_power_sensor" configured as a component of your robot. If your power sensor has a different name, change the name in the code.

Import the power sensor package for the SDK you are using:

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

API

The power sensor component supports the following methods:

Method NameDescription
GetCurrentReturn the current of a specified device and whether it is AC or DC.
GetVoltageReturn the voltage of a specified device and whether it is AC or DC.
GetPowerReturn the power consumption of a specified device in watts.

GetCurrent

Return the current of a specified device and whether it is AC or DC.

Parameters:

Returns:

  • (Tuple[float, bool]): A tuple which includes a float representing the current reading in amps, and a bool indicating whether the current is AC (true) or DC (false).

For more information, see the Python SDK Docs.

# Get the current reading from the power sensor
current, is_ac = await my_power_sensor.get_current()
print("The current is ", current, " A, Is AC: ", is_ac)

Parameters:

Returns:

  • float64: The measurement of the current, represented as a 64-bit float number.
  • bool: Indicate whether current is AC (true) or DC (false).
  • error: Report any errors that might occur during operation. For a successful operation, error returns nil.

For more information, see the Go SDK Docs.

// Create a power sensor instance
myPowerSensor, err := powersensor.FromRobot(robot, "my_power_sensor")
if err != nil {
  logger.Fatalf("cannot get power sensor: %v", err)
}

// Get the current reading from device in amps
current, isAC, err := myPowerSensor.Current(context.Background(), nil)

GetVoltage

Return the voltage reading of a specified device and whether it is AC or DC.

Parameters:

Returns:

  • (Tuple[float, bool]): A float representing the current reading in amps. A bool indicating whether the voltage is AC (true) or DC (false).

For more information, see the Python SDK Docs.

# Get the voltage reading from the power sensor
    voltage, is_ac = await my_power_sensor.get_voltage()
    print("The voltage is", voltage, "V, Is AC:", is_ac)

Parameters:

Returns:

  • float64: The measurement of the voltage, represented as a 64-bit float number.
  • bool: Indicate whether voltage is AC (true) or DC (false).
  • error: Report any errors that might occur during operation. For a successful operation, error returns nil.

For more information, see the Go SDK Docs.

// Create a power sensor instance
myPowerSensor, err := powersensor.FromRobot(robot, "my_power_sensor")
if err != nil {
  logger.Fatalf("cannot get power sensor: %v", err)
}

// Get the voltage from device in volts
voltage, isAC, err := myPowerSensor.Voltage(context.Background(), nil)

GetPower

Return the power reading in watts.

Parameters:

Returns:

  • float: The measurement of the power, represented as a float.

For more information, see the Python SDK Docs.

# Get the power reading from the power sensor
    power = await my_power_sensor.get_power()
    print("The power is", power, "Watts")

Parameters:

Returns:

  • float64: The measurement of the power, represented as a 64-bit float number.
  • error: Report any errors that might occur during operation. For a successful operation, error returns nil.

For more information, see the Go SDK Docs.

// Create a power sensor instance
myPowerSensor, err := powersensor.FromRobot(robot, "my_power_sensor")
if err != nil {
  logger.Fatalf("cannot get power sensor: %v", err)
}

// Get the power measurement from device in watts
power, err := myPowerSensor.Power(context.Background(), nil)

Troubleshooting

You can find additional assistance in the Troubleshooting section.

You can also ask questions on the Viam Community Slack and we will be happy to help.