5.0

5.0 | 11 ratings Rate this file 180 Downloads (last 30 days) File Size: 23.8 KB File ID: #24675
image thumbnail

Arduino Target

by Doug Eastman

 

09 Jul 2009 (Updated 21 Jun 2012)

Develop applications for the Arduino platform using Simulink®.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

**UPDATE**
There are now two approaches to compile your Simulink model to run on an Arduino:

1. With only Simulink (including the student version) you can use the Simulink Support Package for the Uno/Mega described here:
http://www.mathworks.com/hardware-support/arduino-simulink.html

2. If you have Simulink Coder and Embedded Coder you can use this target which supports Duemilanove/Uno/Mega, allows the development of custom driver blocks, supported processor-in-the-loop execution, and provides access to the generated C code:
http://www.mathworks.com/matlabcentral/fileexchange/30277

Please use the download and instructions at the above links rather than the download here, which is now just an example model that you can use once you have downloaded and set up the Arduino Target.
---------------------------------------------
Arduino (www.arduino.cc) is a low-cost open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. Arduino Target allows you to develop applications for the Arduino platform right from Simulink. The target includes blocks to interface with the I/O ports on the Arduino board as well as a target file that automatically compiles and downloads the application onto the board directly from Simulink.

Acknowledgements

This file inspired Embedded Coder Support Package For Arduino.

Required Products Simulink Coder
Embedded Coder
Simulink
MATLAB release MATLAB 7.8 (R2009a)
Other requirements Arduino Environment Only tested in Windows C++ compiler recommended for serial communication
Tags for This File  
Everyone's Tags
arduino(7), pick of the week, potw, rtw, rtw target, simulink target(2)
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (56)
10 Mar 2013 klob

Hi,

I have a problem using the arduino target.
I am trying to send data via serial communication from a model running on arduino to a model running in my computer.

The problem is that I get wrong values when the data I am about to send are negative (i.e. the transmission is fine when I am sending values 0 or greater but wrong when they are below zero (instead of -4 I receive 252 etc)).
I also tried to send negative values through the example that you have (demo_arduino_serial_communication and demo_arduino_serial_communication_host), by changing the Analog input block with a constant block and once again it works fine when using values zero or greater but I am getting the following error message when I am trying to send negative values through the serial communication.
"Parameter overflow occurred for 'Value'. The parameter's value is outside the range that the run-time data type can represent. The specified value was saturated to the closest representable value. You can control this diagnostic on the Diagnostics pane of the Configuration Parameters dialog."

I thought to try and change something on the Diagnostics pane, but I thought that it would be wiser to ask someone more experienced first.

What do you think?

I have an arduino UNO and MATLAB R2011b.

Thanks in advance,
a desperate student

25 Aug 2012 Vanaroth Samrith

I am unsure on what is this Pref.m file that we can edit to use the Arduino 1280. Can some please point me in the right direction?

30 Jul 2012 ipac

Actually I am interested sending the high level commands from matlab to arduino. For example, the desired motor speed, or motor position and the feedback control loop to be performed in arduino. Do someone has any sample program for this kind of problem

Thank you

25 Jul 2012 klob

Hello,
i am trying to send two signals from my arduino uno to simulink(using arduino target).Is it possible?

Thank you!

21 Jun 2012 Mohammad

Has anyone ever try to use the arduino target package on chipkit max32 microcontroller? Or know whether it will support this package?

29 Nov 2011 MathWorks Classroom Resources Team

The latest version of Arduino Target (a.k.a Simulink Support Package for Arduino) addresses the above problems, and has improved support for Uno and newer Arduino boards: http://www.mathworks.com/matlabcentral/fileexchange/30277

26 Oct 2011 bruno brunov

Hello,
how can I receive 2 or more signals from analog input in demo_arduino_serial_communication. I used Mux (before int16 to uint8 converter) to collect 2 signals. But i do not know, how configure the serial receive at host side. What is the output of serial receive? (vector [1 1] is default, that doesn't work of course). I tried everything, [2 2], 2 ...

Thanks

11 Oct 2011 Boris

Oh yeah

### Successful completion of Real-Time Workshop build procedure for model: demo_arduino_blink

Thank you, Hever!

10 Oct 2011 Hever Moncayo

Boris,
Yes, you can get Arduino Uno working with the package. Try these steps in the Pref.m file:
1. Add a mcu function getMcuForAvrDude to use atmega328p whenever you are using atmega328p-pu. You can add it before the uploadRate function code:

function mcu = getMcuForAvrDude
mcu = arduino.Prefs.getPref('ArduinoMcu');
if strcmp(mcu,'atmega328p-pu')
mcu = 'atmega328p';
end
assert(~isempty(mcu),'Arduino MCU preference must be set');
end

2. Then include a case inside the uploadRate:

case 'atmega328p'
uploadRate = '115200';

3. At the very end of the Pref.m file, change the Device name for:
deviceName='\\Device\\USBSER000';

Hope these steps solve your problem

08 Oct 2011 Boris

Hello
i have the arduino Uno with the atmega328p-pu and i have a familiar problem using the arduino target
avrdude: stk500_getsync(): not in sync: resp=0x00
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51
i`ve red a lot about this but with no succes
all i am asking is can Arduino Uno work with arduino target and if so how can i solve this problem

Thanks

15 Sep 2011 Hever Moncayo

Gustavo,
Probably you are using the wrong device name. Try this in the Prefs.m file:

deviceName='\\Device\\USBSER000';

15 Sep 2011 Gustavo Rodriguez

Why the arduino.Prefs.searchForComPort does not give me any port? just ''.
The arduino card is plugged and it editor shows it is in port COM5.

14 Sep 2011 Hever Moncayo

Akram, you just need to add couple of lines of code in the Prefs.m file.
swicht mcu
case ' atmega1280'
uploadRate='57600';

25 Aug 2011 Gary

Hi Doug,

Demo's work great!

Noticed that the block folder contains: LcdOutput & ServoOutput s-functions, can you tell me how to add them to the library as functional blocks?

Thanks.

09 Jul 2011 akram

hello
is there any way to use Arduino Mega 1280 with simulink.

15 Jun 2011 Ossama

Hello,
I would like to play a Simulink model into a ATmega128-board.
how can I create Simulink Blocksets for a Atmega128 µC ( or general for any microcontroller)?

Thanks a lot.

NB: please a detailed example.

16 May 2011 Mehmet Ali Güney

Hello Doug
I am attempting to use the target library with MATLAB 2010b, Arduino-0022, and ATMega 328. Whenever I tried to connect my arduino uno to simulink I always get this error: Could not execute target data map file 'untitled_arduino\untitled_targ_data_map' or it does not exist.
Stop the target, delete the untitled executable, rebuild the code, and try again. Note that execution of external mode requires the build directory to be present.
thanks

01 May 2011 Jonathon

Doug,

I am trying to develop a control system for a personal transport vehicle using the Arduino Mega2560 board. I have not been able to use Simulink with this board. I have been using arduino-0018. Is there a way to change the pin addresses and allow support for the ATmega2560?

Thanks,

20 Mar 2011 Rino

Hello Doug, thank you for this application. I've a system where an algorithm on Arduino controls the position of a motor. I would send the set-point variable and receive the actual position variable by serial communication, in soft real-time. In demo_arduino_serial_communication_host, time simulation seems to be equal to real world clock, but in my model doesn't. I use a fixed time step because I need to send and acquire data in a regular way. There is a way to run the simulation with a real clock time, so I can interface control code looping on Arduino and supervision model of Simulink? I've tried with external mode, but there isn't a Transport Layer in Host/Target Interface for arduino.tlc.
Thanks in advance.

20 Mar 2011 Rino  
20 Feb 2011 richard clapham  
18 Feb 2011 Doug Eastman

Kun, the easiest way to add code like that found on your link is with the Custom Code blocks in the Real-Time Workshop block library. I would put the frequency setting function in a Model Source block, and the call to the function in a System Start block.

14 Feb 2011 Kun

Doug, everything is working great on my board. Except that I want to change the frequency of the PWM signal. I know how to change the frequency in arduino-0022, which can be found here, http://www.arduino.cc/playground/Code/PwmFrequency.
I'm wondering if i can also change the frequency in simulink. Thanks

09 Feb 2011 Mohd Ismail  
13 Dec 2010 Doug Eastman

To compile the C files for win64 (or another platform), first make sure you have a supported compiler: http://www.mathworks.com/support/compilers/R2010b/win64.html

Once you have a compiler installed, you can run "mex -setup" to point to the appropriate compiler.

Finally, you can run "mex filename.c" to compile all the functions in the blocks directory.

12 Dec 2010 Juan

Hello,

How do you compile these files for using them in windows 64-bit?

Thank you!

11 Oct 2010 Alex

Hello Doug.

I`m having some problems with the demo_arduino_blink. I`m using the Matlab 2010a and Win7 32 bits. I did all the process to set the arduino target on Simulink, but I don`t get to do the arduino board work with simulink. When I run the demo_arduino_blink doesn`t happened nothing. Please, I need some help.

Thank you!

23 Sep 2010 thanos

When im trying to run the demo_arduino_blink it shows me this error Error in S-function 'demo_arduino_blink/Digital Output': S-Function 'sfunar_digitalOutput' does not exist. Does anybody have experience with this? Im running windows7 x64 and the matlab 2010a and i have allready installed the 64 bit visual studio components.

29 Aug 2010 Steven

Doug,
I have installed the new package from http://www.mathworks.com/academia/arduino-software/arduino-simulink.html and everything seems to be working except the analog write block. Every time I try to compile a model with that block in it I get an implicit declaration error for the analogWrite command. Any suggestions?

28 Aug 2010 Kiran

When I am trying to run install Ardunio its not recognizing Ardunio.exe(java) I am using matlab2009

Thanks

27 Aug 2010 Doug Eastman

**UPDATE**
The target has been updated, but is now located here:
http://www.mathworks.com/academia/arduino-software/arduino-simulink.html

Please use the download and instructions at the above link rather than the download here. This submission will be removed in the near future.
Thanks!

01 Aug 2010 marco gomez

forget the above...now I have a next error: Error "sfcn_serialConfig.c: 130 undeclared identifier `MDL_START_AND_MDL_PROCESS_PARAMS_OK'"...what is the reason?

01 Aug 2010 marco gomez

I really wanna help, how compile for 64 bits, and how generic the files with extension .mexw64.

Thank you.

31 Jul 2010 marco gomez

i dont have "simstruc.h"

31 Jul 2010 marco gomez

I have the same error:"Error in S-Function", but how compile for 64 bits?...i don't know.

Thank you

28 Jul 2010 Bob Withrow

Yes, dumb question: reading the "getting started" says simulink is recommended... ;-)

28 Jul 2010 Bob Withrow

This is probably a dumb question, but will stateflow work with this?

25 Jul 2010 Olaf Hagendorf

Yes, they are only for 32bit. Just compile them for 64bit and everything is OK. I use the Arduino target under Win7 64bit with Matlab 64bit without any problems.

25 Jul 2010 Sean

I believe I found the problem for the "Error in S-Function" error message. Are the mex files for Window 32-bit systems only? That would explain everything since I have a 64-bit system.

24 Jul 2010 Sean

Hi Doug,

I seem to be having the same problem as Jesus above. I get the error "Error in S-function 'blink/Digital Output': S-Function 'digital_output' does not exist." while the installArduino process seems to have work properly.

12 May 2010 Jesus Sanchez

Hi I'm having some trouble, I can't seem to be able to run any example, it gives me the error "Error in S-function 'blink/Digital Output': S-Function 'digital_output' does not exist", thanks in advance

03 May 2010 Sahak Margossian

Hey Doug,

Thank you for this software. I am trying to use my arduino to control a servo based on some image processing done in matlab and I am having trouble figuring out what the output has to be in, i.e. does it have to be uint8() and how do I turn a pin to HIGH or LOW? Also, can I use the pulse generator function in simulink to control my servo or do I just have to write a function to create the pulses? Thank you.

-Sahak

20 Mar 2010 Emre

thank you for useful information

16 Mar 2010 Jon

Doug,
Thnaks for the suggestion, everything works, I am thinking to just create a Configure Text Block that performs the mex-setup command when double clicked for now. However, I had other issues when trying to use the blockset with MATLAB R2009a and Arduino -0018. I made the same changes to the installation and .tmf files for the Arduino -0018 directory updates, but when I run the blink model, I get the following error:

C:\MATLAB\arduinoTarget\examples\blink_arduino>gmake -f blink.mk GENERATE_REPORT=0 GENERATE_ASAP2=0
gmake: *** No rule to make target `arduino', needed by `core.a'. Stop.
### Real-Time Workshop build procedure for model: 'blink' aborted due to an error.

I know this is an issue that outside of MATLAB, but any recommendations on probing for a solution?

Jon

15 Mar 2010 Doug Eastman

Hi Jon,
The serialCommunication example includes an Embedded MATLAB block. If you use EmbeddedMATLAB, Stateflow, or a TruthTable block, Simulink will try to build code for the simulation using the compiler you have chosen using mex -setup. Because the Arduino requires C++ code generation, the simulation currently also needs to support C++. I will try to investigate a better solution, but for now, you will need to install a supported C++ compiler and choose that option by running "mex -setup" at the MATLAB command prompt. A list of supported compilers is shown here: http://www.mathworks.com/support/compilers/R2010a/index.html
I'm using Microsoft Visual C++ 2005.

13 Mar 2010 Jon

Doug,

Thank you for your help. I have the Arduino-0018 and Arduino-0017 both working with the Blink example. Unfortunately, I am having problems generating code for the serialCommunication example:
Error using ==> sf
Error using ==> construct_error at 109
The current Real-Time Workshop target is configured to generate C++, but the C-only compiler, LCC, is the default compiler.

I cannot find the discrepancy between the configuration errors in the two examples. Is there a check to ensure I am configuring the RTW to use the appropriate compiler?

Jon

13 Mar 2010 Doug Eastman

Hi Jon,
Yes, that's unfortunate, it looks like version 0018 has changed the directory structure which breaks the install script and the makefile. The change you made to the install script is correct. To fix the build error, I believe you only need to change line 126 in arduino.tmf to:
ARDUINO = $(INSTALL_DIR)/hardware/arduino/cores

13 Mar 2010 Jon

Doug,

I am attempting to use the target library with MATLAB 2009b, Arduino-0018, and ATMega 328.

I ran into an error while running installArduino.m, which stated that I was not addressing the correct directory with the arduino directory. I replaced line 297 with:
if ~exist(fullfile(installDir,'hardware','arduino','cores'),'dir')
as the cores directory location must have change in version 0018. No issues with installation after this change.
However, when I try to run the blink example, I get the following error:
blink.h:27:22: error: WProgram.h: No such file or directory
which prevents me from linking the libraries for OUTPUT, PINMODE, and ANALOGWRITE, and building the hex file.
Is this error also associated with the directory changes for arduino-0018?

Jon

14 Jan 2010 Gareth Thomas

Great example of how powerful tlc, and makefiles can be...

Great way to integrate MATLAB with the embedded world:)

07 Jan 2010 Christopher Korpela

Hi Doug,

I am still having problems running the serial communication example in Simulink on my Arduino board. My setup is the same as before. I am able to run the blink example but with the same error I get as before (see above).

Here is the error when I build:

Error building Real-Time Workshop target for block diagram 'serialCommunication'. MATLAB error message: Error(s) encountered while building model "serialCommunication".

Any help would be great. Thanks.

-Christopher

30 Dec 2009 Olaf Hagendorf

Hi Dough,

great work!
I tried it with an older MATLAB 2007b and it works straightaway except some warning when loading the simulink file.

Thanks,
Olaf

17 Dec 2009 Sophie Taylor

It'd be great if you could add some more blocks such as Servo read/write

03 Dec 2009 Sreeram Mohan

Hi Doug,

This file is great for some body who wants to start using simulink with arduino !

It would be great if the getting started section includes the steps as to how a user could get the s-function blocks of there own ready so that people get to know the entire design flow in customizing the tool for a platform.

Given the fact that the arduino has lot of shields available (ethernet, touch shield ..etc) folks could just get the blocks ready and update it as per the requirement allowing a complete design flow.

Hope it makes sense.

Thanks,
Sreeram Mohan

29 Nov 2009 Christopher Korpela

Hi-

I am getting the following error:

Error building Real-Time Workshop target for block diagram 'blink'. MATLAB error message: ### Unable to find build success string: "### Created" in build log.

I am using arduino-0017, an ATmega328, and MATLAB 7.8.0 (R2009a). It appears the hex file is being loaded as the LED is blinking properly. Any ideas? Thanks.

10 Nov 2009 Doug Eastman

Hi Matthew, the ~ is a new construct that was introduced in R2009b. The files will be updated shortly with a version without ~'s that works in R2009a and latter. In the meantime, you can simply replace the ~'s with valid variable names (x1, x2, etc.).

08 Nov 2009 Matthew

The code for "installArduino" has several tilde which do not pass the MATLAB compiler. As an example the very first line:
function installArduino(~) produces the error:
??? Error: File: installArduino.m Line: 1 Column: 25
Unexpected MATLAB operator.

The tilde exists at the location listed and stops the install program from executing. This same problem will cause the installation program to seize at several other points:
Line: 240 Column: 29 & 33
Line: 253 Column: 32
Line: 275 Column: 31
etc . . .

I am very interested in the practicality of this MATLAB & Simulink application. Please fix these problems so I can make good use of MATLAB and Arduino.
Thank you.

Updates
01 Oct 2009

Added support for multiple chips including ATmega328, ATmega168, ATmega8, and ATmega1280. Also improved the installation script, added support for newest Arduino IDE, and added more information about serial communication in the Getting Started Guide.

13 Oct 2009

Changed the install script to be compatible with releases prior to 2009b.

10 Nov 2009

Updating files to remove ~'s so it works in R2009a.

27 Aug 2010

Adding a link to the new target location

31 Aug 2010

Removing files and replacing with an example model. Adding link to new location in the description and HTML file.

21 Jun 2012

Updated description with two links to help clarify different options for using Arduino with Simulink.

Contact us