-1

I am trying to interface an Arduino Nano CH340 with Matlab but keep getting this error:

Error using ArduinoMatlabExampleYT (line 11)
Cannot program board Nano33BLE (COM3). Please make sure the board is supported and the port and board type are correct. For more information, see Arduino Hardware Troubleshooting.

I tried Nano3, Nano33IoT, and Nano33BLE, but all failed. below is the basic code in Matlab.

Also, the Arduino package is installed.

clear
clc
close all

ledPin = 'D13';
deltaT_blink = 0.5;

port = 'COM3';
board = 'Nano3';

a = arduino(port, board);

for k=1:10
    a.writeDigitalPin(ledPin,0);
    pause(deltaT_blink/2);
    
    a.writeDigitalPin(ledPin,1);
    pause(deltaT_blink/2);
end

Am I doing something wrong?

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Pix
  • 1
  • 1
  • Which tutorial are you following? – Gabriel Staples Feb 20 '22 at 14:20
  • 1
    Please copy paste the code into the question as text, not an image. – Gabriel Staples Feb 20 '22 at 14:21
  • 1
    @GabrielStaples https://www.youtube.com/watch?v=8NQ1h0gGgX8 – Pix Feb 20 '22 at 14:30
  • Nano33IoT and Nano33BLE are completely different boards from Nano with CH340. – gre_gor Feb 20 '22 at 14:32
  • I suspect you have the wrong board name. None of `Nano3`, `Nano33IoT`, and `Nano33BLE` seem correct for the basic `nano` board. – Gabriel Staples Feb 20 '22 at 14:32
  • And you error says "Nano33BLE" but your code has "Nano3". – gre_gor Feb 20 '22 at 14:32
  • @GabrielStaples I tried those 3 as when I checked with MathWorks those are the 3 that somehow relate with Nano. https://www.mathworks.com/help/supportpkg/arduinoio/ref/arduino.html – Pix Feb 20 '22 at 14:36
  • @gre_gor yes you're right, that's because I tried with all three of them, Nano3, Nano33IoT, and Nano33BLE, and all gave me the same error. (obviously with instead on Nano33BLE it mentioned the others) – Pix Feb 20 '22 at 14:38
  • @GabrielStaples ["Nano3" refers to "Arduino Nano 3.1"](https://www.mathworks.com/help/supportpkg/arduinoio/ref/arduino.html#mw_c00e99d2-1165-443e-8dce-595596a641a4) which is the basic Nano. – gre_gor Feb 20 '22 at 14:42
  • 1
    Are you sure COM3 is the right port? Do you have the CH340 drivers installed and working? Does it work with Arduino IDE? – gre_gor Feb 20 '22 at 14:43
  • @GabrielStaples Do you know the Input Argument for Nano CH340? – Pix Feb 20 '22 at 14:43
  • 1
    @Pix, Nano CH340 is a Chinese clone. It's probably version 2.x of the Arduino Nano schematic, and is probably not supported by Matlab. It looks like the official nano is now schematic version 3.3. Go here (https://store-usa.arduino.cc/products/arduino-nano/) and click the ["SCHEMATICS IN .PDF"](https://content.arduino.cc/assets/NanoV3.3_sch.pdf) link. – Gabriel Staples Feb 20 '22 at 14:47
  • Try uploading the Uno bootloader to your Nano and selecting "Uno" as the board. I've used the Uno bootloader on that board before. You'll need an ICSP programmer to do this, such as the USBasp programmer, or another Arduino running the ["Arduino as ISP"](https://docs.arduino.cc/built-in-examples/arduino-isp/ArduinoISP) sketch. – Gabriel Staples Feb 20 '22 at 14:47
  • @gre_gor yes I uploaded a program through Arduino IDE already, hence the com port is good. I also tried to close the Arduino IDE so there will be no conflicts between Matlab and IDE. Matlab is finding the board as when I Run code through Matlab the Nano is blinking and a message is showing (Updating server code on board Nano3(COM3). This may take a few minutes.), but after a while, it fails and displays the said error. – Pix Feb 20 '22 at 14:50
  • @Pix Edit all that info into the question. – gre_gor Feb 20 '22 at 17:38
  • I've updated my answer. Remember to upvote any useful answer, and mark correct with the green checkmark an answer if it solves your problem. – Gabriel Staples Feb 20 '22 at 18:07

2 Answers2

0

I don't have the hardware to test this, nor the Matlab software to test this at this time, but here are some ideas. Try them in this order:

  1. Since your Nano is a Chinese clone, set the board in Matlab to ProMini328_5V. I got that name from this list here: https://www.mathworks.com/help/supportpkg/arduinoio/ug/find-board-name.html. It is also stated to work on this forum here: https://www.mathworks.com/matlabcentral/answers/494451-problem-connecting-to-arduino-nano-3-0#answer_404385. If it works, it is because the Pro Mini uses the same ATmega328 microcontroller IC as the Nano, and since the Pro Mini does not come with a built-in UART chip, Matlab might theoretically support a variety of UARTs that you might use on this board, the CH340 included. So, I suspect selecting this board would work.
  2. Upload the latest Nano bootloader to the Nano.(Do NOT skip this step! The latest Nano bootloader may be what is missing to make it work for you with Matlab.)
  3. Ensure you can upload to the Nano using the Arduino IDE.
  4. Ensure you upload the Matlab Arduino Server code to the Arduino: https://www.mathworks.com/help/supportpkg/arduinoio/ug/configure-setup-for-arduino-hardware.html

The Nano CH340 is a Chinese clone. It's probably version 2.x of the Arduino Nano schematic, and is probably not supported by Matlab since they say they support Nano version 3.x. It looks like the official nano is now schematic version 3.3. Go here (https://store-usa.arduino.cc/products/arduino-nano/) and click the "SCHEMATICS IN .PDF" link to see.

So, if it doesn't work at a "Nano", flash the Uno bootloader to the Nano and treat the Nano as an Uno, redoing all steps as though it was an Uno. The Uno bootloader must be on the Nano for this to work.

I've used the Uno bootloader on that board before. You'll need an ICSP programmer to do this, such as the USBasp programmer, or another Arduino running the "Arduino as ISP" sketch.

Lastly, if you still can't get the Matlab code to work, abandon the Matlab server idea and just send raw serial commands to and from the Arduino. This is more advanced but gives you full flexibility. You'll need to write Matlab code to send serial data, and Arduino code to parse and interpret it. I have a demo of doing that many years ago here:

https://www.instructables.com/Arduino-to-MATLAB-GUI-Live-Data-Acquisition-Plotti/

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
0

The CH40 based Arduino Nano is indeed incompatible and requires the processor option "ATmega328P (Old Bootloader)" under "Tools" in the Arduino IDE.

If your Simulink model builds, but fails to upload with an error like this then you can directly upload the .hex file: The following error occurred during deployment to your hardware board: Failed to connect to Arduino board.

  1. Ensure the Nano is working using the Arduino IDE installed with the Simulink Hardware support package. Somewhere like here:

C:\ProgramData\MATLAB\SupportPackages\R2020b\3P.instrset\arduinoide.instrset\arduino.exe

  1. In the Arduino IDE set your board, port, and processor in Tools>Board, Tools>Processor, and Tools>Port.

  2. Under File>Preferences check the box for "Show verbose output during: upload"

  3. Upload a test file like the "blink" program from "examples" and, if successful, then scroll up to find the avrdude command at the top like this:

C:\ProgramData\MATLAB\SupportPackages\R2020b\3P.instrset\arduinoide.instrset\hardware\tools\avr/bin/avrdude -CC:\ProgramData\MATLAB\SupportPackages\R2020b\3P.instrset\arduinoide.instrset\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM4 -b57600 -D -Uflash:w:C:\Users\200000~1\AppData\Local\Temp\arduino_build_62007/Blink.ino.hex:i

  1. In the Matlab command prompt copy that avrdude command and replace the path and .hex filename with the one created by Simulink and add the system prompt "!" at the beginning like this:

!C:\ProgramData\MATLAB\SupportPackages\R2020b\3P.instrset\arduinoide.instrset\hardware\tools\avr/bin/avrdude -CC:\ProgramData\MATLAB\SupportPackages\R2020b\3P.instrset\arduinoide.instrset\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM4 -b57600 -D -Uflash:w:C:\Users\200000~1\Documents\MATLAB\Examples\R2020b\arduino\GettingStartedWithArduinoRHardwareExample/arduino_gettingstarted.hex:i

  1. Should receive response at the end: avrdude done. Thank you.