{"id":2616,"date":"2010-11-05T13:07:56","date_gmt":"2010-11-05T13:07:56","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2010\/11\/05\/deploy-auto-generated-c-code-from-simulink-to-arduino-development-board\/"},"modified":"2017-02-04T14:28:27","modified_gmt":"2017-02-04T19:28:27","slug":"deploy-auto-generated-c-code-from-simulink-to-arduino-development-board","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2010\/11\/05\/deploy-auto-generated-c-code-from-simulink-to-arduino-development-board\/","title":{"rendered":"Deploy Auto-Generated C-Code from Simulink to Arduino Development Board"},"content":{"rendered":"<p xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">Today's Pick comes to us from guest blogger Greg Wolff. <introduction><\/introduction><\/p>\r\n<p xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\"><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620\">Greg's<\/a> pick of the week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/30277-embedded-coder-target-for-arduino\">Embedded Coder Target for Arduino<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/320968\">MathWorks' Embedded Coder Team<\/a>.<\/p>\r\n<p>Did you know that you can quickly implement your design ideas that you simulated in Simulink on a microprocessor? The Arduino Target provides a push-button solution to execute your Simulink algorithm on the Arduino development boards.\r\n\r\n<p>I use Arduino hardware and this File Exchange entry to demonstrate C-code generation from Simulink models to many of our customers.\r\n\r\n<p>While Doug Eastman wrote and posted the original version, the actual file download is now hosted from our academic section of the MathWorks website.\r\n\r\n<p><strong>Special Announcement: MathWorks Virtual Career Fair<\/strong>\r\n\r\n<p>The MathWorks Virtual Career Fair is on Thursday November 11th. This event provides you the opportunity to talk directly with Engineers and Hiring Managers at MathWorks.\r\n\r\n<h3>Contents<\/h3>\r\n<ul>\r\n\t<li><a href=\"#1\">What Is An Arduino?<\/a><\/li>\r\n\t<li><a href=\"#2\">What Features Does the Arduino Target Have?<\/a><\/li>\r\n\t<li><a href=\"#3\">How Do You Use the Arduino Target?<\/a><\/li>\r\n<\/ul>\r\n<h3>What Is An Arduino?<a name=\"1\" title=\"1\"><\/a><\/h3>\r\nSimply stated, an <a href=\"http:\/\/www.arduino.cc\/\">Arduino<\/a> is an inexpensive, practical, simple hardware development package that includes an Atmel microprocessor.\r\n\r\nIt is often considered a hobbyist's development board, but professionals also use this board to prototype new system hardware designs.\r\n<h3>What Features Does the Arduino Target Have?<a name=\"2\" title=\"2\"><\/a><\/h3>\r\n<p>The Arduino Target has a couple key features that I really like for quickly testing my design ideas in hardware:\r\n\r\n<p><strong>Push-Button Approach to Implementation<\/strong>\r\n\r\n<p>The Arduino Target structures the generated C-code so that it can be compiled and used on the Arduino development boards. In addition it automates the process of:\u00a0\u00a0generating the C-code, associating the necessary source and header files, compiling the C-code to object code, downloading the object code to the processor.\r\n\r\n<p><strong>Interact With Sensors and Actuators with Peripheral Drivers<\/strong>\r\n\r\n<p>The Arduino Target provides a few peripheral driver blocks for Simulink that that allow you to specify how the algorithm executing on the processor will interact with sensors and actuators. For example it provides a block for the analog-to-digital converters, and blocks for digital input\/output pins.\r\n\r\n<p><strong>Start-up Guide in Documentation<\/strong>\r\n\r\n<p>When you download the Arduino Target it provides documentation on where to obtain the compiler, how to install the Arduino Target, as well as making the target peripheral blocks available in the Simulink Library Browser.\r\n<h3>How Do You Use the Arduino Target?<a name=\"3\" title=\"3\"><\/a><\/h3>\r\n<p>If you have never generated code before from a Simulink model for a real-time application, there are a few key things to keep in mind:\r\n\r\n<p><strong>Use a Fixed-Step Solver<\/strong>\r\n\r\n<p>I usually recommend the <em>Discrete<\/em> version of the fixed-step solver. However if you choose this option, you cannot use Continuous Time blocks in your Simulink\r\nmodel, but you can use Discrete Time blocks.\r\n\r\n<p><strong>Set the System Target File to Arduino<\/strong>\r\n\t<li>Select <em>Simulation -&gt; Configuration Parameters ...<\/em> from the Simulink menu bar.<\/li>\r\n\t<li>In the configuration parameters dialog, select <em>Real-Time Workshop<\/em> from the parameter selection tree in the left-hand pane.<\/li>\r\n\t<li>Next to the System Target File on the right-hand pane, click on <em>Browse<\/em>. This will open a dialog where you can select available code generation targets.<\/li>\r\n<p><p><strong>Remember Processor Resources are Limited<\/strong>\r\n\r\n<p>Compared to using your desktop or laptop computer, these microprocessors do not have the same extensive computing power or\r\nmemory storage capabilities. Embedded software design is a constant balance of desired features against limited processor\r\nresources, so you may need to make algorithm design changes in order to properly execute on the Arduino hardware.\r\n\r\n<p><strong>Comments<\/strong>\r\n\r\n<p>If you would like to leave any comments regarding this post, please click <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2616#respond\">here<\/a>.\r\n\r\n<script language=\"JavaScript\">                  <!--\r\n\r\nfunction grabCode_bbd7240e1dd641f088a7049879b06ab8() {          \/\/ Remember the title so we can use it in the new page          title = document.title;\r\n\r\n\/\/ Break up these strings so that their presence          \/\/ in the Javascript doesn\\\\\\\\\\\\\\\\'t mess up the search for          \/\/ the MATLAB code.          t1=\\\\\\\\\\\\\\\\'bbd7240e1dd641f088a7049879b06ab8 \\\\\\\\\\\\\\\\' + \\\\\\\\\\\\\\\\'##### \\\\\\\\\\\\\\\\' + \\\\\\\\\\\\\\\\'SOURCE BEGIN\\\\\\\\\\\\\\\\' + \\\\\\\\\\\\\\\\' #####\\\\\\\\\\\\\\\\';          t2=\\\\\\\\\\\\\\\\'##### \\\\\\\\\\\\\\\\' + \\\\\\\\\\\\\\\\'SOURCE END\\\\\\\\\\\\\\\\' + \\\\\\\\\\\\\\\\' #####\\\\\\\\\\\\\\\\' + \\\\\\\\\\\\\\\\' bbd7240e1dd641f088a7049879b06ab8\\\\\\\\\\\\\\\\';\r\n\r\nb=document.getElementsByTagName(\\\\\\\\\\\\\\\\'body\\\\\\\\\\\\\\\\')[0];          i1=b.innerHTML.indexOf(t1)+t1.length;          i2=b.innerHTML.indexOf(t2);\r\n\r\ncode_string = b.innerHTML.substring(i1, i2);          code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,\\\\\\\\\\\\\\\\'--\\\\\\\\\\\\\\\\');\r\n\r\n\/\/ Use \/x3C\/g instead of the less-than character to avoid errors           \/\/ in the XML parser.          \/\/ Use \\\\\\\\\\\\\\\\'\\x26#60;\\\\\\\\\\\\\\\\' instead of \\\\\\\\\\\\\\\\'<\\\\\\\\\\\\\\\\' so that the XML parser          \/\/ doesn\\\\\\\\\\\\\\\\'t go ahead and substitute the less-than character.           code_string = code_string.replace(\/\\x3C\/g, \\\\\\\\\\\\\\\\'\\x26#60;\\\\\\\\\\\\\\\\');\r\n\r\nauthor = \\\\\\\\\\\\\\\\'Greg Wolff\\\\\\\\\\\\\\\\';          copyright = \\\\\\\\\\\\\\\\'Copyright 2010 The MathWorks, Inc.\\\\\\\\\\\\\\\\';\r\n\r\nw = window.open();          d = w.document;          d.write(\\\\\\\\\\\\\\\\'\r\n<pre>\\n\\\\\\\\\\\\\\\\');                \r\n        d.write(code_string);                \r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.                \r\n        if ((author.length > 0) || (copyright.length > 0)) {                \r\n            d.writeln(\\\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\\');                \r\n            d.writeln(\\\\\\\\\\\\\\\\'%%\\\\\\\\\\\\\\\\');                \r\n            if (author.length > 0) {                \r\n                d.writeln(\\\\\\\\\\\\\\\\'% _\\\\\\\\\\\\\\\\' + author + \\\\\\\\\\\\\\\\'_\\\\\\\\\\\\\\\\');                \r\n            }                \r\n            if (copyright.length > 0) {                \r\n                d.writeln(\\\\\\\\\\\\\\\\'% _\\\\\\\\\\\\\\\\' + copyright + \\\\\\\\\\\\\\\\'_\\\\\\\\\\\\\\\\');                \r\n            }                \r\n        }                \r\n\r\n        d.write(\\\\\\\\\\\\\\\\'<\/pre>\r\n\\n\\\\\\\\\\\\\\\\');\r\n\r\nd.title = title + \\\\\\\\\\\\\\\\' (MATLAB code)\\\\\\\\\\\\\\\\';        d.close();        }\r\n\r\n--><\/script>\r\n\r\n<p style=\"font-weight: lighter; font-size: xx-small; color: gray; font-style: italic; text-align: right\"><a href=\"javascript:grabCode_bbd7240e1dd641f088a7049879b06ab8()\"><span style=\"font-size: x-small; font-style: italic\">Get\r\nthe MATLAB code\r\n<noscript><\/noscript><\/span><\/a>\r\n\r\nPublished with MATLAB\u00ae 7.11\r\n\r\n<!--\r\nbbd7240e1dd641f088a7049879b06ab8 ##### SOURCE BEGIN #####\r\n%% Deploy Auto-Generated C-Code to Arduino Development Board\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32620 Greg's>\r\n% pick of the week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24675-arduino-target\r\n% Arduino Target> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/62957 Doug\r\n% Eastman>.\r\n%\r\n% Did you know that you can quickly implement your design ideas that you\r\n% simulated in Simulink onto a microprocessor? The Arduino Target provides a\r\n% push-button solution to execute your Simulink algorithm on the Arduino\r\n% development boards.\r\n%\r\n% I use Arduino hardware and this File Exchange entry to\r\n% demonstrate C-code generation from Simulink models to many of our\r\n% customers.\r\n%\r\n% While Doug Eastman wrote and posted the original version, the actual file\r\n% download is now hosted from our\r\n% <https:\/\/www.mathworks.com\/academia\/arduino-software\/arduino-simulink.html\r\n% academic> section of the MathWorks website.\r\n\r\n%% What Is An Arduino?\r\n%\r\n% Simply stated, an <http:\/\/www.arduino.cc\/ Arduino> is a cheap, practical,\r\n% simple hardware development package that includes an Atmel microprocessor.\r\n%\r\n% It is often considered a hobbyist's development board, but professionals\r\n% also use this board to prototype new system hardware designs.\r\n%\r\n\r\n%% What Features Does the Arduino Target Have?\r\n%\r\n% The Arduino Target has a couple key features that I really like for quickly\r\n% testing my design ideas in hardware:\r\n%\r\n% *Push-Button Approach to Implementation*\r\n%\r\n% The Arduino Target structures the generated C-code so that it can be\r\n% compiled and used on the Arduino development boards. In addition it\r\n% automates the process of * Generating the C-code * Associating the\r\n% necessary source and header files * Compiling the C-code to object code *\r\n% Downloading the object code to the processor\r\n%\r\n% *Interact With Sensors and Actuators with Peripheral Drivers*\r\n%\r\n% The Arduino Target provides a few peripheral driver blocks for Simulink\r\n% that that allow you to specify how the algorithm executing on the processor\r\n% will interact with sensors and actuators. For example analog-to-digital\r\n% converters to read sensor data, and digital input\/output pins to send\r\n% signals to actuators such as motors.\r\n%\r\n% *Start-up Guide in Documentation*\r\n%\r\n% When you download the Arduino Target it provides documentation on where to\r\n% obtain the compiler, how to install the Arduino Target, as well as making\r\n% the target peripheral blocks available in the Simulink Library Browser.\r\n%\r\n\r\n%% How Do You Use the Arduino Target?\r\n%\r\n% If you have never generated code before from a Simulink model for a\r\n% real-time application, there are two key things to keep in mind:\r\n%\r\n% *Use a fixed-step solver*\r\n%\r\n% I usually recommend the _Discrete_ version of the fixed-step solver.\r\n% However if you choose this option, you cannot use Continuous Time blocks\r\n% in your Simulink model, but you can use Discrete Time blocks.\r\n%\r\n% *Set the System Target File to Arduino*\r\n%\r\n% # Select _Simulation -> Configuration Parameters ..._ from the Simulink\r\n% menu bar.\r\n% # In the configuration parameters dialog, select _Real-Time Workshop_\r\n% from the parameter selection tree in the left-hand pane.\r\n% # Next to the System Target File on the right-hand pane, click on\r\n% _Browse_. This will open a dialog where you can select available code\r\n% generation targets.\r\n%\r\n% *Remember Processor Resources are Limited*\r\n%\r\n% Compared to using your desktop or laptop computer, these microprocessors\r\n% do not have the same extensive computing power or memory storage\r\n% capabilities. Embedded software design is a constant balance of desired\r\n% features against limited processor resources, so you may need to make\r\n% algorithm design changes in order to properly execute on the Arduino\r\n% hardware.\r\n%%\r\n% *Comments*\r\n%\r\n% If you would like to leave any comments regarding this post, please click\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2616#respond here>.\r\n\r\n##### SOURCE END ##### bbd7240e1dd641f088a7049879b06ab8\r\n-->","protected":false},"excerpt":{"rendered":"<p>Today's Pick comes to us from guest blogger Greg Wolff. \r\nGreg's pick of the week is Embedded Coder Target for Arduino by MathWorks' Embedded Coder Team.\r\nDid you know that you can quickly implement... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2010\/11\/05\/deploy-auto-generated-c-code-from-simulink-to-arduino-development-board\/\">read more >><\/a><\/p>","protected":false},"author":44,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2616"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2616"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2616\/revisions"}],"predecessor-version":[{"id":8382,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2616\/revisions\/8382"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}