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.
Tip
Viam has three additional component types defined separately from sensor that you can use to implement sensors with specific functions:
- Movement sensors for Global Positioning System (GPS) units, inertial measurement units (IMUs), and other sensors that detect position, velocity, and acceleration.
- Power sensors for sensors that can detect voltage, current, and power consumption of connected hardware.
- Encoders for sensors that can detect speed and direction of rotation of a motor or a joint.
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
Related Services
Supported Models
To use your sensor with Viam, check whether one of the following built-in models or modular resources supports your sensor.
Add support for other models
If none of the existing models fit your use case, you can create a modular resource to add support for it.
Built-in models
For configuration information, click on the model name:
Model | Description |
---|---|
fake | A model used for testing, with no physical hardware. |
ultrasonic | The HC-S204 ultrasonic distance sensor |
bme280 | BME280 environmental sensor |
ds18b20 | DallasTemperature DS18B20 digital temperature sensor |
sensirion-sht3xd | Sensirion SHT3x-DIS temperature and humidity sensor |
Modular Resources
Search for additional sensor models that you can add from the Viam Registry:
For configuration information, click on the model name:
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.
API key and API key id
By default, the sample code does not include your robot API key and API key id. We strongly recommend that you add your API key and API key id as an environment variable and import this variable into your development environment as needed.
To show your robot’s API key and API key id in the sample code, toggle Include secret on the Code sample tab. You can also see your API key and API key id on your robot’s Security tab.
Caution
Do not share your API key or robot address publicly. Sharing this information could compromise your system security by allowing unauthorized access to your robot, or to the computer running your robot.
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 Name | Description |
---|---|
GetReadings | Get the measurements or readings that this sensor provides. |
GetGeometries | Get all the geometries associated with the sensor in its current configuration, in the frame of the sensor. |
DoCommand | Send or receive model-specific commands. |
Close | Safely shut down the resource and prevent further use. |
GetReadings
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:
- (Mapping[str, Any]): The measurements or readings that this sensor provides.
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:
- (map[string]interface{}): The measurements or readings that this sensor provides.
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)
GetGeometries
Get all the geometries associated with the sensor in its current configuration, in the frame of the sensor. The motion and navigation services use the relative position of inherent geometries to configured geometries representing obstacles for collision detection and obstacle avoidance while motion planning.
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:
- (List[Geometry]): The geometries associated with the sensor, in any order.
For more information, see the Python SDK Docs.
my_sensor = Sensor.from_robot(robot=robot, name="my_sensor")
geometries = await my_sensor.get_geometries()
if geometries:
# Get the center of the first geometry
print(f"Pose of the first geometry's centerpoint: {geometries[0].center}")
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 sensor and add features that have no built-in API method, you can access them with DoCommand
.
Parameters:
command
(Dict[str, Any]): The command to execute.
Returns:
- (Dict[str, Any]): Result of the executed command.
my_sensor = Sensor.from_robot(robot=robot, name="my_sensor")
my_calibration_routine = {
"command": "calibrate",
"offset": 273
}
await my_sensor.do_command(my_calibration_routine)
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.cmd
(map[string]interface{}): The command to execute.
Returns:
- (map[string]interface{}): Result of the executed command.
- (error): An error, if one occurred.
mySensor, err := sensor.FromRobot(robot, "my_sensor")
resp, err := mySensor.DoCommand(ctx, map[string]interface{}{"command": "calibrate", "offset": 273})
For more information, see the Go SDK Code.
Close
Safely shut down the resource and prevent further use.
Parameters:
- None
Returns:
- None
my_sensor = Sensor.from_robot(robot, "my_sensor")
await my_sensor.close()
For more information, see the Python SDK Docs.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
Returns:
- (error) : An error, if one occurred.
mySensor, err := sensor.FromRobot(robot, "my_sensor")
err := mySensor.Close(ctx)
For more information, see the Go SDK Docs.
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
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!