Send new program to Arduino without Simulink

52 views (last 30 days)
Paul on 6 Aug 2014
Commented: Xiaokai Li on 2 May 2017
I'd like to send a new program/sketch to an Arduino, through Matlab. The closest I've been able to come is to use:
This seems to require using Simulink to send the new program. Is there a way to use this code without Simulink? Or maybe someone knows of another route?
Please note that the ArduinoIO package doesn't help us either in this regard.
  1 Comment
Paul on 8 Aug 2014
Solution has been worked out:
1) Develop in the Arduino IDE all of the programs/sketches that you will need
2) In the Arduino IDE, turn ON verbose output via File -> Preferences
3) Upload program 1
4) When upload is finished, record the last line of the compilation output. If my program is called blink1, then I saved this line to blink1_cmd.txt
Note that in the example shown, the last line is:
C:\ArduinoIDE\hardware/tools/avr/bin/avrdude -CC:\ArduinoIDE\hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega328p -carduino -P\\.\COM9 -b115200 -D -Uflash:w:C:\Users\PAULJO~1\AppData\Local\Temp\build6145494704296263461.tmp\blink1.cpp.hex:i
5) Find the HEX file associated with your program and save it elsewhere. In the case above, the HEX file is:
And I moved it to:
Please note: If you run into trouble, try using folders without spaces
6) In your text file, update the HEX file path
7) Copy the entire line from the text file
8) In the Matlab command window:
[status,cmdout] = dos(' paste code here ')
You should get an output of:
avrdude done. Thank you.
9) Repeat as necessary for the other programs.

Sign in to comment.

Accepted Answer

Paul on 14 Aug 2014
Solution works for Windows, Mac, and Linux
What you want to be able to do:
Through MATLAB, upload a program/sketch to your Arduino.
During the upload process, the Arduino IDE creates a temporary HEX file. We want to copy that HEX file to a permanent location and be able to send it to the Arduino via MATLAB
Before beginning:
  1. Make sure you have the Arduino IDE installed on your computer
  2. Connect your Arduino via USB to the computer
  3. Locate the program you want to upload to the Arduino
Important: As I was looking for this solution, I've heard that some people encounter problems when folder paths include spaces. I haven't done testing myself, I instead re-installed the Arduino IDE to C:\ArduinoIDE\ on the PC and /Applications/ on the mac
1. Launch Arduino IDE ( on mac, Arduino.exe on PC)
2. Go to Preferences by using CTRL + comma on the PC or COMMAND + comma on the Mac
3. In Preferences , find the fourth line that starts "Show verbose output...". Check ON the compilation box
4. Open your file and upload it to the Arduino.
5. When the uploading is done, find the second to last line of the output (immediately before Binary sketch size: ...) on Mac or the very last line on PC
5.a. On a mac, the line should begin with /Applications/
5.b. On a PC, the line should begin with C:\Program Files\Arduino\hardware... if you have the default install, otherwise I get C:\ArduinoIDE\hardware...
6. Copy the line, then launch Matlab and save the line in strOutput. Also, define the COM port used to connect to the Arduino (in the Arduino IDE, go to Tools -> Serial Port)
strOutput = ' [paste line here] '; % Be sure to remove the extra whitespaces at the front and end
comPort = '/dev/cu.usbmodem1d11' % mac example
comPort = 'COM9' % windows example
7. Get path to avrdude and store it in pathAVR
idxSpace = regexp(strOutput,'\s');
strAVRBIN = 'avr.bin';
idxAVRBIN = regexp(strOutput, strAVRBIN );
idxSpace = idxSpace( idxSpace>idxAVRBIN );
strAVR = strOutput(1:idxSpace(1)-1);
pathAVR = strAVR(1:idxAVRBIN+size(strAVRBIN,2));
pathAVR = sprintf('%s%s',pathAVR,'avrdude');
8. Get path of the temporary HEX file (this is your Arduino program)
idxHEX = regexp(strOutput,'hex');
pathHEX = strOutput(idxSpace(end)+1:idxHEX(end)+2); % This may be incorrect if there are spaces
9. Provide path to your permanent library of HEX files. Cannot have spaces. For now, I'll use the desktop
accountName = 'Paul';
if ispc
% Windows
pathHexLib = sprintf('C:\\Users\\%s\\Desktop\\',accountName);
% Mac
pathHexLib = sprintf('/Users/%s/Desktop/',accountName);
10. Save the temporary HEX file
if ispc
% Windows therefore slash is \
idxSlash = regexp(pathHEX,'[\\]');
% Mac therefore slash is /
idxSlash = regexp(pathHEX,'[/]');
filenameHEX = pathHEX(idxSlash(end)+1:end);
pathHEX_new = sprintf('%s%s',pathHexLib,filenameHEX);
[status] = copyfile(pathHEX,pathHEX_new);
if ~status
[status] = copyfile(pathHEX,pathHEX_new,'f');
11. We have saved our HEX file. Now we can send it to the Arduino
pathAVRconf = sprintf('%s%s%s',pathAVR(1:end-11),'etc','/avrdude.conf');
deviceType = 'atmega328p';
programmerType = 'arduino';
baudRate = '115200'; % Not needed. To use in strUpload, add before '-D' as '-b%s'
if ispc
% Windows
strUpload = sprintf('%s -C%s -p%s -c%s -P%s -D -Uflash:w:%s:i',...
% Mac
strUpload = sprintf('%s -C%s -p%s -c%s -P%s -D -Uflash:w:%s',...
[status,result] = system( strUpload );
Xiaokai Li
Xiaokai Li on 2 May 2017
Hello Paul, I followed your instruction and was able to upload script (HEX) to Uno board successfully. But the same method does not seem to work for MEGA2560 board. I got the following error "avrdude: stk500_recv(): programmer is not responding". I have made sure the comPort is correct and the device type is changed to "atmega2560".

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!