Table of Contents

Control Digital Outputs

The Behavior board has four general-purpose digital outputs DO0DO3 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:

Control Digital Outputs

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.

Set and Clear Outputs

  • Insert a KeyDown operator and set the Filter property to A.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select OutputSetPayload.
    • OutputSet - Select DO0.
  • 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 OutputClearPayload.
    • OutputClear - Select DO0.
  • Insert a MulticastSubject operator named Behavior 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.

Toggle Outputs

  • Insert a KeyDown operator and set the Filter property to D.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select OutputTogglePayload.
    • OutputToggle - Select DO0.
  • Insert a MulticastSubject operator named Behavior 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).

Pulse Outputs

  • Insert a KeyDown operator and set the Filter property to F.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select OutputPulseEnablePayload.
    • OutputPulseEnable - Select DO0 to enable pulse mode on DO0.
  • Insert a MulticastSubject operator named Behavior Commands.
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select PulseDO0Payload.
    • PulseDO0 - Set the pulse duration to 500 ms.
  • Insert a MulticastSubject operator named Behavior 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 (SupplyPort0SupplyPort2) 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.

Set Outputs Timer

  • Insert a Timer operator and set the DueTime property to the number of seconds to wait before setting the output (e.g. 2 seconds).
  • Insert a CreateMessage operator and configure the following properties:
    • Payload - Select OutputSetPayload.
    • OutputSet - Select DO0.
  • Insert a MulticastSubject operator named Behavior 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