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 navigate to the Create component menu. Enter a name for your sensor, select the type sensor, and select the ultrasonic model.

Click Create component.

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.


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