Skip to main content

Simple class for simplified pySerial interface for use in TTL event marking using e.g. PsychoPy.

Project description

digital-trigger

Easier sending of digital triggers/event markers/TTL signals using the Serial (pySerial) package with a Black Box Toolkit USB TTL module in Python and PsychoPy.

Install

pip install digital_trigger

Requires Python 3.8+ and pyserial (installed automatically).

In PsychoPy you can install packages in the Builder GUI by going to: Tools > Plugins and packages manager > Packages > Open PIP terminal, and run pip install digital-trigger

Usage

PsychoPy

Add the following in a 'custom code block' in the routine where your stimuli are presented. Note drag the Code Component so it sits below the stimulus component in the routine view, as PsychoPy runs them top to bottom. Consider managing the triggers using a 'trigger_line' variable in your conditions file.

Before Experiment

from digital_trigger import Trigger
port = Trigger('COM4', names=['cond_1', 'cond_2', 'stim_1', 'stim_2'])

Note you can only do this once per experiment or you will get an 'access/permission denied' error. Watch out if you insert the same routine twice as a copy.

Each Frame

if image.status == STARTED and port.is_closed(condition):
    win.callOnFlip(port.open, condition)

if image.status == FINISHED and port.is_open(condition):
    win.callOnFlip(port.close, condition)

OR, more simply:

port.sync_to_component(condition, image, win)

End Experiment

port.stop()

Python

from digital_trigger import Trigger

port = Trigger('COM4', names=['cond_1', 'cond_2', 'stim_1', 'stim_2'])

port.open('stim_1')          # turn a line on  (by name)
port.open([1, 'cond_2'])     # turn several on (numbers and names mixed)
port.close('stim_1')         # turn one off
port.close_all()             # turn everything off
port.open_lines()            # index of open lines and prints the bitmask
port.status()                # list of bools for each port if open or closed

port.stop()                  # reset all lines and close the port

Or as a context manager, which closes the port for you:

with Trigger('COM4', names=['stim_1']) as port:
    port.open('stim_1')

Issues

Finding COM port number

To find COM port number:

  • run the command python -m serial.tools.list_ports -v
  • On Windows, open Device Manager, expand "Ports (COM & LPT)", unplug and replug to see which is your device
  • On Mac run ls /dev/cu.* in Terminal and look for something like /dev/cu.usbserial-XXXX or /dev/cu.usbmodemXXXX — use the cu.* name, not tty.*.
  • On Linux, run ls /dev/ttyUSB* /dev/ttyACM* — USB-serial adapters are usually ttyUSB0
  • Arduino-style boards ttyACM0; dmesg | tail right after plugging in shows the assigned name, and you may need to add yourself to the dialout group for permission.

Make sure your COM port is set up with a latency of 1ms (or lower) and Baudrate of 115200. This can be done under Device Manager > COM port > Advanced on Windows.

Module not found on Mac

ModuleNotFoundError: No module named 'digital_trigger' error on Mac: PsychoPy 2025 issue with install path typo. Confirm by running this inside a code component in psychopy:

import sys
print(sys.executable)
for p in sys.path:
    print("  ", p)

see if package is installed to python3.1 instead of python3.10.

  • also show digital-trigger in PsychoPy's pip terminal (after running install digital-trigger) should also state that the package is installed.
  • try find ~/.psychopy3 /Applications/PsychoPy.app -name "digital_trigger*" 2>/dev/null in terminal

Either install directly using /Applications/PsychoPy.app/Contents/MacOS/python -m pip install \ --target ~/.psychopy3/packages/lib/python/site-packages \ digital-trigger or move the directory by running this in a terminal:

mv /Applications/PsychoPy.app/Contents/Resources/lib/python3.10/site-packages/digital_trigger \
   ~/.psychopy3/packages/lib/python/site-packages/

mv /Applications/PsychoPy.app/Contents/Resources/lib/python3.10/site-packages/digital_trigger-0.1.2.dist-info \
   ~/.psychopy3/packages/lib/python/site-packages/

Resources:

Dev notes

build & upload

Setup venv:

python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip build twine pytest

run test.py from root with python test.py

re-run build:

rm -rf dist                # clear old builds so nothing stale is uploaded
python3 -m build
ls dist                    # confirm the new version number is shown

Push to PyPi test with twine upload --repository testpypi dist/* Push to PyPi with twine upload dist/*

test

cd /tmp
rm -rf testenv
python3 -m venv testenv
source testenv/bin/activate
pip install -i https://test.pypi.org/simple/ \
    --extra-index-url https://pypi.org/simple/ \
    digital-trigger==0.1.2          # match the version
python -c "from digital_trigger import Trigger; t = Trigger(simulate=True, names=['stim_1']); t.open('stim_1'); print('ok', t.bitmask)"
deactivate

To install from the test repo on a new computer:

pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ digital-trigger==0.1.2

Small script to check:

from digital_trigger import Trigger
t = Trigger(simulate=True, names=['stim_1'])
t.open('stim_1')
print('ok', t.bitmask)

release

cd /path/to/project         # back to the project folder
rm -rf dist
python -m build
twine upload dist/*         # no --repository = real PyPI

OR github: Releases > Draft a new release. Choose a tag: type v0.1.3, click "Create new tag: v0.1.3 on publish". Title: v0.1.3. Description: brief notes on what changed. Click Publish release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

digital_trigger-0.1.4.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

digital_trigger-0.1.4-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file digital_trigger-0.1.4.tar.gz.

File metadata

  • Download URL: digital_trigger-0.1.4.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for digital_trigger-0.1.4.tar.gz
Algorithm Hash digest
SHA256 ad07f1740a53fa0289c2329e08b95fee418b0c1cc5bea772adb2f599d17c4374
MD5 1c6caf6550f6efcbf5b416553b0ad75a
BLAKE2b-256 c87888d70e0594c8c10c54b809f8e1b8b5e858e76b65b42919d41331ae11d505

See more details on using hashes here.

File details

Details for the file digital_trigger-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for digital_trigger-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ea80caa8f0c65cc91498c9490e7833662baaae321ece3ae5741da51c628e1030
MD5 839a34ff872a59df71db5ffb88d020dd
BLAKE2b-256 fde7b1e3444b379adc18dd303408424ae90373b139e56ccb8386563f5aeb9dc0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page