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.
Note
Viam has two 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.
- 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
Configuration
Supported sensor models include:
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 Name | Description |
---|---|
Readings | Get 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:
- (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)
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 there is anything we could be doing better, please create an issue.
We're sorry about that. If you'd like to talk to us for help, please join the Community Discord. To ensure we know what's wrong with this page, you can also open an issue.