MATLAB: Send new program to Arduino without Simulink

arduinoEmbedded Coder

Hi,
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.

Best Answer

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/Arduino.app/ on the mac
-----------------------------------------------------
-----------------------------------------------------
Solution
-----------------------------------------------------
1. Launch Arduino IDE ( Arduino.app 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/Arduino.app/Contents/...
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);
else
% Mac

pathHexLib = sprintf('/Users/%s/Desktop/',accountName);
end
10. Save the temporary HEX file
if ispc
% Windows therefore slash is \
idxSlash = regexp(pathHEX,'[\\]');
else
% Mac therefore slash is /
idxSlash = regexp(pathHEX,'[/]');
end
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');
end
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',...
pathAVR,pathAVRconf,deviceType,programmerType,comPort,pathHEX_new);
else
% Mac
strUpload = sprintf('%s -C%s -p%s -c%s -P%s -D -Uflash:w:%s',...
pathAVR,pathAVRconf,deviceType,programmerType,comPort,pathHEX_new);
end
[status,result] = system( strUpload );