Send new program to Arduino without Simulink
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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:
Embedded Coder Target for Arduino ( http://www.mathworks.com/matlabcentral/fileexchange/30277-embedded-coder-target-for-arduino )
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
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:
C:\Users\PAULJO~1\AppData\Local\Temp\build6145494704296263461.tmp\blink1.cpp.hex
And I moved it to:
C:\Users\PAULJO~1\Documents\MATLAB\TestCode\Arduino\ArduinoIdeProgramming\blink1.cpp.hex
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.
채택된 답변
Paul
2014년 8월 14일
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] '; % 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 );
댓글 수: 7
Hi, I need help in this issue please, i tried the steps you wrote and the program runs without errors but noyhing happens and the program doesnt run, can you help please?
did i take the output correct as shown in the image?

It appears you are using Windows, in which case you need to use 'AVRDUDE' rather than the highlighted 'avr-objcopy'
I did not make this obvious. I will re-write the solution today or in the next few days.
1) Find filepath to AVRDUDE
[Arduino base folder] \ hardware \ tools \ avr \ bin \ avrdude
2) Find file to AVRDUDE.CONF
[Arduino base folder] \ hardware \ tools \ avr \etc \ avrdude.conf
3) Find filepath to your HEX file
4) Update your filepaths to deal with whitespaces.
Example: C:\Program Files (x86)\Arduino\... becomes C:\"Program Files (x86)"\Arduino\...
5) Identify your device and programmer type. For Arduino Uno, typeDevice='atmega328p' and typeProgrammer='arduino'
6) Identify COM port. If using COM port 4, strPort='\\.\COM4'
7) Build your DOS command
strUpload = [pathAvrdude ' -C' pathAvrdudeConf ' -v -v -v -v' ...
' -p' typeDevice ' -c' typeProgrammer ' -P' strPort ' -D' ...
' -Uflash:w:' pathHex ':i'];
Should look like my original screenshot above
C:\ArduinoIDE\hardware\tools\avr\bin\avrdude -CC:\ArduinoIDE\hardware\tools\avr\etc\avrdude.conf -v -v -v -v -patmega328p -carduino -P\\.\COM9 -D -Uflash:w:C:\Users\" [user name] "\AppData\Local\TempFolder\blink1.cpp.hex:i
Remember to use " " if there are whitespaces
8) Send the command
[status,response] = system( strUpload )
please can you explain how to do step 3 and then step 4 and how to update the filepaths
3) Find filepath to your HEX file
This is in your screenshot, the last line
C:\Users\ziad\ ... \servosg90.cpp.hex
By the way, this is in a temp folder which means it will be deleted when you restart your computer. You should save it to another folder, perhaps wherever the rest of your Matlab code is.
4) Update your filepaths to deal with whitespaces.
DOS doesn't accept whitespaces. You need to put " " around folders with whitespaces.
Original filepath = C:\Program Files (x86)\Arduino\randomFile
Updated filepath = C:\"Program Files (x86)"\Arduino\randomFile
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".
I have the same problem with arduini Mega. Please let me know if you hav efound the solution. Thanks.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기
태그
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
