Sending multiple triggers in one routine using serial port

OS : Win 10

PsychoPy version : V 2021.2.3

What are you trying to achieve?:

I’m trying to get Psychopy to send triggers to our brainvision EEG system through a serial port. The goal is to send triggers both when the stimuli (images) are presented and when the response key is pressed.

Below is the overview of the task in Psychopy:

I have a loop for the routine (ValenceRating) to show 1 image per iteration. Below is the condition file for this loop:


Below you can see the code I have so far in both “begin experiment” and “begin routine” tabs for triggers:

My questions:

  1. With a serial port, how can I read triggers from the condition file (is there a way to read through the trigger column when calling the triggers in Psychopy)? Right now I wrote “…” in the code in the “begin routine” tab because I didn’t know what should I placed there in order for the code to work.

  2. How to send triggers for more than one component in a routine? Right now with the code in the ‘begin routine’ tab, I expect Psychopy to send triggers for the first component in the routine. But I don’t know what to add to the code to also send triggers for the other components (for example in this task, third component (response key)) as well.

  3. Options for the possibility to integrate serial port as a component? (Similar to the parallel port component option). This will probably help us with the second question as well.

Thank you very much in advance.
Best wishes,

Hi Somi,

Thanks for your question - we’re working on making some materials to help people with this - so your questions are really helpful thankyou! In answer to your third question, yes I do think a component for serial port communication would be helpful.

I think this should work. Add a code component and in the “Begin Experiment” tab use:

import serial
port = serial.Serial('COM1')

In your Begin Routine tab use:

pulse_started = False
pulse_ended = False

in the Each frame tab use:

    if mything.status == STARTED and not pulse_started:# where "mything" refers to a visual thing you are trying to sync the trigger to (so you could have one of these for different component's in the same routine
        win.callOnFlip(port.write([0x01])) # send the trigger but only when the window flips to ensure syncronisation
        pulse_started  = True
        pulse_ended = False
        pulse_start_time = globalClock.getTime()
    if pulse_started and not pulse_ended:
        if globalClock.getTime() - pulse_start_time >= 0.005:
            pulse_ended = True

Oh and in terms of setting the trigger value through a conditions file. If you have a column with the header “trigger_value” and write what you want the trigger value to be on each trial, you should in theory be able to use this in the port.write() command. So instead of port.write([0x01]) it would be port.write([trigger_value]) - please let me know if that works!

I think that should do the trick - but please do let me know if not!!

Hope this helps

PS. to check the frame timing is write here we also highly recommend checking with a photodiode for visual stimuli if you can.

1 Like