Controlling Optogenetic Stimulation via Arduino-TTL

1,769 views
Skip to first unread message

aaron.sa...@gmail.com

unread,
Apr 10, 2017, 6:28:57 PM4/10/17
to Bonsai Users
Hi everyone

Goncalo - first off, thanks for Bonsai. Its a fantastic language - quite intuitive in a large number of ways.

OK, now the problem:

I've been trying to create a custom protocol where I can track the movement of rodents and apply optogenetic stimulation. Thanks to the valuable discussions in this group, I've been able to create a protocol where I can track animal movement and trace the centroid (using code from Goncalo's earlier posts). So far, so good.

I'm using Arduino UNO to try to send a TTL signal to a Thorlabs DC2200 which will then control the 470 nm LED. My stimulus protocol will require a ROI-based detection. So, similar to what has been discussed before. And I've been successful to the point of observing the correct digital output depending on the position of the animal in the ROI. My questions are:

1. Is there a quick and easy way to verify that the signal is actually being received by the arduino? I see the RX LED blinking - From other arduino forums, I gather this is indication that it is working? But how can I visualize this on bonsai?

2. Can I use this signal as the TTL "on", followed by a "off" which I could use through a delay-->bitwiseNOT-->digitalOUT ? Would this yield a TTL signal to pulse the LED for the period the animal is in the ROI?

Please find my workflow and a screenshot attached.

Best
Aaron
Opto track w stim.png
Mouse opto track v1.1 w stim.bonsai

Gonçalo Lopes

unread,
Apr 10, 2017, 8:39:42 PM4/10/17
to aaron.sa...@gmail.com, Bonsai Users
Hi Aaron and welcome to the forums!

I'm glad to hear that you've been finding these discussions useful in implementing your own project. About the questions:

1. Indeed the RX blinking LED on the Arduino is a visual indication provided by the Firmata protocol that the board is receiving messages from the host computer. From the Bonsai side, the best visualization is the one you already have coming out of the DigitalOutput or AnalogOutput nodes. For every value displayed there, Bonsai will send it to the Arduino via the serial port. What happens after that is up to the firmware of the board and you can test it by directly checking the voltages coming out of the pins.

2. If I understand you correctly, you can indeed use the detection signal to trigger fixed interval pulses to the DigitalOutput using a fixed Delay and BitwiseNot. The important bit to decide, however, is usually how you want to deal with multiple activations of the signal. For example, if the ROI is deactivated and reactivated  again during one of these pulses, do you want to trigger a new pulse or wait for the previous one to finish?

In terms of the precision of these pulses, bear in mind that in general running a pulse through the computer timer will be slightly less precise than running it directly on the Arduino (with custom firmware or by using an external pulse generator such as PulsePal, Master 8, etc). You should definitely measure the TTL pulse intervals, for example with an external oscilloscope, to confirm whether the timing jitter is small enough for your particular application.

Hope this helps and let me know if you would like me to ellaborate on any of these ideas.


--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/d5a52642-3912-4b54-824a-fe259295d9f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

aaron.sa...@gmail.com

unread,
Apr 11, 2017, 6:33:05 PM4/11/17
to Bonsai Users, aaron.sa...@gmail.com
Hi Goncalo

Thanks for the important considerations regarding the stimulation protocol.

I've tried changing the arduino firmware as suggested in this post, but I'm not sure if I'm doing it correctly. I tried changing the code in the "StandardFirmata" folder in the Examples folder which is in the main Arduino folder by pasting in what I wanted the arduino to do. As an example, I just tried pasting in the LED example which is the first Arduino example on the Arduino comic. In red is the part I changed in the original Firmata.

void digitalWriteCallback(byte port, int value)
{
  byte pin, lastPin, pinValue, mask = 1, pinWriteMask = 0;

  if (port < TOTAL_PORTS) {
    // create a mask of the pins on this port that are writable.
    lastPin = port * 8 + 8;
    if (lastPin > TOTAL_PINS) lastPin = TOTAL_PINS;
    for (pin = port * 8; pin < lastPin; pin++) {
      if (pin == 10) {
          digitalWrite(10, HIGH);
          delay(500);
          digitalWrite(10, LOW);
          delay(1000);
        }
      }
      // do not disturb non-digital pins (eg, Rx & Tx)
      if (IS_PIN_DIGITAL(pin)) {
        // do not touch pins in PWM, ANALOG, SERVO or other modes
        if (Firmata.getPinMode(pin) == OUTPUT || Firmata.getPinMode(pin) == INPUT) {
          pinValue = ((byte)value & mask) ? 1 : 0;
          if (Firmata.getPinMode(pin) == OUTPUT) {
            pinWriteMask |= mask;
          } else if (Firmata.getPinMode(pin) == INPUT && pinValue == 1 && Firmata.getPinState(pin) != 1) {
            // only handle INPUT here for backwards compatibility
#if ARDUINO > 100
            pinMode(pin, INPUT_PULLUP);
#else
            // only write to the INPUT pin to enable pullups if Arduino v1.0.0 or earlier
            pinWriteMask |= mask;
#endif
          }
          Firmata.setPinState(pin, pinValue);
        }
      }
      mask = mask << 1;
    }
    writePort(port, (byte)value, pinWriteMask);
  }
}

Best
Aaron
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.

aaron.sa...@gmail.com

unread,
Apr 12, 2017, 1:46:01 PM4/12/17
to Bonsai Users, aaron.sa...@gmail.com
Hi Goncalo

I just used the standard method..just uploading the StandardFirmata on Arduino IDE. I havent recorded the exact delay. But I think for my purposes, this works very well.

Thanks a lot!

Best
Aaron

On Monday, April 10, 2017 at 8:39:42 PM UTC-4, goncaloclopes wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages