Configure a Raspberry Pi-Controlled Servo

Configure a pi servo to integrate a hobby servo controlled by general-purpose input/output (GPIO) pins on a Raspberry Pi board 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 servo type, then select the pi model. Enter a name or use the suggested name for your servo and click Create.

An example configuration for a pi servo in the Viam app Config Builder.

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

{
  "components": [
    {
      "name": "<your-servo-name>",
      "model": "pi",
      "type": "servo",
      "namespace": "rdk",
      "attributes": {
        "pin": "<your-pin-number>",
        "board": "<your-board-name>",
        "min": <float>,
        "max": <float>,
        "starting_position_deg": <float>,
        "hold_position": <int>,
        "max_rotation_deg": <int>
      }
    }
  }
{
  "components": [
    {
      "name": "local",
      "model": "pi",
      "type": "board",
      "namespace": "rdk"
    },
    {
      "name": "my_servo",
      "model": "pi",
      "type": "servo",
      "namespace": "rdk",
      "attributes": {
        "pin": "16",
        "board": "local"
      }
    }
  ]
}

A servo JSON config with explanatory annotations for each attribute.

The following attributes are available for pi servos:

NameTypeInclusionDescription
pinstringRequiredThe pin number of the pin the servo’s control wire is wired to on the board.
boardstringRequiredname of the board the servo is wired to.
minfloatOptionalSets a software limit on the minimum angle in degrees your servo can rotate to.
Default = 0.0
Range = [0.0, 180.0]
maxfloatOptionalSets a software limit on the maximum angle in degrees your servo can rotate to.
Default = 180.0
Range = [0.0, 180.0]
starting_position_degsfloatOptionalStarting position of the servo in degrees.
Default = 0.0
Range = [0.0, 180.0]
hold_positionbooleanOptionalIf false, power down a servo if it has tried and failed to go to a position for a duration of 500 milliseconds.
Default = true
max_rotation_degintOptionalThe maximum angle that you know your servo can possibly rotate to, according to its hardware. Refer to your servo’s data sheet for clarification. Must be greater than or equal to the value you set for max.
Default = 180

Test the servo

After you establish the connection to your servo motor, navigate to the Control tab and select the dropdown panel dedicated to the motor. Use the buttons to move the servo motor to the desired angle.

The servo 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.