Table of Contents

Drive Servos

The digital outputs DO2 and DO3 can drive standard servo motors, for instance, to move a lick spout or a door in and out of reach. Refer to the connections article to set up the servo motor on DO2, which we will use for the rest of these examples.

This article covers how to configure the servo, enable and disable the pulse train, and adjust the position in Bonsai.

The complete workflow is shown below. Copy and paste it into Bonsai or build each section by following the step-by-step instructions below.

Drive Servos

Configure the Servo

To configure the servo, set the period and pulse-width registers for the digital output (ServoMotor2Period and ServoMotor2Pulse for DO2) in microseconds. The period sets the refresh rate for the servo update and the pulse width sets the servo angle.

Configure the Servo

  • Insert a KeyDown operator and set the Filter property to A.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select ServoMotor2PeriodPayload.
    • ServoMotor2Period - Set the period of the servo pulse train in µs (e.g. 20000 for a 50 Hz servo update).
  • Insert a MulticastSubject operator named Behavior Commands.

In a separate branch:

  • Insert a KeyDown operator and set the Filter property to S.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select ServoMotor2PulsePayload.
    • ServoMotor2Pulse - Set the pulse width in µs (e.g. 1500 for the center position of a standard servo, the range is typically 1000–2000 µs).
  • Insert a MulticastSubject operator named Behavior Commands.

Run the workflow, press A to set the servo period and S to set the servo starting angle. The two commands are bound to separate keys, so you can reset the angle later without reconfiguring the period. Check your servo's datasheet for its period and pulse-width range if you are unsure about which values to use.

Enable and Disable Servos

Use the EnableServos and DisableServos registers to turn on and off the servo:

Enable and Disable Servos

  • Insert a KeyDown operator and set the Filter property to D.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select EnableServosPayload.
    • EnableServos - Select the servo output to activate (e.g. ServoOutput2).
  • Insert a MulticastSubject operator named Behavior Commands.

In a separate branch:

  • Insert a KeyDown operator and set the Filter property to F.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select DisableServosPayload.
    • DisableServos - Select the servo output to deactivate (e.g. ServoOutput2).
  • Insert a MulticastSubject operator named Behavior Commands.

Run the workflow, press D to enable the servo and F to stop. The servo moves to the starting position set in the previous exercise.

Adjust the Position

While the servo is enabled, writing a new pulse width moves it immediately:

Adjust the Position

  • Insert a KeyDown operator and set the Filter property to G.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select ServoMotor2PulsePayload.
    • ServoMotor2Pulse - Set a new pulse width in µs (e.g. 2000).
  • Insert a MulticastSubject operator named Behavior Commands.

Run the workflow, then press G to move the servo to a new position. Press S to move it back to the starting angle.



Last updated with:
Hardware v2.1
Firmware v3.3
Bonsai Package v0.2