Feeling Warm and Fuzzy

I have previously talked about developing a touch-free timer for use in surgery. The goal was to better enable a single researcher to maintain sterility during animal surgeries. I really think this is a genuine unmet need in the research world, and widespread adoption of touch-free surgery kit would be extremely beneficial, both to the researchers and to the animals.

Anyway, with the plan to expand my touch-free surgery range, I figured the next piece of kit should be a heat mat for keeping rodents warm in surgery. And again, I wanted something that can be controlled by touch-free sensors. Helpfully, Pi Hut sell a small, flexible heating pad:

Looking at the specs, it uses ~1 A of power, which is far more than we can safely run from an Arduino digital pin. In order to do this, we make use of a component called a MOSFET, which is a special kind of transistor used to amplify circuits. A MOSFET lets you use a digital signal (eg. an Arduino output pin) to fully switch a separate circuit (eg. a fully powered heat mat).

Therefore, using a MOSFET, I can control the power going to the heat mat by the digital output of the Arduino. I’ve mentioned pulse width modulation (PWM) before, and it is perfectly suited to this application. PWM will let me digitally control the amount of power going through the heat mat. And, best of all, because it’s digitally controlled, I can shift the PWM up/down with IR proximity sensors.

But, how to display the power going through the heat mat? For this, I again turned to Pi Hut, who sell a 10-segment LED bar:

Each LED in the bar is individually controlled, which means that I can set the Arduino to display an indication of the power going through the heat mat, on a scale of 1-10. Bringing it all together in a 3D printed housing, I have power up and power down proximity sensors, a power indicator bar, and a flexible heat mat that warms quickly to the extent determined by the user:

Touch-free heat mat for keeping rodents warm in surgery.

I have used this heat mat in surgery myself, and it worked really well. It heated up super quick and I could change the power of the heat mat to the temperature needed by the mouse. This piece of kit is indispensible for keeping rodents warm in surgery.

The one thing that I think it missing is an actual reading of the mouse’s temperature – I kept having to feel the surgery bed to check the temperature, which kind of defeats the purpose of being touch-free.

So, my next plan for this piece of kit is to add in a temperature sensor (whether a standalone one or one that runs through the Arduino, I have yet to figure out). Stay tuned for updates.

How to Track a Mouse

Our old locomotor tracking

One of my projects is investigating a population of neurones that controls mouse locomotor activity and food intake. In the past I have used either implantable telemetry or IR beam break cages to quantify the mice’s movement. But the telemeters, even when they’re functioning well, don’t give particularly good quantification of mouse locomotor activity, which leaves the beam break cages.

For anyone that doesn’t know, these cages are set up to have a couple of IR beams that cross the cage. Whenever the beam is broken (ie. the mouse gets in the way), this is registered by the computer. It’s quite an effective (although crude) method to quantify mouse activity. And it does so completely non-invasively. However, our current IR beam break cages have a number of drawbacks that make them unattractive:

  • They only work with some of the older open cages, and don’t work at all if the mice have any bedding in the cage (it blocks the beams)
  • The beam break cages we have available in the facility, which actually belong to one of the other lecturers (although she is happy for us to use them), are a decade or two old and were built by a previous postdoc – as such they have suffered some degradation over the years and only have partial functionality left

Anyone who reads my blog will already know what I’m about to say – with these issues I’ve raised, I decided to try and build my own set of beam break cages.

Setting up beam breaks

Right, so first step was to find some IR LED’s and sensors that I could pair across 20-30 cm of a mouse’s cage. I’ve used things like this in the past, so I know you can detect an IR signal using an LED in the ~900 nm range and a phototransistor (Figure 1A).

Luckily, I had some sat around, so I hooked them up to an Arduino, but could only detect the IR signal up to around 5 cm distance. This is obviously not enough, so after some detective work, I found some “IR Beam Break Sensors” from PiHut (Figure 1B). If those didn’t work, it would require some more complex electrical engineering to make it work. Apparently you need to use modulated signals to be sensitive enough to work over multiple metres.

Fortunately, the IR sensors from PiHut worked a treat, up to about 40 cm, which is more than enough for my purposes. The next issue was how to fix the sensors in a way that they would remain aligned in a pairing across the cage.

Aligning the sensors

For this I turned to my trusted 3D printer. After borrowing an IVC from the animal facility, I figured I could make hanging holders that would hook onto the side ridges (Figure 2).

These worked great, with the only issue that the mice tended to move their bedding around and block the direct beams. A very simple solution to this problem was to use strong neodymium magnets to “pin” the tube/bedding at one end of the cage, out of the way of the sensor beams.

Right, so now I had 2 pairs of sensors successfully attached to each mouse cage, next I needed to actually track the data in some way.

Tracking data using Arduino

It turns out that tallying IR beam crosses is easy peasy using an Arduino. The only annoyance being having to duplicate the code 24 times (ie. 2 sensors each for 12 cages). But, I still need to get the data out of the Arduino. I figured I could either hook up an SD card reader and write the data to a removable card, or hook up to a PC and download the data directly.

As I was already connecting the Arduino to my laptop, I tried that first. A little Google sleuthing found me an open source (ie. free) “terminal” program, that will happily log data that comes in over a “COM” port, such as is used by the Arduino. It was actually really easy to set up, and will log the IR beam break data in a CSV (comma separated values) format, that can be directly opened by Excel.

For ease of later data analysis, I made the program log the data in 10 second intervals. However, it will be easy to change that depending on the experimental paradigm eg. 1 or even 10 minute intervals for longer term studies over days or weeks.

Just to prove how well the system works, you can see a massive increase in activity following injection of caffeine (Figure 3A). You also get fantastic circadian activity if you record for longer time periods (Figure 3B).

Where to get it from

As always, I am making this system available on my shop, far cheaper than any commercially available system. Obviously I’ll include a copy of the data logging software with instructions of how to use it. Anyone who wants to measure mouse locomotor activity easily and cheaply, check it out.

Edit 5/5/22: I have now uploaded details of how to make this kit to Hackaday, so head over there if you want to try and build it yourself.

One Tiny Step for Man

I’ve been working on the next in my EasyTTL series. Whereas my previous iteration had additional functions and output, this time I had a single goal: make a portable optogenetics TTL driver. This means making it as small as possible and, most importantly, battery powered.

While it is possible to run an Arduino off a battery source, they are pretty big and relatively power hungry. So, I wanted to find a smaller microcontroller to use for this purpose. It is, of course, possible to design a circuit from scratch to use a microcontroller, but that is a huge amount of effort. I would only want to go to those lengths if I had a good reason, like I needed to fit it into a miniscule space, or I was intending to make hundreds.

Fortunately, others have thought the same, and helpfully produced microcontroller breakout boards. Essentially this puts the chip on a board with easily accessible pins and all the control circuitry you need for easy programming via USB, with power regulation etc. I won’t go into all the available microcontroller boards, there are loads out there.

I picked the Adafruit Trinket (Figure 1), because it is small and can be programmed using Arduino IDE, which means I don’t even need to learn any new programming languages. You can think of it as a tiny Arduino, perfect for making a simple and portable optogenetics TTL driver.

The biggest drawback of the Trinket, or any smaller and more basic microcontroller, is that I lose functions; in particular there are fewer I/O pins to connect my switches and dials to. Whereas the Arduino Uno has 14 digital I/O pins, the Trinket only has 4. Now, I obviously need the TTL output and a switch to turn the flashing on/off. I also like to have an LED indicator of the TTL being switched on, which leaves a single pin to control the flashing frequency, on times etc.

With the restriction of a single available pin to control the flashing, I can put in a toggle switch to allow the user to choose between two stimulation paradigms. I will therefore just program my two “favourites”, ie. those that I see most often in the literature or that I am most likely to use myself in the lab:

  • 10 ms flash on-time; 10 Hz frequency
  • 10 ms flash on-time; 20 Hz frequency for 1 second, then off for 3 seconds

My loyal readers will know about my dislike of the 20 Hz and higher frequencies, but as you see them so often in the literature it felt remiss not to include. So anyway, I programmed the Trinket, connected it to switches etc, and hooked the output up to an oscilloscope (Figure 2).

The timing is very good, although it runs about 100 µs fast for a 10 ms pulse, giving it a timing accuracy of 99 %. While this isn’t as good as the Arduino, it is still great, and to be honest is far better than you would ever need for an optogenetics study, either in vivo or in vitro.

Next, I printed a housing unit for the Trinket and a 9 V battery, and I also included a slide switch to cut the power and prevent battery drain when not in use. I think it looks quite smart (Figure 3).

I can’t wait to turn up somewhere with this little box, and hook it up to drive a laser or LED in an optogenetics study.

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.