Serial trigger-stimulus delay

Hello everyone,
untitled
I am trying to send a trigger to an Arduino board through a serial port every time a stimulus is displayed on the screen. To program this simple experiment and send the trigger, I followed the instructions on the Psychopy webpage (Sending triggers via a Serial Port — PsychoPy v2024.1.5). To test the lag between the trigger and the stimulus, I used a photodiode. Unfortunately, I observe a substantial difference between the onset of the trigger and the visual stimulus (mean=15 ms). My monitor has a refresh rate of 144 Hz, so I should expect a significantly lower lag. Can anyone kindly suggest how to improve this result?

Hi @ftad,

Would you please be able to share your experiment so that I can take a look? Or a minimal version of it is also fine.

Thanks,

Kim

Thank you very much for your response.
I am attaching the experiment. A white circle simply appears on a black background every two seconds intermittently. To implement the trigger, I inserted this piece of code into the “Each frame” tab of the trial routine (where the circle is shown):

if polygon.status == STARTED and not stimulus_pulse_started: 
    win.callOnFlip(sendTrigger, str.encode('1'))
    stimulus_pulse_start_time = globalClock.getTime()
    stimulus_pulse_started  = True

    

if stimulus_pulse_started and not stimulus_pulse_ended:
        if globalClock.getTime() - stimulus_pulse_start_time >= 0.1:
            win.callOnFlip(port.write, str.encode('0'))
            stimulus_pulse_ended = True

After running some tests, I noticed that the delay I observe seems to be caused by a delay of 2 frames in the stimulus presentation. In fact, with a refresh rate of 144 Hz, I observe a delay of about 15-20 ms, while with 60 Hz the delay is about 40 ms. So basically delay = 2*frame_time + frame_time/2. frame_time/2 should be due to the fact that the stimulus appears at the center of the screen. I then tried delaying the trigger by 2 frames and in this case, I get an acceptable delay (about 3.5 ms). Do you think this is a valid solution or could it be improved?
triggerTest.psyexp (16.3 KB)