External input device


How can I modify the script to make it recognize an external sensor as an input tool to respond to a task?
For example by associating it with the spacebar.
So even though the spacebar is recognized as a response input, an external sensor is pressed.



There isn’t sufficient detail here to give an answer. What is the device? How is it connected? What protocol does it use to communicate? Are specific drivers needed? Is there a Python API (if required)?

Go to the manual for the device and try to extract the technical detail needed.

The device is built with arduino; it communicate trough a serial port; To let the sensor works I have a script running on Matlab.

I don’t have any information more

OK, there are standard ways to interact with a serial port via Python. You shouldn’t need to use a Matlab script, but it would be worth reading through that to see how the communication works in this particular case, and adapt it to Python:


The latest version of PsychoPy Builder comes with a graphical serial port component but I think it only sends, rather receives data, but I could be wrong. So you are still likely to need to insert some custom code as above.

I wouldn’t bother with trying to simulate a keypress. Instead, just set some variable when the data is received, and take action accordingly. We’d need to know more to give you specific advice on that (e.g. what you want to do upon receipt of serial data).

Thank you for your reply.

I will explain what I need.

Through the sensor I am should respond to both cognitive and motor psychopy tasks.

The tasks have a list of keys to press giving the response so I would like to insert this sensor into this list, perhaps having it recognized as one of the keys already present “space” or “letter s of the keyboard” etc.

Do you think this is possible?
Do you have a line code to put in the task script to allow this?

Thanks again

As above, there is likely no value in simulating a key press – you can take action directly depending on the serial port input. And again:

We simply can’t suggest code without knowing what it is intended to do (e.g. end a routine, display some feedback, store something in the data file, etc, etc,…)


I just want to answer to task, whenever the stimulus target appears, pressing the sensor.
So, I think I should set the device as keyresp. Is it correct?

I saw the instructions for the communication between arduino and psychopy but I don’t understand if it sufficient to answer to the task.


As above: no.

Hi Michael,

Thank you again for your reply.

What do you mean by “set some variable when the data is received, and take action accordingly”?
What information do you need in order to help me?

I still don’t understand if it is possible to respond with an external sensor to a cognitive task or not.

I apologize for the continuous questions