{"id":3804,"date":"2012-08-24T09:00:53","date_gmt":"2012-08-24T14:00:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=3804"},"modified":"2012-08-24T16:52:22","modified_gmt":"2012-08-24T21:52:22","slug":"control-an-arduino-from-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2012\/08\/24\/control-an-arduino-from-matlab\/","title":{"rendered":"Control an Arduino from MATLAB"},"content":{"rendered":"<p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/62957\">Doug<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/278-arrow-arduino-io-package-slides-and-examples\">Arduino IO Package<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/76178\">Giampiero Campa<\/a>.<\/p>\r\n\r\n<p>You may remember my <a href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/04\/20\/run-on-target-hardware\/\">last Pick<\/a> was about a new capability to download and run Simulink models on an <a href=\"http:\/\/arduino.cc\/\">Arduino<\/a>, a low-cost electronics prototyping platform. Well I just got a new <a href=\"http:\/\/arduino.cc\/en\/Main\/ArduinoBoardADK\">Arduino Mega ADK<\/a> board and have been playing around with different ways to connect it with MATLAB and Simulink. So if you'll indulge me I'd like to do one more Pick about the Arduino.<\/p>\r\n\r\n<p>Giampiero's submission allows you to easily control an Arduino directly from MATLAB or Simulink. You first download a fixed Arduino program to your board and then you can use either MATLAB code or Simulink blocks to access and control the I\/O on your board. For example, the following MATLAB code connects to a board, reads the voltage on one pin and uses it to control the output on another:<\/p>\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\"><span style=\"color: #228b22;\">% connect to the board<\/span>\r\na=arduino('COM9');\r\n\r\n<span style=\"color: #228b22;\">% set pin 9 to output<\/span>\r\na.pinMode(9,'output');\r\n\r\n<span style=\"color: #228b22;\">% read analog input from analog pin 5<\/span>\r\nav=a.analogRead(5);\r\n\r\n<span style=\"color: #228b22;\">% normalize av from 0:1023 to 0:255<\/span>\r\nav=(av\/1023)*255;\r\n\r\n<span style=\"color: #228b22;\">% ouptput value on digital (pwm) pin 9 <\/span>\r\na.analogWrite(9,av)\r\n\r\n<span style=\"color: #228b22;\">% close session<\/span>\r\ndelete(a)<\/pre>\r\n<p>If you connect a potentiometer to analog pin 5 and an LED to digital pin 9, you could use this code to set the brightness of the LED by turning the potentiometer!<\/p>\r\n\r\n<p>Giampiero's interface lets you read and write to the digital and analog pins, and also control DC, Servo, and Stepper motors if you have the <a href=\"http:\/\/arduino.cc\/en\/Main\/ArduinoMotorShieldR3\">Motor Shield<\/a>. One thing I really like about this package is that it uses <a title=\"https:\/\/www.mathworks.com\/discovery\/object-oriented-programming.html (link no longer works)\">object-oriented programming<\/a>, which makes it very simple to see the available methods by hitting the tab key.<\/p>\r\n\r\n<p>You can accomplish the same thing using the Simulink block library, if you prefer that approach:\r\n<a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/IOLib.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3808\" title=\"IOLib\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/IOLib.jpg\" alt=\"\" width=\"655\" height=\"618\" \/><\/a><\/p>\r\n\r\n<p>The big difference between this package and the Run on Target Hardware capability is that the actual algorithm is running on your computer so you will always need to be connected to the Arduino.<\/p>\r\n\r\n<p>Note that the actual Arduino IO Package is available <a href=\"https:\/\/www.mathworks.com\/hardware-support\/arduino-matlab.html\">here<\/a> on our academic website, the File Exchange submission now just contains some additional demos.<\/p>\r\n\r\n<strong>Comments<\/strong>\r\n\r\n<p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=3804#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/278-arrow-arduino-io-package-slides-and-examples#comments\">comment<\/a> for Giampiero.<\/p>","protected":false},"excerpt":{"rendered":"<p>Doug's Pick this week is Arduino IO Package by Giampiero Campa.\r\n\r\nYou may remember my last Pick was about a new capability to download and run Simulink models on an Arduino, a low-cost electronics... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/08\/24\/control-an-arduino-from-matlab\/\">read more >><\/a><\/p>","protected":false},"author":36,"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\/3804"}],"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\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=3804"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3804\/revisions"}],"predecessor-version":[{"id":3816,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3804\/revisions\/3816"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=3804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=3804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=3804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}