Using serial as a "trigger"

6 views (last 30 days)
Matthew Peoples
Matthew Peoples on 7 Jan 2022
Answered: Walter Roberson on 5 Nov 2023
Hi there!
Currently in an experimental set up, I have the experimental code set to be triggered upon the press of a numerical key. However, I need to alter this so that the detection of serial imput from an external source will trigger the experiment.
Any ideas on how to do this?
Thank you!

Answers (2)

Riya on 5 Nov 2023
Hello Matthew,
As per my understanding you want to use serial as “trigger”.
Please note that to trigger your experiment based on serial input from an external source, you'll need to modify your code to listen for incoming serial data and perform the desired action when the input is received.
Below is a general outline of the steps you can follow:
1. Connect your external source (e.g., another device or sensor) to your computer using a serial communication interface (such as USB).
2. Make sure you have a serial communication library installed for your programming language of choice. For example, if you're using Arduino, you can use the Serial library.
3. Initialize the serial communication in your code, specifying the appropriate baud rate and other settings.
4. Set up a loop in your code to continuously check for incoming serial data.
5. Within the loop, use a conditional statement to detect the desired input from the external source.
6. Once the input is detected, trigger the experiment by calling the relevant function or executing the necessary code.
Here's a basic example in Arduino code to give you an idea:
void setup() {
Serial.begin(9600); // Set the baud rate to match your external source
void loop() {
if (Serial.available() > 0) {
char incomingByte =;
if (incomingByte == '1') { // Change '1' to the desired input from your external source
// Trigger your experiment here
// Call the relevant function or execute the necessary code
Remember to adapt the code to your specific programming language and requirements. This example assumes you're using Arduino, but the general concept of listening for serial input applies to other platforms as well.
Feel free to provide more details about your setup or programming language if you need further assistance!
You can refer the following article for more details:
Hope it helps.
  1 Comment
Walter Roberson
Walter Roberson on 5 Nov 2023
imaqdevice.trigger only has to do with triggering some kinds of events on image acquisition hardware, and is irrelevant for serial devices.
The looping logic you posted the code for was for triggering code executing on the Arduino side. The user instead wants to trigger MATLAB code when incoming data is deteted on the serial port

Sign in to comment.

Walter Roberson
Walter Roberson on 5 Nov 2023
You would use serialport and configureCallback with "byte",1 . When MATLAB detects 1 (in this case) byte of data on the serial port it will invoke the callback , The callback would read the byte to get it out of the buffer.


Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange



Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!