MATLAB Answers

Failure to connect Arduino to Matlab (R2019a)

27 views (last 30 days)
Justin Riley
Justin Riley on 10 Jun 2020
Answered: Justin Riley on 23 Oct 2020
Hi everyone,
I'm struggling to connect to my Arduino using the Arduino Support Package. Some background:
  • Matlab version: R2019a
  • OS: Windows 10, 64-bit
  • Arduino boards tried: Nano v3.2, Nano v3.1, Uno, Sparkfun Redboard, Chinese knock-off Nano (running Nano 3.1 bootloader)
With every board I've tried:
  • a = arduino();
  • a = arduino('COM4','Uno'); % or the respective port that the board is showing up as in the Device Manager
  • a = arduino('COM4','Nano3');
The response to a = arduino(); is always: "Cannot detect Arduino hardware. Make sure Arduino hardware is properly plugged in."
The response to the other two is always: "Cannot program board Uno/Nano3 (COM4). Please make sure the board is supported adn the port and board type are correct. For more information, see Arduino Hardware Troubleshooting."
There are all boards that I can open and program within the Arduino IDE (1.8.12).
Any guidance here?


Rohan Kale
Rohan Kale on 23 Oct 2020
Hi Justin,
Can you confirm if the boards that you had mentioned, are clone boards or genuine Arduino boards? If they are clone boards, the connection might be failing due to a bootloader issue, which can be confirmed by enabling the TraceOn option while constructing the arduino object by
a = arduino('COM4', 'Nano3', 'TraceOn', true)
The TraceOn options will enable the debug messages on the command window.
For a clone nano3 board, the workaround is to upload the old Atmega328 bootloader. For getting more information on that, you can contact the MathWorks Technical Support.
Hope it helps
Justin Riley
Justin Riley on 23 Oct 2020
Hi Rohan,
I figured it out through a lot of googling. To first answer your questions, yes it was a genuine Arduino. I experimented with genuines, clones, varying revs (3.1 -> 3.2), and all received the same failure.
Ultimately, the failure was that the default IDERoot was in a location that I did not have read/write access to. I'm not sure why that was the case, but by changing it via:
arduinoio.IDERoot = 'C:\Program Files (x86)\Arduino' all worked out.
For what it's worth, I still ended up abandoning the Arduino Support Package as it's just terribly slow in communicating. This application involved driving stepper motors; I've always driven my steppers through custom signals as then you can match the speed/torque curves of your specific motors. This requires creating a signal in the 6-10 kHz range to drive at the rotation rates I was going after (with top speed step toggles generally occuring every 140 uS --- 7.1 kHz). The checks that the package goes through (is the board connected? Are the ports open? Can I send this type of data to the ports?) slows down communication to around 45 ms per command, or 320x slower than I needed.
To work around that, I am treating my arduino just as a normal serial device. It's code is "independent" of Matlab, and listens for strings (i.e. X123Y456Z789) which it then converts into action (Go to position X = 123, Y = 456, Z = 789). It then sends a serial command back to Matlab, which the .m code will then use to call the next function. It proceeds in this ping-pong style through all actions. A bit clunky, but it's the only way I could generate the signal patterns that I needed.

Sign in to comment.

Accepted Answer

Justin Riley
Justin Riley on 23 Oct 2020
Answering my own question.
Ultimately, the failure was that the default IDERoot was in a location that I did not have read/write access to. It can be changed by the following code:
arduinoio.IDERoot = 'C:\Program Files (x86)\Arduino'


Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!