Serial Connection Issue On ESP32 DevkitC V4/WROOM32-E

31 views (last 30 days)
Hi everyone, I'm facing an issue about connecting to the serial port(COM5) of my ESP32 on MATLAB. I have already installed Arduino Support Package and configured it. When I try to interface with the ESP32 on my MATLAB code I get the following error:
Unable to connect to the serialport device at port 'COM5'. Verify that a device is connected to the port, the port is not in use, and all serialport input arguments and parameter values are supported by the device. See related documentation for troubleshooting steps.
Contrary to that, when I check the available serial ports from the command line, it lists COM5 as unoccupied. Interestingly, serial connection works well when I establish it from the Arduino Explorer app. Also, there is another problem that I run into.
Another thing that bothers me: MATLAB code, on running, overwrites the code I have flashed from the Arduino IDE by uploading its own code.
It's probably the code I'm using, which is:
clc; clear;
if ~isempty(instrfind)
fclose(instrfind); %found this simple condition check on this forum
delete(instrfind);
end
delete(instrfind({'Port'},{'COM5'}));
esp = arduino("COM5","ESP32-WROOM-DevKitC","BaudRate",115200); %error received at this line
esp_serial = serialport("COM5",115200,'DataBits',8,'StopBits',1,'Parity','none');
fopen(esp_serial); esp_serial.Timeout = 50;
--The rest of the code is just the structure to use the inputs I received.--
I'll be grateful if someone can guide me. Thanks.

Answers (3)

Pratyush Swain
Pratyush Swain on 31 Dec 2024
Hi Meric,
I understand you are facing issues connecting to ESP32 and overwriting of arduino code. Here are some troubleshooting steps which can be helpful:
  1. You can try with a different port other than "COM5" and ensure you close other connections to the ESP32 before trying to connect with MATLAB.
  2. You should use the 'arduino' object or the 'serialport' object , but not simultaneously, as they can conflict with each other.
  3. To avoid overwriting arduino code and use serial communication, you can stick to using the 'serialport' object without creating an 'arduino' object.
Please refer to this following example implementation:
clc; clear;
% List available serial ports
availablePorts = serialportlist("available");
% Check if COM5 is available
if ismember("COM5", availablePorts)
try
% Create a serial port connection
esp_serial = serialport("COM5", 115200, 'DataBits', 8, 'StopBits', 1, 'Parity', 'none');
disp('Serial connection successful');
esp_serial.Timeout = 50;
fopen(esp_serial);
catch ME
disp('Failed to establish serial connection');
disp(ME.message);
end
end
Please note I have used the 'serialportlist' function to check for available ports since 'instrfind' is currently deprecated.
For more information on troubleshooting serial communication issues, please refer to https://www.mathworks.com/help/instrument/resolve-serial-port-connection-errors.html
I hope this helps.

Andrei
Andrei on 24 Feb 2025
Use delete(serialportfind) to disconnect from all existing serialport connections (instrfind is for use with legacy serial).
Also note that using arduino and serialport at the same time with the same COM port will not work since the hardware allows only one connection at the time.
If you want to run your own/custom code on the Arduino board you can communicate with it by using serialport function. If you use use arduino function, this will upload its own code to the Arduino board.

MathWorks MATLAB Hardware Team
Hello,
Kindly avoid using both the Arduino API and the Serialport API within the same MATLAB script. The serial port COM5 is a resource that can only be held by one of these APIs at a time.
For working with the ESP32 board, please use the Arduino APIs available at the following link:
Thanks
MATLAB Hardware Team
MathWorks

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!