Drive Servos
The outputs DO2 and DO3 can drive standard hobby servo motors — for instance to move a lick spout or a door in and out of reach. Each servo has a period register (ServoMotor2Period) and a pulse-width register (ServoMotor2Pulse), both in microseconds, and the EnableServos and DisableServos registers switch the pulse train. The examples below use the servo on DO2.
The complete workflow is shown below:
Configure the Servo
- 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. 20000 for the standard 50 Hz servo frame).
- Insert a
MulticastSubjectoperator namedBehavior Commands. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectServoMotor2PulsePayload.ServoMotor2Pulse- Set the pulse width in µs (e.g. 1500 for the center position of a standard servo).
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow and press A to load the servo configuration. Check your servo's datasheet for its period and pulse-width range.
Enable and Disable Servos
- Insert a
KeyDownoperator and set theFilterproperty toS. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectEnableServosPayload.EnableServos- SelectServoOutput2.
- Insert a
MulticastSubjectoperator namedBehavior Commands.
In a separate branch:
- Insert a
KeyDownoperator and set theFilterproperty toD. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectDisableServosPayload.DisableServos- SelectServoOutput2.
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow, press S to start driving the servo and D to stop. The servo moves to the position set by the pulse width and holds it while enabled.
Adjust the Position
While the servo is enabled, writing a new pulse width moves it immediately:
- Insert a
KeyDownoperator and set theFilterproperty toF. - 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, enable the servo with S, then press F — the servo moves to the new position.
Warning
DO2 and DO3 share their hardware timers with the PWM generator. Don't run PWM and a servo on the same output at the same time.
Last updated with:
Hardware v2.1
Firmware v3.3
Bonsai Package v0.2