# Serial trigger-stimulus delay

**URL:** <https://discourse.psychopy.org/t/serial-trigger-stimulus-delay/40168>\
**Category:** Builder\
**Tags:** timing\
**Created:** [May 28, 2024, 1:57pm UTC](https://discourse.psychopy.org/t/serial-trigger-stimulus-delay/40168 "2024-05-28T13:57:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ftad](https://avatars.discourse-cdn.com/v4/letter/f/e99b99/32.png) [@ftad](https://discourse.psychopy.org/u/ftad)\
**Post date:** [May 28, 2024, 1:57pm UTC](https://discourse.psychopy.org/t/serial-trigger-stimulus-delay/40168/1 "2024-05-28T13:57:56Z")

</div>

Hello everyone,  
 ![untitled](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/7/b/7b8185aad07a26dbb546e44254347435f939b7b6.png)  
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](https://psychopy.org/hardware/serialPortInstr.html)). 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?

---

<div class="post-metadata">

**Author:** ![Kimberley\_Dundas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/kimberley_dundas/32/32322_2.png) [@Kimberley\_Dundas](https://discourse.psychopy.org/u/Kimberley_Dundas)\
**Post date:** [May 29, 2024, 10:01am UTC](https://discourse.psychopy.org/t/serial-trigger-stimulus-delay/40168/2 "2024-05-29T10:01:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ftad](https://avatars.discourse-cdn.com/v4/letter/f/e99b99/32.png) [@ftad](https://discourse.psychopy.org/u/ftad)\
**Post date:** [May 29, 2024, 10:59am UTC](https://discourse.psychopy.org/t/serial-trigger-stimulus-delay/40168/3 "2024-05-29T10:59:45Z")

</div>

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):

```auto
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](https://discourse.psychopy.org/uploads/short-url/xRguEANCSwEfDGQIMGJHY3XJylV.psyexp) (16.3 KB)
