Communicating with external hardware using PsychoPy

PsychoPy is able to communicate with a range of external hardware, like EEG recording devices and eye trackers.

This page provides step-by-step instructions on how to communicate with some of the more commonly used hardware. The page is being updated regularly so if you don’t see your device listed here please do post in the forum as we keep an eye on commonly-faced issues (and solutions!) there.

Communicating with EEG

Before getting started with an EEG study in PsychoPy, we highly recommend reading relevant information on how to measure and understand timing. Although these guides will talk you through how to communicate with EEG hardware, they can really be used to communicate with any device that is connected via the same method:

Communicating via a Parallel Port

Step one: Set up your Parallel Port component in Builder

PsychoPy has a Parallel Port component in Builder view. This can be found in the I/O component drop down. This component supports both traditional parallel ports and USB devices.

If you’d like to use a Parallel Port to record responses (for example from a button box) please read this excellent thread from our Discourse Forum user jtseng.

../../../_images/parallel1a.png

Fig. 5 Select the Parallel Port component from the I/O or EEG component drop-down menus.

../../../_images/parallel1b.png

Fig. 6 In the Basic tab, we’ll choose to start our trigger when a condition is met by selecting condition from the Start drop down.

../../../_images/parallel1c.png

Fig. 7 Set the data to be sent by the trigger

Note

The address of the port is not the same as the name of the port. For instance, the name of the port could be “LPT 1” but the address might be “0378”.

../../../_images/parallel2.PNG

Fig. 8 Select your port from the drop down, if you don’t see it listed just follow the next step.

../../../_images/parallel3.PNG

Fig. 9 Follow these steps to add your port address, only if it was not already in the drop-down menu.

Note

The parallel port address is usually a hexadecimal address. We tell PsychoPy to read it as such by prefixing with “0x”. So if your port address appears in Device Manager as “0378-037F” for example, in PsychoPy this would be written as “0x0378”.

Step two: Make sure you have the correct drivers installed

If you’re using a Mac, it’s recommended that you skip this step. For Windows users, a common error when trying to communicate via a Parallel Port component is that certain drivers are not found. We’re going to pre-empt that error by downloading and installing the correct drivers now.

../../../_images/parallel4.PNG

Fig. 10 The correct folder to select is shown here.

Step three: Test your triggers

../../../_images/parallel5.png

Note

If you’d like to use a Parallel Port to record responses (for example from a button box) please read this excellent thread from our Discourse Forum user jtseng.

Sending triggers via a Serial Port

Note that if you are using PsychoPy version 2022.2 onwards, you may use the :ref: serial port component <serial_comp>. If you are using an earlier version you will need to use :ref: code components <serial_code>. For both use cases you will need to know your serial port address.

Find out the address of your serial port

Serial port addresses are different depending on whether you’re using a Mac or a Windows device:

If you’re using a Mac

../../../_images/terminalPorts.png

If you’re using Windows

../../../_images/deviceManager.png

Using a Serial Port Component to communicate via Serial Port

If you’re using PsychoPy version 2022.2 or later, you can use the serial port component. If you’re running an earlier version, you’ll need to use a code component (see :ref: this section <serial_code>).

_images/serial1.png

Fig. 11 Select the SerialPort component from the I/O or EEG component drop-down menus.

_images/serial2.png

Fig. 12 In the Basic tab, we’ll choose to start our trigger when a condition is met by selecting condition from the Start drop down.

_images/serial3.png

Fig. 13 Type in the address of your serial port.

_images/serial4.png

Fig. 14 What do you want PsychoPy to send at the start of your trigger pulse, and what do you want it to be reset to at the end of the pulse?

Using a Code Component to communicate via Serial Port

../../../_images/insertCode.png

Fig. 15 Select the Code component from the Custom component drop-down

Test your triggers

../../../_images/serialExp.png

Communicating with an Eyetracker

PsychoPy has components that allow you to connect and communicate with eyetrackers directly from Builder - without any code! These steps will guide you through how to set up, calibrate, and record from your eyetracker.

Step one: Know Your Eyetracker

PsychoPy supports many of the commonly used eyetrackers, you can find out if yours is supported by following these steps:

../../../_images/eyeTrackers.png

Step two: Set up your Eyetracker

When you’ve selected your eyetracker from the drop-down menu, a set of options that are specific to that device will appear, such as the model and serial number of your device. Here we will follow through with the MouseGaze options:

../../../_images/mouseGaze.png

Step three: Add Eyetracker components to your Builder experiment

You can find the eyetracker components in the eyetracker component drop-down on the right-hand side of the Builder window.

../../../_images/eyeRecord.png

Fig. 17 You can choose whether you want this component to just start your eyetracker recording, just stop the recording, or whether you want the component to start the recording and then stop it after a certain duration.

Note

If you’ve started the eyetracker recording at the start of your experiment, be sure to add in another eyetracker record component at the end of your experiment to stop the recording too!

../../../_images/eyeROI.png
../../../_images/eyeROIPos.png
../../../_images/eyeCaliBasic.png

Fig. 18 Set the basic properties of the calibration routine here.

../../../_images/eyeCaliTarget.png

Fig. 19 Set the properties of the target on this tab.

../../../_images/eyeCaliAni.png

Fig. 20 This tab allows you to set the properties of the target animation.

What about the data?

../../../_images/eyeData.png

Fig. 21 The data output will vary according to what you’ve asked PsychoPy to record about gaze.

Communicating with other devices

Recording information from an Arduino via serial port

Arduino microcontrollers are a relatively cost-effective way to record biophysical responses to stimuli, such as galvanic skin response (GSR) or heart rate. This page will guide you through how to record information from an Arduino via a serial port connection.

This guide will cover how to set up your PsychoPy experiment only - for lots of tutorials on using your Arduino, and also how to download the open-source Arduino software, take a look at the Arduino website.

Step one: Find out the address of your serial port

You can quickly find out the address of the serial port that your Arduino is connected to by opening the Arduino IDE and clicking on Tools at the top of the window, then down to Port. Here, the port that your Arduino is connected to will show the model of your Arduino next to it.

../../../_images/arduinoPort.png

Step two: Add code components to your Builder experiment

Let’s assume for this tutorial that we have a basic experiment set up where we are presenting an image stimulus to a participant, and we want to record their heart rate, via a module connected to an Arduino, during viewing.

Communicating with fMRI

Due to the haemodynamic response being comparatively sluggish relative to scalp voltage changes, fMRI studies don’t typically require sub-millisecond timing precision within a trial like EEG studies do.

However it is important that an fMRI study has consistent timing across trials so that the scanner sequence remains in sync with an experiment.

Step one: Know your Scanner!

Rather than programming your PsychoPy experiment to send triggers to some hardware in the same way as EEG, with fMRI you would want to set up your experiment so that it waits until it has detected when the scanner has sent out a trigger before moving on to present trials.

Before doing anything else, it’s important that you know how the scanner you’ll be using will emit these triggers, and whether these are converted to some other signal such as characters on a serial port or a simulated keypress. In general, there are at least 3 ways a scanner might send a trigger to your experiment:

  1. Emmulate a keypress.
  2. Via parallel port
  3. Via serial port

Step two: Create a Routine to wait for scanner triggers

A Routine to detect fMRI triggers is really simple to set up. Regardless of the method your scanner uses to send the triggers, you’ll just need a Routine that waits until it’s detected the trigger before moving on. Create a new Routine and insert a Text component that says ‘Waiting for Scanner’.

Timing in fMRI

In fMRI studies, it’s important that the scanner runs remain in sync with the experiment, especially if you are only waiting for the scanner to send a trigger once at the start of the experiment.

PsychoPy implements a feature called non-slip timing to help with this (you can find out more about what this is and why it’s important here.

If you set your trial Routines to have a definite end-point (e.g. all components within a trial Routine will end after 5 seconds), you’ll notice that the colour of the Routine in your Flow changes from blue to green. This is your indication that the Routine is making use of non-slip timing.

If you can’t set your Routines to have a fixed duration (for example if a trial ends when a participant makes a response), it’s a good idea to insert a ‘Waiting for Scanner’ Routine at the start of every trial so that you know that each trial has been synced with your scanner’s trigger.