Configure an Ultrasonic Sensor (viam-micro-server)

Configure an ultrasonic sensor to integrate the HC-S204 ultrasonic distance sensor into your machine. Physically connect your sensor to your microcontroller and power both on. Then, configure the sensor:

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.

Edit and fill in the attributes as applicable to your sensor, according to the table below. Although "board" is marked as required in viam-server, it is not required for viam-micro-server usage.

{
  "trigger_pin": "<pin-number>",
  "echo_interrupt_pin": "<pin-number>"
}
{
  "trigger_pin": "15",
  "echo_interrupt_pin": "18"
}
{
  "components": [
    {
      "name": "<your-ultrasonic-sensor-name>",
      "model": "ultrasonic",
      "type": "sensor",
      "namespace": "rdk",
      "attributes": {
        "trigger_pin": "<pin-number>",
        "echo_interrupt_pin": "<pin-number>"
      },
      "depends_on": []
    }
  ]
}
{
  "components": [
    {
      "name": "my-ultrasonic-sensor",
      "model": "ultrasonic",
      "type": "sensor",
      "namespace": "rdk",
      "attributes": {
        "trigger_pin": "15",
        "echo_interrupt_pin": "18"
      },
      "depends_on": []
    }
  ]
}

The following attributes are available for ultrasonic sensors:

AttributeTypeRequired?Description
trigger_pinstringRequiredThe GPIO number of the board’s GPIO pin that you have wired to the trigger pin of your ultrasonic sensor.
echo_interrupt_pinstringRequiredThe GPIO number of the board’s GPIO pin that you have wired to the echo pin of your ultrasonic sensor. Please note that unlike the RDK ultrasonic sensor, you must not use a named pin associated with a digital interrupt configured on your board: it will not (currently) work.
timeout_msintOptionalTime to wait in milliseconds before initiating a timeout when requesting readings from your ultrasonic sensor.
Default: 50
Max: 100

Test the sensor

After you configure your sensor, open the sensor’s TEST panel on the CONFIGURE or CONTROL tabs. To access detailed readings from your sensor, click on the Get Readings button.

The sensor component in the test panel

Troubleshooting

If your sensor is not working as expected, follow these steps:

  1. Check your machine logs on the LOGS tab to check for errors.
  2. Review this sensor model’s documentation to ensure you have configured all required attributes.
  3. Check that any wires are securely attached to the correct pins, if appropriate.
  4. Click on the TEST panel on the CONFIGURE or CONTROL tab and test if you can use the sensor there.

If none of these steps work, reach out to us on the Community Discord and we will be happy to help.

Next steps

Check out the sensor API or check out one of these guides:

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.