Arduino clearing existing connection issue

I have declared a global object arduino() and in a different function I would like to clear this connection and create another. It keeps saying that the connection to arduino is existing eventhough I use "clearvars -global a".
Error: "MATLAB connection to Uno at COM3 exists in your workspace. To create a new connection, clear the existing object."
function ClearArduino(app)
clearvars -global a
a = arduino('COM3', 'Uno'); %Error is on this line
writeDigitalPin(a, 'D11', 0);
writeDigitalPin(a, 'D12', 0);
writeDigitalPin(a, 'D13', 0);
end
function runArduinoCode(app, indicator)
clearvars -global a
a = arduino('COM3', 'Uno');
while indicator == "On"
writeDigitalPin(a, 'D11', 1);
pause(0.5)
writeDigitalPin(a, 'D11', 0);
writeDigitalPin(a, 'D12', 1);
pause(0.5)
writeDigitalPin(a, 'D12', 0);
writeDigitalPin(a, 'D13', 1);
pause(0.5)
writeDigitalPin(a, 'D13', 0);
end
clear a;
end

1 Kommentar

Laura Letellier
Laura Letellier am 28 Apr. 2021
Bearbeitet: Laura Letellier am 28 Apr. 2021
Did anyone ever find a solution for this?
My students are encountering this issue and the only way we can 100% clear the existing object is to exit MATLAB, unlpug the Arduino, replug, and restart MATLAB.
If any of the MATLAB software developers are monitoring this, please tell us how to even see the workspace when using App Designer and then give us a hint as to how to clear the connection. Since you are throwing the error you must have some way to access the associated pointer, handle, or hwind to know that the connection is unavailable. If so there must be a way for us to programmatically release it.
Thank you!

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Geoff Hayes
Geoff Hayes am 21 Jun. 2019
Bearbeitet: Geoff Hayes am 21 Jun. 2019

1 Stimme

Borian - why not define a property in your app that represents the arduino object so that all callbacks have access to it? See Share Data Within App Designer Apps for details. Presumably you would do something like
properties (Access = private)
arduinoObject = [] % arduino object
end
and your callbacks (?) would become something like
function ClearArduino(app)
if ~isempty(app.arduinoObject)
clear(app.arduinoObject);
end
app.arduinoObject = arduino('COM3', 'Uno');
writeDigitalPin(app.arduinoObject, 'D11', 0);
writeDigitalPin(app.arduinoObject, 'D12', 0);
writeDigitalPin(app.arduinoObject, 'D13', 0);
end
function runArduinoCode(app, indicator)
if ~isempty(app.arduinoObject)
clear(app.arduinoObject);
end
app.arduinoObject = arduino('COM3', 'Uno');
while indicator == "On"
writeDigitalPin(app.arduinoObject, 'D11', 1);
pause(0.5)
writeDigitalPin(app.arduinoObject, 'D11', 0);
writeDigitalPin(app.arduinoObject, 'D12', 1);
pause(0.5)
writeDigitalPin(app.arduinoObject, 'D12', 0);
writeDigitalPin(app.arduinoObject, 'D13', 1);
pause(0.5)
writeDigitalPin(app.arduinoObject, 'D13', 0);
end
end
I have no idea if the above will work but it may be something to start with.

3 Kommentare

George Aiken
George Aiken am 3 Dez. 2019
This worked for me, thanks.
Fixed the comm port error and the LEDs turn on, thanks.
franck tinkeu
franck tinkeu am 21 Apr. 2021
We Can try " clear Arduino" at the Window command
franck tinkeu
franck tinkeu am 21 Apr. 2021
We Can try it before beginning the next programm!!!

Melden Sie sich an, um zu kommentieren.

shaherbano zaidi
shaherbano zaidi am 15 Sep. 2023

0 Stimmen

I was encountering the same issue i just added "Clear all' at the top of my code.
Makenzie
Makenzie am 16 Dez. 2025

0 Stimmen

To avoid the “Error using arduino (line 156) MATLAB connection to UNO at COM3 exists in your workspace. To create a new connection clear the existing object.

Unplug and replug the arduino into the computer, instead of clicking on the arduino’s name you set up. Click set up by usb, then in the top left click hardware setup and go through all of the steps. I don’t select any special libraries. This should work. Make sure to have all of your variables cleared beforehand.

I’ve been having to do this everytime I connect my computer to the arduino even when all objects/workspace variable are cleared.

Kategorien

Produkte

Version

R2019a

Gefragt:

am 21 Jun. 2019

Beantwortet:

am 16 Dez. 2025

Community Treasure Hunt

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

Start Hunting!

Translated by