Read digital inputs
The Behavior device has one digital input that is accessible without extra hardware: DI3 on the Input connector. Each poke port also carries a general-purpose DI line, and a DIO line that can be used as an active-low digital input. Refer to the connections article to set up the hardware connection on DI3 or DIO0, which we will use for the rest of these examples.
This article covers how to visualize the digital input events and read the poke port DIO lines as a digital input in Bonsai.
The complete workflow is shown below. Copy and paste it into Bonsai to get started immediately, or build each section by following the step-by-step instructions in the following sections.
Note
If you need more digital inputs, the Breakout provides access to the poke port input lines through screw terminals.
Visualize digital input events
The Behavior device broadcasts changes on its digital input lines as DigitalInputState event messages. In addition to DI3, this register also reports events from the DI lines on the poke ports P0–P2 (represented as DIPort0–DIPort2 in Bonsai), such as poke events from the Mice Poke peripheral or other digital inputs connected to the Breakout. The workflow below subscribes, parses, and displays them in a visualizer window.
- Insert a
SubscribeSubjectoperator namedBehavior Events. - Insert a
Parseoperator and configure theRegisterproperty toTimestampedDigitalInputState. - Insert a
VisualizerWindowoperator.
Run the workflow and set the DI3 line high, then low. The visualizer will display:
DI3@12.001504
None@12.301823
The first value is the payload, listing the digital inputs that are currently active, and the second value is the timestamp on the device clock. Both rising and falling edges generate an event, so None marks the moment the line returned low.
Visualize port DIO events
By default, the DIO line acts as an active-low digital input. It idles high at 5 V through onboard pull-up resistors, and the connected device must actively drive it low. Changes on the DIO lines are broadcast in a different register, PortDIOStateEvent, which can be visualized in the same way as the digital inputs:
- Insert a
SubscribeSubjectoperator namedBehavior Events. - Insert a
Parseoperator and configure theRegisterproperty toTimestampedPortDIOStateEvent. - Insert a
VisualizerWindowoperator.
Run the workflow and set the DIO line on P0 low, then high. The visualizer will display:
DIO1, DIO2@20.154016
DIO0, DIO1, DIO2@20.575872
The payload lists the DIO lines that are currently high. Since the lines idle high, driving DIO0 low removes it from the list, and releasing the line adds it back.
Documentation for release v3.4.0