MATLAB ® Support Package for Arduino ® Hardware User’s Guide R2014a How to Contact MathWorks Latest news: www.mathworks.com Sales and services: www.mathworks.com/sales_and_services User community: www.mathworks.com/matlabcentral Technical support: www.mathworks.com/support/contact_us Phone: 508-647-7000 The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098 MATLAB ® Support Package for Arduino ® Hardware User’s Guide © COPYRIGHT 1984–2014 by The MathWorks, Inc. The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the government's needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc. Trademarks MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders. Patents MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information. Revision History September 2014 Online only New for Version 14.1.0 (R2014a) iii Contents Setup and Configuration 1 Install Support for Arduino Hardware . . . . . . . . . . . . . . . . . . 1-2 Install the Support Package . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Accessing Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5 Arduino Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6 Read Write Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6 Supported Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7 Connect to Arduino Hardware . . . . . . . . . . . . . . . . . . . . . . . . 1-8 Read and Write to Digital Pin . . . . . . . . . . . . . . . . . . . . . . . . . 1-9 Play a Tone on a Piezo Speaker . . . . . . . . . . . . . . . . . . . . . . 1-10 Control LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11 I2C Sensors 2 Arduino I2C Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 I2C Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 Communicate With an I2C Device Using EEPROM Data . . . 2-4 iv Contents SPI Sensors 3 Arduino and SPI Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 SPI Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 Servo Motors 4 Servo Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Servo Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Rotate a Servo Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3 Add-On Shields 5 Add-On Shields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 Add-On Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 Functions — Alphabetical List 6 Troubleshooting 7 Cannot Auto Detect Arduino hardware . . . . . . . . . . . . . . . . . 7-2 Update Device Driver on a Windows System . . . . . . . . . . . . . 7-2 v Unsupported Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2 Manual disconnect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2 Why do I need to specify board type and port? . . . . . . . . . . . 7-3 Find port number on Windows . . . . . . . . . . . . . . . . . . . . . . . 7-3 Find port number on Macintosh . . . . . . . . . . . . . . . . . . . . . . 7-4 My I2C server code hangs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5 I lost my Arduino connection . . . . . . . . . . . . . . . . . . . . . . . . . 7-6 Unplugged hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-6 Board with low data memory . . . . . . . . . . . . . . . . . . . . . . . . 7-6 Too many device objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-6 Pin is Not Receiving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-7 I see strange data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-8 Pin not receiving proper signal . . . . . . . . . . . . . . . . . . . . . . . 7-8 Board with low data memory . . . . . . . . . . . . . . . . . . . . . . . . 7-8 Incorrect input or output . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-8 Problems using pulse duration setting on Servo . . . . . . . . . . 7-9 My Servo Pulse is Not Working . . . . . . . . . . . . . . . . . . . . . . . 7-10 Hardware reserved error . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11 Cannot stack motor shields . . . . . . . . . . . . . . . . . . . . . . . . . . 7-12 My Stepper is Not Rotating . . . . . . . . . . . . . . . . . . . . . . . . . . 7-13 My Motor is Not Working . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-14 I Need More Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-15 Examples 8
Please Wait Your download Will Start in Seconds
Your DownLoad Will start automatically