Output trigger from computer with Arduino

How to create TTL trigger from any program using Arduino

Danielle H
4 min readNov 25, 2022

Perhaps you want to activate some hardware using a trigger from your computer. Perhaps you want to sync between a program on your computer and EEG or other multi-channel hardware. How can you do it?

That’s our project today.

Quick Arduino overview

Arduino have cheap, easily customizable boards with digital I/O pins and Analog I/O pins. In order to output a TTL trigger, we will need one of the digital I/O pins. Arduinos can also be programmed using C to perform logic, i.e. reading a value from a digital pin 3 and outputting the opposite to digital pin 4, or reading from two pins and perform the OR function and outputting to a third pin, etc. Arduino boards remember their programming, so you can program it using one computer and then move it to a second computer and it will continue to work as before.

Arduino boards connect to the computer using a USB cable, and act like a Serial Port. Some programs, like Matlab, can control Arduino boards directly, so you can read and write from/to pins using Matlab code. In addition, almost every programming language supports serial communication.

If we are using Matlab or similar, we can output the trigger using Arduino package for Matlab, and not attempt to program the arduino itself. In this case, the Arduino board merely acts as another input/output mechanism of the computer, without any internal logic.

However, if we program the Arduino using Matlab we will always need Matlab on every computer using this Arduino. On the other hand, if we program the Arduino itself and implement a simple serial protocol, we will be able to use it in different computers with a variety of programming languages. So this is what we will do here.

The plan

Here I will show how to output a trigger from an Arduino when commanded to by an external program on the computer, like Python, Matlab, Processing etc. This has two parts:

  1. The Arduino code, that receives a command, parses it and outputs a trigger
  2. The code to send a command from the computer to the Arduino
An example system

Arduino

Here also there are two parts to the code: we need to get Serial input from the computer to tell us when to output the trigger, and we need the code to output the trigger.

Arduino is easily programmed using the Arduino IDE, you can download it from here. I recommend the Arduino IDE as it is much easier to use. A simple Arduino example if you are not familiar with Arduino code is here.

Output the trigger

Let’s start with the actual trigger. If all your Arduino will be doing is outputting the trigger, then the following code will work:

This is a very simple way of creating a trigger in Arduino. However, keep in mid that while the Arduino is delaying for one second, nothing else will work. So if you wish to also acquire data from another pin, or manage trigger output from two pins, this is not a good solution.

An alternative to waiting is continuously checking if enough time has passed:

And we now have a TTL wave that runs continuously.

How do we activate a trigger only on command?

Receive commands

We want to receive data from the computer. If the computer sent “T”, we will output a trigger (HIGH for triggerTime and then back to LOW). We do this using the Serial library:

Here, we check if data is available at the serial port. If so, we read until carriage return (we can also read until newline('\n') , depending on what is convenient for you). If the data is “T”, then we set hte output to HIGH and start counting. Every loop we check if we are currently triggering, and if so, how much time has passed. If more than triggerTime has passed we set the output to LOW.

To test this code, use the Serial monitor in the Arduino IDE (Tools → Serial monitor). You can set the output to end in carriage return, and send the T (Type T and Enter). You should get in return the statement high and then a second later the statement low.

Computer side

Now we need to send the trigger command to the Arduino board. I will demonstrate the code in Python with the pyserial package (not the serial package!), but almost every programming language has a Serial Port package that can be used for sending simple commands.

You need to know which port the Arduino is connected to in order to communicate with it, in Windows this can be easily found using the Device Manager.

You can test this by running the code and seeing the high and low statements from the Arduino.

Next steps

You can create more complex commands, e.g. “T <triggerTime>” for triggers of different lengths, or add different commands to output triggers to different pins. If you have a number of output trigger pins, you can create a class that will track all the start and trigger times.

Let me know how this worked and what you used it for in the comments :)

--

--

Danielle H
Danielle H

Written by Danielle H

Physicist turned programmer, exploring many languages to build projects from neurobiology tools to abstract videos.

No responses yet