Amy’s Microtome Counter

Yesterday something awesome happened: Amy (one of the other postdocs in the lab) came to me with an idea for a piece of kit that would help her in the lab. She found that when she was cutting brain sections on the microtome, she would sometimes zone out and forget which plate she was up to. Her request was for a microtome section counter.

The idea was to have something that would light up an indicator of which plate she needed to put the next section into. It would also require a sensor of some kind that would be activated each time she placed a section. We quickly came up with a workable idea that would use an IR sensor, and the user would sweep their hand close to the sensor after each brain section was added.

This project is just asking for an Arduino, so first thing I did was sketch out some code:

// Specify pin connections
int IR = 2;
int LED1 = 3;
int LED2 = 4;
int LED3 = 5;
int LED4 = 6;
int toggle = 7;

// Specify other variables
int count = 0;            // Counter
int maxcount = 4;         // Number of plates to count
long delayTime = 500;     // Delay time to prevent multiple activations
long lastTime = 0;        // Time stamp from last activation

void setup() {

// Specify pin setup
pinMode(IR, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(toggle, INPUT_PULLUP);

}

void loop() {

// Toggle to select number of plates being used
if(digitalRead(toggle) == LOW){
  maxcount = 3;
}
else{
  maxcount = 4;
}

// Detection by IR sensor 
if (digitalRead(IR) == LOW){
  if(millis() - delayTime > lastTime){    // Have you exceeded time since last activation?
    count++;                              // Add to counter
    lastTime = millis();                  // Specify timestamp of IR activation
  }
}

// Overflow counter back to start
if (count >= maxcount){              
  count = 0;
}

// Switch on each LED in turn depending on counter
if(count == 0){
  digitalWrite(LED1,HIGH);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,LOW);
  digitalWrite(LED4,LOW);
}

if(count == 1){
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,HIGH);
  digitalWrite(LED3,LOW);
  digitalWrite(LED4,LOW);
}

if(count == 2){
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,HIGH);
  digitalWrite(LED4,LOW);
}

if(count == 3){
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,LOW);
  digitalWrite(LED4,HIGH);
}

// Reset timer overflow
if(millis() - lastTime < 0){
  lastTime = millis() - lastTime;
}

}

The electronics is fairly straightforward:

Amy's microtome section counter schematic.

A small amount of assembly later, and we had a working prototype:

Microtome sectioning counter
Amy’s microtome counter

Amy has promised to test it. I’ll let you guys know how it goes.

A Mega Piece of Kit

I have exciting news for my loyal readers: I have finally completed the prototype for my EasyTTL Mega controller! This is a 4-channel optogenetics TTL driver, which I plan to use for in vivo optogenetics experiments.

I’ve mentioned parts of the development of this project before. But, for those who haven’t read the previous blog posts, the idea behind it was to make an optogenetics TTL driver that is simple and easy to use. I get so annoyed by the unnecessary complexity (and associated costs and difficulties) inherent in most neuroscience research equipment. As such, I have produced a massively simplified device that is controlled by the user with knobs (teehee) and switches.

The EasyTTL Mega (Figure 1) is a 4-channel optogenetics TTL driver built on an Arduino Mega core1. The Arduino Mega is a robust open-source microcontroller board with a massive array of I/O pins. I have given it four TTL outputs, each individually switchable by tactile toggle switches. The stimulation settings are determined by three dials: one for the flash duration, one for the frequency of stimulation and one for the brightness. The flash settings have been taken from a decade of optogenetics literature and experiments by yours truly, and covers 99% of flashing paradigms that I have seen.

My prototype 4-channel optogenetics TTL driver.

Being a good scientist, I needed to test the system, so I connected a couple of the outputs to an oscilloscope and turned on the pulsing. First, I checked the pulse durations (Figure 2), which are consistently accurate across the range.

Confirming accurate timing from my optogenetics TTL driver.

Next, I tested the range of flashing frequencies (Figure 3), which are also bang tidy.

Confirming accurate frequency timing from my optogenetics TTL driver.

Finally, I needed to check the brightness control (Figure 4). A quick note: the brightness control is based on pulse width modulation, which means that the laser or LED controller can be set to a constant current. However, the frequency of the PWM is 980 Hz for TTL outputs 1 and 2, but only 490 Hz for TTL outputs 3 and 4. What this means practically is that there will be a threshold flash duration below which the PWM makes the brightness unstable from one flash to the next, and this will be worse for the pins 3 and 4 which run a slower PWM.

Investigating the PWM dimming function on my 4-channel optogenetics TTL driver.

Based on my recording of the PWM outputs, the dimming control is unusable for flash durations of 1 ms on all outputs, and 2 ms or less on outputs 3 and 4. Despite my earlier misgivings, I think the 2 ms flash on outputs 1 and 2 looks fine. I’ve put the EasyTTL Mega in the shop, in case anyone wants one for their own research.

1. https://store.arduino.cc/products/Arduino-mega-2560-rev3

Fine Control

I have recently been working on a more advanced version of my EasyTTL stimulator, this time including a dimming control for the TTL output. This is crucial if we wish to control our LED brightness for optogenetics.

Quick lesson on electronics: how do we adjust the brightness of an LED? The typical answer is to say “Adjust the voltage across the LED, or adjust the resistance of the circuit”, and in the simplest terms this will change the power the LED can use. But, it’s not a reliable way to do this, because of the exponential increase in light output across a narrow voltage range (Figure 1); with the LED I’ve shown here, a voltage change of 0.5 V will take you from very low current and negligible light output to maximal output. Furthermore, LED’s get hot during use, which changes their properties and makes them produce more light at the same voltage – this is not only terrible for ensuring precise brightness output, it can also lead to runaway heating and the LED burning out.

So, how do we fix this? This is really the whole point of using a TTL control device. The LED (or laser) is set up to drive a constant current (and it’s the current which determines the actual amount of light being produced), and then you pulse a controlling input to switch the light source on and off as you please. What this means is that a TTL is a very simple digital signal, on or off, with no information about the intensity of the signal.

So, what is an easy way to control an LED brightness for optogenetics? One answer is to use Pulse Width Modulation (PWM), which produces small pulses within your signal, but faster than your eye can detect. Essentially, you’re turning the LED on and off very fast (hundreds or thousands of times a second), and you control the brightness by adjusting the relative levels of on and off. More “on” and it’s brighter, more “off” and it’s dimmer. Simples.

Here’s how I’ve put PWM control into an Arduino Uno (Figure 2). It uses a potentiometer (also known as a variable resistor), but rather than connect directly to the LED output, we connect this to an analog input on the Arduino. The Arduino can then read the value of the potentiometer and using software we can drive an appropriate PWM output on our TTL signal.

See below for the code I wrote to control the circuit. We very simply set the LED as an output and the potentiometer as an analog input (read as “sensorValue”) – the reading needs to be adjusted by a factor of 4 to take into account the change in “bit” rate; essentially it reads the analog input on a different scale than it drives PWM output. Helpfully, Arduino has a built-in PWM function called “analogWrite”, which will drive a 490 Hz PWM based on your value.

Setting up a PWM manually is of course possible, but more of a pain. However, it is something I am likely to do in the future because, as the sharp-eyed among you may have noticed, 490 Hz produces PWM widths of around 1 ms, so it only works for pulses that are longer than that. In fact, I wouldn’t want to use it for pulses much shorter than 10 ms, because it will make the brightness output unstable from one pulse to the next. However, for the immediate future, I’m not overly bothered because I never use a pulse shorter than 10 ms anyway and the PWM produces fantastic brightness control at those speeds.

I hope you’ve found this post interesting, and I would absolutely recommend any readers to buy themselves an Arduino and some components and try this out, see how easy it is to control your LED brightness for optogenetics. However, if you do want an easy-to-use TTL driver for your optogenetics system but don’t want the hassle of building one yourself, I am planning to build some myself and make them available on this website at a reasonable cost.

EDIT 3/9/21: Doing some further investigations today, I found that the Arduino Mega has 980 Hz PWM control from pins 4 and 13, so I will connect 2 of the TTL outputs from my EasyTTL Mega device to those pins instead. This means that you will have access to higher resolution PWM control from TTL outputs 1 and 2, and should work well with on-times of 5 ms, and would probably be acceptable (but not perfect) for 2 ms on-times.

It’s Hard, We Know

Simplifying optogenetics equipment

I’ve mentioned once or twice the LED-based optogenetics system I’ve been working on recently, so I thought today I would put my faithful readers out of their misery and explain what I’ve been up to.

The driving force behind it was to simplify optogenetics experiments for the user, particularly with the hardware/user interface. I was actually reminded of this again yesterday, when it took me a while to sort out the stimulation protocol on the Radiant software I use for my optogenetics experiments.

So what I wanted was an easily programmable computer hardware that I could connect various LED’s and switches to, and there was really only one answer for me: an Arduino.

The Arduino Uno

For those that don’t know, Arduino is an open-source hardware/software company that produce electronics boards for the easy programming and use of microcontrollers. Their bog-standard model is the Uno (Figure 1); it has a USB input for easy programming by a computer and pin headers so you can easily connect to the microcontroller, for 14 digital in/out pins and 6 analog in pins.

I’ll save an indepth investigation into microcontrollers for another day. For now, suffice it to say that you can connect a huge array of sensors (eg. light detectors, or even switches) and outputs (eg. LED’s), and the Arduino will control them in whichever way you programmed it to.

The Arduino Uno

Controlling optogenetics

Anyway, my goal was to generate a TTL output to drive flashing of the LED, effectively controlling optogenetics with Arduino. Essentially, I want a physical switch that I can use to turn the flashing on and off, and the Arduino will output a signal for the stimulation parameters that I program it to do.

So, my electronics layout will look something like this (Figure 2). I have a toggle switch connecting pin 0 to ground (it has an internal pull-up resistor that sets the pin high, then latching the switch changes the signal to low), a pilot LED connected to pin 1, and an output TTL from pin 2.

Simple circuit for Arduino to flash and LED.

Coding the Arduino Uno

Next, we need to write the code, otherwise the Arduino will just sit there and do nothing. Fortunately, Arduino programming software is really easy to use, and they have endless tutorials and sample code online; if you want to do something but don’t know how, just type it into Google and someone will almost certainly have done it before.

To write our code, we have three sections:

  1. Naming any values
  2. Setup, which is where you instruct the Arduino for its beginning attributes
  3. Loop, which is the main program, and the Arduino will just cycle through your code endlessly in a loop, doing exactly whatever you tell it to do

Some notes on syntax:

  • Int          allows you to specify anything by an integer
  • ;              denotes the end of each “phrase”
  • {  }          denote each section or subsection
  • //            blanks out anything after it on that line, which is useful for putting in comments that won’t affect the program

Without further ado, here’s the simple program I wrote to run the TTL:

Arduino code for controlling optogenetics

I’ve put info into the comments about what the bits of code mean, hopefully it all makes sense. I found the Arduino to be quite easy to code (which I’m pretty sure is the point of them), so I would absolutely recommend any readers to pick one up. Anyone planning in vivo or in vitro optogenetics studies should consider controlling optogenetics with Arduino.

Even if you have no specific projects in mind, I think it’s great for everyone to have at least a basic understanding of electronic circuits and coding. And you might just find that you can solve some problems much easier than you thought.

A device for controlling optogenetics

I will add here an update about a device I have made to easily control optogenetics. I added a couple of dials to allow the user to easily switch pulse on-time and frequency, and housed it with a BNC output for the TTL. I have called it the EasyTTL Uno, and it is available to purchase in my shop. Alternatively, I have made the design specs and code freely available on Hackaday.

The EasyTTL Uno provides a single channel TTL output for controlling an optogenetics laser or LED. Stimulation parameters (pulse on-time and pulse frequency) are controlled by dials, and the flashing is turned on/off with a toggle switch. It’s super-easy to use, and fully customisable if you want to set your own flashing parameters. Please do check it out.