# Sending triggers from psychopy to Brain vision triggerbox

**URL:** <https://discourse.psychopy.org/t/sending-triggers-from-psychopy-to-brain-vision-triggerbox/8952>\
**Category:** Coding\
**Created:** [September 4, 2019, 11:38am UTC](https://discourse.psychopy.org/t/sending-triggers-from-psychopy-to-brain-vision-triggerbox/8952 "2019-09-04T11:38:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiva](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@shiva](https://discourse.psychopy.org/u/shiva)\
**Post date:** [September 4, 2019, 11:38am UTC](https://discourse.psychopy.org/t/sending-triggers-from-psychopy-to-brain-vision-triggerbox/8952/1 "2019-09-04T11:38:35Z")

</div>

Hello,

I want to do an EEG experiment with Psychopy and Brainvision software (through BrainVision Triggerbox). I know how to use Builder but I am pretty amateur in coding. Since I must use a serial port for sending triggers, I could not use the Builder. My planned experiment is to playing sounds as the stimulus and sending the time of the stimulus to the EEG recording system. For a test of sending triggers, I just created a simple version of the experiment like this:

 ![11](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/4/8/48bc6757609e4102e2f96a3181d4909871f29ecd.png)

I have found this code suggested:

```auto
import serial
import time
import threading

Connected = True
PulseWidth = 0.01

def ReadThread(port):
    while Connected:
        if port.inWaiting() > 0:
            print "0x%X"%ord(port.read(1))

# Open the Windows device manager, search for the "TriggerBox VirtualSerial Port (COM6)"
# in "Ports /COM & LPT)" and enter the COM port number in the constructor.
port = serial.Serial("COM6")

# Start the read thread
thread = threading.Thread(target=ReadThread, args=(port,))
thread.start()

# Set the port to an initial state
port.write([0x00])
time.sleep(PulseWidth)

# Set Bit 0, Pin 2 of the Output(to Amp) connector
port.write([0x01])
time.sleep(PulseWidth)

# Reset Bit 0, Pin 2 of the Output(to Amp) connector
port.write([0x00])
time.sleep(PulseWidth)

# Reset the port to its default state
port.write([0xFF])
time.sleep(PulseWidth)

# Terminate the read thread
Connected = False
thread.join(1.0)

# Close the serial port
port.close()

```

But to be honest, I could not find in which section I should use it.  
I would be happy if someone helps me.

---

<div class="post-metadata">

**Author:** ![Leidy](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@Leidy](https://discourse.psychopy.org/u/Leidy)\
**Post date:** [March 16, 2020, 2:26pm UTC](https://discourse.psychopy.org/t/sending-triggers-from-psychopy-to-brain-vision-triggerbox/8952/2 "2020-03-16T14:26:51Z")

</div>

Hi Shiva,

have you got any feedback or find the way to make it work.

Thank you very much,  
Leidy

---

<div class="post-metadata">

**Author:** ![swijeakumar](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@swijeakumar](https://discourse.psychopy.org/u/swijeakumar)\
**Post date:** [January 7, 2022, 1:45pm UTC](https://discourse.psychopy.org/t/sending-triggers-from-psychopy-to-brain-vision-triggerbox/8952/3 "2022-01-07T13:45:27Z")

</div>

Hi Leidy and Shiva,

Were you able to find solutions to make this work?

Best,  
Sobana

---

<div class="post-metadata">

**Author:** ![Helen](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Helen](https://discourse.psychopy.org/u/Helen)\
**Post date:** [January 21, 2022, 6:52am UTC](https://discourse.psychopy.org/t/sending-triggers-from-psychopy-to-brain-vision-triggerbox/8952/4 "2022-01-21T06:52:08Z")

</div>

Hi,

I had the same problem and I think it worked liked that:  
(I used the builder with code)

```auto
#Begin Experiment
if expInfo['EEG'] == "y":
    import serial
    import time
    from psychopy import core, visual, event
    port = serial.Serial("COM3")

#Begin Routine
if expInfo['EEG'] == "y":
    triggerSent = False
    port.write([0])

#Each Frame
if expInfo['EEG'] == "y" and trVern1.status == STARTED and not triggerSent:
        if vernTrials.thisIndex == 0:
            port.write([10])
            triggerSent = True
        elif vernTrials.thisIndex == 1:
            port.write([11])
            triggerSent = True
        else:
            port.write([50])

```

However it’s a long time ago since I wrote this and I don’t remember exactly if it worked. Still hope it helps.

The if statement is not necessary, I only implemented it so that I can run the experiment with or without the trigger box.

Helen
