Control Digital Outputs
The Behavior board has four general-purpose digital outputs DO0 – DO3 on the Output screw terminal. Refer to the connections article to set up a digital output on DO0, which we will use for the rest of these examples.
This article covers how to set, clear, toggle, and pulse any of these outputs in Bonsai.
The complete workflow is shown below:
Note
You can use the Breakout extension board to adapt the poke ports as regular digital input/output pins if you need more digital outputs. Refer to the Control Poke Peripheral article for more information on controlling those pins.
Set and Clear Outputs
The OutputSet and OutputClear registers turn output lines on and off. Each bit in the payload selects one output line, so a single command can drive several outputs at once.
- Insert a
KeyDownoperator and set theFilterproperty toA. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectOutputSetPayload.OutputSet- SelectDO0.
- 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- SelectOutputClearPayload.OutputClear- SelectDO0.
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow, then press A to set the DO0 line high and S to set it low.
Toggle Outputs
The OutputToggle register inverts the current state of the selected output lines.
- Insert a
KeyDownoperator and set theFilterproperty toD. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectOutputTogglePayload.OutputToggle- SelectDO0.
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow and press D repeatedly — the DO0 line inverts its state on every press.
Tip
The OutputState register works like the other output registers but writes all output lines at once: selected lines are set and every other line is cleared. Use it to drive the whole output bank to a known state in one command.
Pulse Outputs
Instead of pairing a set command with a delayed clear command, the device can time pulses itself. When an output is selected in the OutputPulseEnable register, every set command starts a pulse whose duration comes from that output's pulse-duration register (e.g. PulseDO0 for DO0, in milliseconds).
- Insert a
KeyDownoperator and set theFilterproperty toF. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectOutputPulseEnablePayload.OutputPulseEnable- SelectDO0to enable pulse mode on DO0.
- Insert a
MulticastSubjectoperator namedBehavior Commands. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectPulseDO0Payload.PulseDO0- Set the pulse duration to 500 ms.
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow, press F once to configure the pulse, then send a set command with A from Set and Clear Outputs — the DO0 line goes high for 500 ms and returns low on its own.
Tip
The poke valve outputs (SupplyPort0–SupplyPort2) boot with pulse mode already enabled and a default duration of 15 ms — see Deliver Rewards on Poke. All other outputs boot with pulse mode disabled and stay on until cleared.
Alternative: Set Outputs with Timer
You can replace KeyDown with other operators to set outputs with other triggers in Bonsai, for instance a Timer.
- Insert a
Timeroperator and set theDueTimeproperty to the number of seconds to wait before setting the output (e.g. 2 seconds). - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectOutputSetPayload.OutputSet- SelectDO0.
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow and the DO0 line goes high after 2 seconds and turns off automatically if pulse mode is enabled.
Last updated with:
Hardware v2.1
Firmware v3.3
Bonsai Package v0.2