Configure an ultrasonic Sensor

Configure an ultrasonic sensor to integrate an HC-S204 ultrasonic distance sensor into your robot:

Navigate to the Config tab of your robot’s page in the Viam app. Click on the Components subtab and click Create component. Select the sensor type, then select the ultrasonic model. Enter a name for your sensor and click Create.

Creation of a ultrasonic sensor in the Viam app config builder.

Edit and fill in the attributes as applicable.

{
  "components": [
    {
      "name": "<your-ultrasonic-sensor-name>",
      "type": "sensor",
      "model": "ultrasonic",
      "attributes": {
        "trigger_pin": "<pin-number>",
        "echo_interrupt_pin": "<pin-number>",
        "board": "<your-board-name>",
        "timeout_ms": <int>
      },
      "depends_on": []
    }
  ]
}
{
  "components": [
    {
      "name": "your-ultrasonic-sensor",
      "type": "sensor",
      "model": "ultrasonic",
      "attributes": {
        "trigger_pin": "5",
        "echo_interrupt_pin": "15",
        "board": "your-board-name",
        "timeout_ms": "1000"
      },
      "depends_on": []
    }
  ]
}

The following attributes are available for ultrasonic sensors:

AttributeTypeInclusionDescription
boardstringRequiredThe name of the board the sensor is wired to.
trigger_pinstringRequiredThe pin number on the board that you have wired the sensor’s trigger pin.
echo_interrupt_pinstringRequiredThe pin number of the pin the sensor’s echo pin is wired to on the board. If you have already created a digital interrupt for this pin in the board’s configuration, use that digital interrupt’s name instead.
timeout_msintOptionalTime to wait in milliseconds before timing out of requesting to get readings from the sensor.
Default: 1000.

Test the sensor

After you configure your sensor, navigate to the Control tab and select the Sensors dropdown panel. If you have multiple sensors configured, your sensor will be listed here among others. To access detailed readings from your sensor, select the Get Readings button.

The sensor component in the control tab


Have questions, or want to meet other people working on robots? Join our Community Discord.