Forum

DIY Trigger Box for...
 
Notifications
Clear all

DIY Trigger Box for ninjaNIRS '21 and PsychoPy

3 Posts
2 Users
0 Likes
408 Views
Posts: 8
Topic starter
(@sjwest)
Active Member
Joined: 1 year ago

Hi everyone,

I have worked out a super cheap (~$30 depending on what you have on hand) way to send remote triggers to the ninjaNIRS unit using just an Arduino UNO and a few other components. This post will be brief but I have tested this on my unit and confirmed that the triggers from PsychoPy + Arduino do register in the ninjaGUI as an AUX signal. All practical/physical steps and components are mentioned here – Arduino sketch code is at the end of this post. I will add some pictures of my setup when able.

Given the dynamic nature of task triggers this setup will very likely need to be modified to work for your purposes. That said, the Arduino program shouldn’t need changing – all changes are most likely to be done in PsychoPy. This approach also works with ePrime, but I do not have access to ePrime at the moment to give specific instructions. Realistically this should work with any software that is customizable and sends triggers over serial ports.

 

Assuming you have none of the needed items, you will need to purchase the following:

1x MMCX to BNC cable -   https://www.digikey.com/en/products/detail/amphenol-rf/095-850-206-036/9655880

1x Arduino Uno and USB cable - https://store.arduino.cc/products/arduino-uno-rev3

1x Male BNC terminal block - https://www.adafruit.com/product/2888

Some jumper wires - https://www.adafruit.com/product/758

 

Arduino

Attach a wire to each terminal of the BNC block

Plug the positive into digital pin 2 on the Arduino

Plug the negative into the GND on the same side of the board

 

ninjaNIRS

Attach the MMCX connector to one of the six AUX ports on the side of the device.

 

PsychoPy

Add a “Serial” component to your routine loop that has your task trials

Set start as “Condition” and in the field use $stimulus.status == STARTED   (NOTE: you need to rename the “stimulus” part of “stimulus.status” to your actual stim name!)

The “Stop” command will be drastically different from study to study – I am just waiting on their response to send the stop trigger.

Enter your Arduino location as the “Port” field (e.g., “COM4”) – if using Windows check your device manager to confirm the port location of your Arduino

Make start data A1 and stop data A0

 

ninjaGUI

Once you are ready to open the ninjaGUI you will want to make a new config file that accounts for your accessory – for whatever reason this only works for Aux 2 on my unit, so name Aux 2 “Trigger” in your config file.

 

 

Connect all cables, open the ninjaGUI and new config file and run the task! I tested this quite a bit today and it definitely works.

If you run into any frustrating errors about not having permission to “COM4” etc., check your version of PsychoPy. I spent 4 hours trying to overcome this yesterday just to learn that PsychoPy v2022.2.5 has some gnarly bug in it that makes it unable to communicate with serial ports.

 

However, the newest version - v2023.1.0 - has corrected the issue, so make sure you’re running with that! Here is the specific installation I used for developing this: https://github.com/psychopy/psychopy/releases/tag/2023.1.0

 

Note that this code is set up to be a box with 4 BNC outputs but so far I have just used the one – you do not need to alter the code to use it with a single BNC connection

  

Arduino Sketch

/*
This is a simple program for triggering analog outputs from USB to BNC off the Arduino UNO Board. BNC pins are connected to Digital lines 2,3,4,& 5 and tied to the common ground.
*/

// pins for the 4 BNCs and the onboard LED:
const int Pin1 = 2;
const int Pin2 = 3;
const int Pin3 = 4;
const int Pin4 = 5;
const int LED = LED_BUILTIN;

String content;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(Pin1, OUTPUT);
  pinMode(Pin2, OUTPUT);
  pinMode(Pin3, OUTPUT);
  pinMode(Pin4, OUTPUT);
  digitalWrite(LED, LOW);
  digitalWrite(Pin1, LOW);
  digitalWrite(Pin2, LOW);
  digitalWrite(Pin3, LOW);
  digitalWrite(Pin4, LOW);
  content = "";
}

void loop() {
  // if there's any serial available, read it:
  digitalWrite(LED, LOW);
  while (Serial.available() > 0) {
    delay(3);
    char c = Serial.read();
    content += c;
  }
  if (content.length() > 0) {             
    Serial.println(content);
    if (content.indexOf("A1") >= 0) {
      digitalWrite(Pin1, HIGH);
      digitalWrite(LED, HIGH);
      Serial.print("A-on\n");
    }
    if (content.indexOf("A0") >= 0) {
      digitalWrite(Pin1, LOW);
      digitalWrite(LED, LOW);
      Serial.print("A-off\n");
    }
    if (content.indexOf("B1") >= 0) {
      digitalWrite(Pin2, HIGH);
      Serial.print("B-on\n");
    }
    if (content.indexOf("B0") >= 0) {
      digitalWrite(Pin2, LOW);
      Serial.print("B-off\n");
    }
    if (content.indexOf("C1") >= 0) {
      digitalWrite(Pin3, HIGH);
      Serial.print("C-on\n");
    }
    if (content.indexOf("C0") >= 0) {
      digitalWrite(Pin3, LOW);
      Serial.print("C-off\n");
    }
    if (content.indexOf("D1") >= 0) {
      digitalWrite(Pin4, HIGH);
      Serial.print("D-on\n");
    }
    if (content.indexOf("D0") >= 0) {
      digitalWrite(Pin4, LOW);
      Serial.print("D-off\n");
    }
    content = "";
  }
}








2 Replies
Posts: 306
(@dboas)
Joined: 3 years ago

@sjwest

thanks for posting this here. It looks great. I look forward to seeing your photographs of it.

I linked to your post from the main ninjaNIRS page here.

Reply
Posts: 8
Topic starter
(@sjwest)
Active Member
Joined: 1 year ago

@dboas 

Following up - here are some more details and pictures. 

First up, here is the "finished" product using a simple fiberglass enclosure I found on eBay for the Uno.

I ended up ditching the BNC terminal blocks for a standard BNC jack which fit perfectly in one of the pre-existing slots on the enclosure. 

In ninjaGUI...

If you go to do this yourself you will notice that before you launch PsychoPy the signals from the trigger box will be all over the place as pictured below.

However, once you initiate the PsychoPy task this stops and the trigger signals are as clear as can be!

 

In Homer...

Set the stim markers to be generated from your AUX2/trigger box input and set the “threshold” to 2.9 and click generate.

This worked flawlessly for my task – I generated markers for the stim onset and the fixation crosses in my task – it caught all of them without error. You may need to do some testing on the proper threshold to use in cases where you are not using an official Arduino board as voltage outputs may vary slightly.

Trying with Arduino Nano
As a note - I did also try replicating this with the Arduino Nano which has a much smaller footprint and should be able to do most anything the Uno can do. The only board I had was a knock-off one and it was much noisier/not always clear what part of the signal was a trigger pulse. This may be the reason given that the Nano has the same chip as the Uno, but alas, I haven't had the time to get a proper Nano to test it! Soon.

Reply
Share:
en_USEnglish