Configure an Ultrasonic Sensor

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

Navigate to the CONFIGURE tab of your machine’s page in the Viam app. Click the + icon next to your machine part in the left-hand menu and select Component. Select the sensor type, then select the ultrasonic model. Enter a name or use the suggested name for your sensor and click Create.

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

Fill in the attributes as applicable to your sensor, according to the table below.

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

The following attributes are available for ultrasonic sensors:

AttributeTypeInclusionDescription
trigger_pinstringRequiredThe pin number of the board’s GPIO pin that you have wired the ultrasonic’s trigger pin to.
echo_interrupt_pinstringRequiredThe pin number of the pin the ultrasonic’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.
boardstringRequiredThe name of the board the ultrasonic is wired to.
timeout_msintOptionalTime to wait in milliseconds before timing out of requesting to get ultrasonic distance readings.
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, click on 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.

If you notice any issues with the documentation, feel free to file an issue or edit this file.