List Arduinos and connect to multiple Simulink
Answers (1)
0 votes
Hi @Min,
Let me address your first question, “So I found out that 2024A version has the function to list the Arduino using arduinolist - List available Arduino hardware MATLAB (mathworks.com) this function. However, I am currently unable to get 2024a but has 2023b version so I am looking for some suggestions how to make this list automatically so I can utilize. My start idea is to list the comport using the serialportlist("available") to obtain the comport that are connected which was successful but couldn't find a way to obtain the information from the comport to detect what which Arduino is connected to which comport.Can anyone give me some suggestions?”
Please see my response to your comments below.
After reviewing the documentation for Arduino provided at the link below.
Here is how I can address your need for identifying connected Arduino devices in MATLAB 2023b:
Identify Available COM Ports
As you mentioned, you can use the command `serialportlist("available")` to get a list of all available COM ports.
comPorts = serialportlist("available");Attempt to Connect and Identify Each Device
You can iterate through the list of COM ports and attempt to create an arduino object for each port. If the connection is successful, you can retrieve the board type associated with that port. Here’s a sample script to help you achieve this:
% Get available COM ports
HcomPorts = serialportlist("available"); % Initialize an array to hold connected devices
connectedDevices = {}; % Loop through each COM port
for i = 1:length(comPorts)
try
% Attempt to create an arduino object (specify board type if known)
a = arduino(comPorts(i), 'Uno'); % Change 'Uno' to your expected board
type if necessary % If successful, store the information
connectedDevices{end + 1} = struct('Port', a.Port, 'Board', a.Board); % Clear object to avoid connection issues in next iteration
clear a;
catch ME
% Handle connection errors (optional)
fprintf('Failed to connect on %s: %s\n', comPorts(i), ME.message);
end
end % Display connected devices
disp(connectedDevices);Now, if you are unsure of which types of boards might be connected, you could modify the script to check multiple board types or handle exceptions more gracefully. After running the above code, your connectedDevices variable will contain structures with the port and board type of each connected Arduino device. Please bear in mind that the above method is effective but may require knowledge of what boards you expect to connect. If you want a more dynamic approach, consider checking against known board types using conditional statements within your loop.
Now addressing your second question, “how to connect the Simulink to these each Arduino.I am able to open the Simulink using the open_system But couldn't get each Simulink to connect to each Arduino. Is there a way to code so that the specific Simulink can connect to a specific Arduino. Any ideas please?”
Please see my response to your comments below.
After reviewing the following documentations provided below
https://m.youtube.com/watch?v=Hgtmy7pH59A&t=23
Here is how you can connect specific Simulink models to designated Arduino board by following the steps mentioned below.
Open the Desired Simulink Model
Use open_system(your_model_name) in MATLAB to open your specific Simulink model.
Configure the Hardware Settings
Go to the Modeling tab. Click on Model Settings (or Configuration Parameters). In the Hardware Implementation pane, select the appropriate Arduino board from the "Hardware board" dropdown list. This step is crucial as it makes sure that the model is set up for the correct hardware specifications.
Establish Communication
Connect your Arduino board to your computer via a USB cable. On the Hardware tab, select Run on board from the Mode section. Choose Monitor & Tune to allow real-time parameter adjustments while the model is running on the Arduino.
Deploying the Model
Click on Build, Deploy & Start. This action compiles your model and uploads it to the connected Arduino board. You should see feedback from any connected sensors or actuators in real-time through scopes in your Simulink model.
Repeat for Other Models/Boards
To connect different models to other Arduino boards, repeat steps 1-4 for each specific model and ensure you change the "Hardware board" parameter accordingly.
Now, if you encounter issues with serial communication (e.g., error messages about TCP/IP ports), check that no other applications are using those ports. Make sure that your Arduino's firmware is up-to-date; outdated firmware can lead to compatibility issues. If using multiple boards simultaneously, make sure each one is uniquely identified and connected through different USB ports. Also, consider using MATLAB’s arduino function if you want a more programmatic approach for reading and writing data without needing a full Simulink model.
Hope this answers your questions.
5 Comments
Hi @Min,
You asked, “In that case, how does Simulink model know which Due to choose? I know within the Simulink Model I can choose the comport but since I want to pass down that comport and Arduino device information from the Matlab code to Simulink Model to Automate the process. Is this possible? “
Please see my response to your comments below.
I don’t see why it would not be possible to automate the connection of multiple Arduino Duo devices to specific Simulink models using MATLAB. You can achieve this by programmatically setting the COM port for each Arduino in your MATLAB code before launching the Simulink model. Please see step by step instructions listed below.
First, use MATLAB to identify the COM ports associated with each Arduino Due. You can use the serialportlist function to list available ports as you utilized it above in your comments.
ports = serialportlist("available");
Then, try creating a mapping in MATLAB that associates each Arduino with its respective COM port.
comPorts = containers.Map({'Due_A', 'Due_B'}, {'COM3', 'COM4'});
Then, use the Simulink.Parameter object to pass the COM port information to your Simulink models. For example:
set_param('Model1', 'ArduinoPort', comPorts('Due_A'));
set_param('Model2', 'ArduinoPort', comPorts('Due_B'));
Finally, you can open and run the models programmatically.
sim('Model1');
sim('Model2');
I will also suggest reviewing the Mathworks documentation provided in my earlier comments as well. So, please try following the above mentioned steps and let me know if this helps resolve your problem.
Hi @Min,
After reviewing the documentation provided at the link below, it does not list any supported hardware for Arduino Uno but does support hardware for Arduino Due.
The error message shared in your comments indicates that the connection attempt was made on the wrong COM port (e.g., COM8 instead of COM9 for the Due) which suggests that the logic for connecting to devices may not be accurately identifying which boards are connected to which ports. Your loop structure correctly iterates through both available COM ports and known Arduino types. However, you need to make sure that you store successfully connected devices in your connectedDevices array. So, modify your code to capture successful connections and check for the correct board type before attempting to clear the object. Here’s an updated version of your code:
% Auto collect Arduino Test
% Get available COM ports
HcomPorts = serialportlist("available");
% Initialize an array to hold connected devices
connectedDevices = {};
% Known arduinos
arduino_list = ["Due", "Uno"];
% Loop through each COM port
for i = 1:length(HcomPorts)
for ii = 1:length(arduino_list)
try
% Attempt to create an arduino object (specify board
type if known)
a = arduino(HcomPorts(i), arduino_list(ii));
% If successful, add to connected devices
connectedDevices{end+1} = struct('Port', HcomPorts(i),
'Board',arduino_list(ii));
fprintf('Successfully connected to %s on %s\n',
arduino_list(ii), HcomPorts(i));
% Clear object to avoid connection issues in next iteration
clear a;
catch ME
% Handle connection errors (optional)
fprintf('Failed to connect on %s: %s\n', HcomPorts(i),
ME.message);
end
end
end
% Display connected devices disp(connectedDevices);
The revised code now stores successfully connected devices in connectedDevices, allowing you to see which boards were successfully connected and the clear a; command is executed only after a successful connection, to make sure you can debug more effectively if needed. If you have any further questions, please let me know.
Categories
Find more on I2C Devices 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!