Add a power sensor | Add a component

Add a power sensor

Add a power sensor to your machine’s configuration so you can monitor voltage, current, and power consumption from the Viam app and from code.

Concepts

A power sensor component provides three methods:

Built-in models

Registry modules

Viam-maintained power-sensor modules:

Module Sensors supported
viam:texas-instruments Texas Instruments INA219 and INA226 monitors

For power sensors not covered above (Renogy controllers, other chips), search for power sensor in the Viam registry.

Steps

1. Prerequisites

2. Add a power sensor component

  1. Click the + button.
  2. Select Blocks.
  3. Search for the power sensor model that matches your hardware. Search by chip name (for example, ina219, ina226).
  4. Name it (for example, battery-monitor) and click Add to machine.

3. Configure attributes

INA219 example:

{
  "board": "my-board",
  "i2c_bus": "1",
  "i2c_address": 64
}
Attribute Type Required Description
board string Yes Name of the board component.
i2c_bus string Yes I2C bus number (typically "1").
i2c_address int No I2C address. Default: 64 (0x40) for INA219.

4. Save and test

Click Save, then expand the Test section.

Try it

Read voltage, current, and power 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 Key and ID. Then click the CONFIGURE tab, and click Details, and copy the Remote address. When you run the code below, you’ll see voltage, current, and power readings. Verify the voltage matches your power supply.

Install the Viam Python SDK in a virtual environment by following Install the Python SDK.

Save this as power_sensor_test.py:

import asyncio
from viam.robot.client import RobotClient
from viam.components.power_sensor import PowerSensor

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)

sensor = PowerSensor.from_robot(robot, "battery-monitor")

voltage, is_ac = await sensor.get_voltage()
    print(f"Voltage: {voltage:.2f}V ({'AC' if is_ac else 'DC'})")

current, is_ac = await sensor.get_current()
    print(f"Current: {current:.3f}A ({'AC' if is_ac else 'DC'})")

power = await sensor.get_power()
    print(f"Power: {power:.2f}W")

await robot.close()

if __name__ == "__main__":
    asyncio.run(main())

Run it:

python power_sensor_test.py

You should see output like:

Voltage: 12.34V (DC)
Current: 0.567A (DC)
Power: 6.99W
mkdir power-sensor-test && cd power-sensor-test
go mod init power-sensor-test
go get go.viam.com/rdk

Save this as main.go:

package main

import (
    "context"
    "fmt"

"go.viam.com/rdk/components/powersensor"
    "go.viam.com/rdk/logging"
    "go.viam.com/rdk/robot/client"
)

func main() {
    ctx := context.Background()
    logger := logging.NewLogger("power-sensor-test")

robot, err := client.New(ctx, "YOUR-MACHINE-ADDRESS", logger,
        client.WithDialOptions(client.WithEntityCredentials(
            "YOUR-API-KEY-ID",
            client.Credentials{
                Type:    client.CredentialsTypeAPIKey,
                Payload: "YOUR-API-KEY",
            }),
        ),
    )
    if err != nil {
        logger.Fatal(err)
    }
    defer robot.Close(ctx)

sensor, err := powersensor.FromProvider(robot, "battery-monitor")
    if err != nil {
        logger.Fatal(err)
    }

voltage, isAC, err := sensor.Voltage(ctx, nil)
    if err != nil {
        logger.Fatal(err)
    }
    acdc := "DC"
    if isAC {
        acdc = "AC"
    }
    fmt.Printf("Voltage: %.2fV (%s)\n", voltage, acdc)

current, isAC, err := sensor.Current(ctx, nil)
    if err != nil {
        logger.Fatal(err)
    }
    acdc = "DC"
    if isAC {
        acdc = "AC"
    }
    fmt.Printf("Current: %.3fA (%s)\n", current, acdc)

power, err := sensor.Power(ctx, nil)
    if err != nil {
        logger.Fatal(err)
    }
    fmt.Printf("Power: %.2fW\n", power)
}

Run it:

go run main.go

Troubleshooting

No readings or all zeros
Current reads zero but voltage is correct

If your power sensor is not working as expected, follow these steps:

  1. Check your machine logs on the LOGS tab to check for errors.
  2. Review this power sensor model’s documentation to ensure you have configured all required attributes.
  3. Click on the TEST panel on the CONFIGURE or CONTROL tab and test if you can use the power sensor there.

If none of these steps work, reach out to us on the Community Discord and we will be happy to help.