Sensor Component

A sensor is a device that can measure information about the outside world. Add a sensor component to your robot to send the information the sensor measures to the computer controlling the robot.

Most robots with a sensor need at least the following hardware:

  • A board
  • Depending on your sensor’s output type (analog or digital), an analog-to-digital converter (ADC) may be necessary to allow the sensor to communicate with the board

Configuration

Supported sensor models include:

ModelDescription
fakeA model used for testing, with no physical hardware.
ultrasonicHC-S204 ultrasonic distance sensor
bme280BME280 environmental sensor
ds18b20DallasTemperature DS18B20 digital temperature sensor
power_ina219INA219 current sensor
renogyRenogy battery temperature sensor
sensirion-sht3xdSensirion SHT3x-DIS temperature and humidity sensor

You can implement a model of sensor that is not natively supported by Viam by creating and registering your own model of a sensor. This allows you to have the same access and control of the sensor through Viam as you would if it was a built-in model.

For an example of creating a custom component, see a WiFi strength sensor built with the Viam Go SDK or custom resource types implemented with the Viam Python SDK.

Control your 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. Then control your robot programmatically by adding API method calls as shown in the following examples.

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

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

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

API

The sensor component supports the following methods:

Method NameDescription
ReadingsGet the measurements or readings that this sensor provides.

Readings

Get the measurements or readings that this sensor provides.

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_sensor = Sensor.from_robot(robot=robot, name='my_sensor')

# Get the readings provided by the sensor.
readings = await my_sensor.get_readings()

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.

mySensor, err := sensor.FromRobot(robot, "my_sensor")

// Get the readings provided by the sensor.
readings, err := mySensor.Readings(context.Background(), nil)

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