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.
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.
- Insert a
KeyDownoperator and set theFilterproperty toA. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectServoMotor2PeriodPayload.ServoMotor2Period- Set the period of the servo pulse train in µs (e.g.20000for a 50 Hz servo update).
- Insert a
MulticastSubjectoperator namedBehavior Commands.
In a separate branch:
- Insert a
KeyDownoperator and set theFilterproperty toS. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectServoMotor2PulsePayload.ServoMotor2Pulse- Set the pulse width in µs (e.g.1500for the center position of a standard servo, the range is typically 1000–2000 µs).
- Insert a
MulticastSubjectoperator namedBehavior 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:
- Insert a
KeyDownoperator and set theFilterproperty toD. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectEnableServosPayload.EnableServos- Select the servo output to activate (e.g.ServoOutput2).
- Insert a
MulticastSubjectoperator namedBehavior Commands.
In a separate branch:
- Insert a
KeyDownoperator and set theFilterproperty toF. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectDisableServosPayload.DisableServos- Select the servo output to deactivate (e.g.ServoOutput2).
- Insert a
MulticastSubjectoperator namedBehavior 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:
- Insert a
KeyDownoperator and set theFilterproperty toG. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectServoMotor2PulsePayload.ServoMotor2Pulse- Set a new pulse width in µs (e.g.2000).
- Insert a
MulticastSubjectoperator namedBehavior 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