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:
- Make sure you have the Arduino IDE installed on your computer
- Connect your Arduino via USB to the computer
- 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] ';
comPort = '/dev/cu.usbmodem1d11'
comPort = 'COM9'
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);
9. Provide path to your permanent library of HEX files. Cannot have spaces. For now, I'll use the desktop
accountName = 'Paul';
if ispc
pathHexLib = sprintf('C:\\Users\\%s\\Desktop\\',accountName);
else
pathHexLib = sprintf('/Users/%s/Desktop/',accountName);
end
10. Save the temporary HEX file
if ispc
idxSlash = regexp(pathHEX,'[\\]');
else
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';
if ispc
strUpload = sprintf('%s -C%s -p%s -c%s -P%s -D -Uflash:w:%s:i',...
pathAVR,pathAVRconf,deviceType,programmerType,comPort,pathHEX_new);
else
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 );