Sending serial triggers from PsychoPy to ETG-4000 fNIRS

2 minute read

Published:

While designing my latest experiment in PsychoPy, I realised that setting up the serial port connection is far from obvious. I wrote this tutorial so that others — and future me — don’t have to waste time reinventing the wheel.

Once I had a first version of my experiment working (looping over a WAV file), I needed to work out how to send the right trigger to the fNIRS. The PsychoPy docs do have a section on using a serial port, but even after reading it I wasn’t sure how to slot the code into my script. A quick brainstorm with the lab technician sorted it out, and the simple script below does indeed send triggers to the fNIRS: For a fuller explanation of the serial.Serial arguments, see the pySerial documentation. The first argument (‘COM1’) is the name of the serial port. The second is the baud rate, which must match the one set in the ETG-4000’s Parameter/External settings:

The last line of the script sends the signal through the serial port. Here it’s “A “ followed by Python’s string literal for a carriage return. That was one of the strings the ETG-4000 expected — it was on the list I’d set up earlier in Parameter/Stim Measurement:

The easiest way to check I was sending the right signals was the Communication Test in the External tab (see the second screenshot). Start the test, then run the Python script to confirm the serial signals are coming through.

Once the triggers work as expected, you can embed the serial-trigger code in the experiment itself, either through the GUI or the code editor. Here’s where to add it in the GUI: The next step is adding the triggers for the beginning, each stimulus/block, and the end of recording.

Here’s an example experiment that uses serial-port triggers to mark both blocks of stimuli and individual stimuli. Because of the sampling rate, I had to add a delay between the triggers marking the blocks; without it, they weren’t captured accurately. The block-section triggers also had to be sent consecutively, since they can’t be interspersed (I’m not sure whether that’s down to my settings). So AABBAA is fine, but ABABAB is not.