MATLAB Answers

How to implement Serial Communication between Simulink (R2012b) and an Arduino?

17 views (last 30 days)
I would like to have Simulink run a model (i.e. controller), transmit information to the Arduino, have the Arduino receive the data and implement some action based on the data.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 6 May 2013
The solution is to run a model in Simulink using the play button, and run another model on the Arduino using the "Tools->Run on Target Hardware->Run" option.
I have included two models which perform this functionality. The transmitting model is called "SerialSend" and the receiving model is called "ArduinoReceive". The SerialSend model should be run by using the play button while the ArduinoReceive model should be run by selecting "Tools->Run on Target Hardware->Run" to push code to the Arduino. The transmitting model sends a pulse with values of 2 and 3. The pulse has a period of 4 seconds with a 50% duty cycle. The receiving model just checks to see if the value sent is a 2 or 3 and sends a corresponding boolean to pin 13. On the Arduino Mega 2560 and Uno this will cause the corresponding onboard LED to blink with a period of 4 seconds.
The transmitting model should not try to push code to the target by using the "Tools->Run on Target Hardware->Run" command. Doing so will push code to the Arduino and erase the original code, as you already discovered.
The SerialSend model uses the Instrument Control Toolbox "Serial Send" block to transmit data over the serial connection (i.e. usb programming cable). You will need to check the device manager to determine which COM port corresponds to the Arduino. The ArduinoReceive model should use an "Arduino Serial Receive" block.


hey,I tried this code but it shows the following error. Could you please help me with that.
Errom message: "The call to realtime_make_rtw_hook, during the after_make hook generated the following error: Could not connect to specified Arduino board."
And it says that i may be connected to the wrong COM port, so i tested the port and i connected correctly. But still the error persists.
I'm using Arduino Mega 2560 board, Simulink(R2012b).
Please help me with this problem.
Cristian  Acero Mancipe
Cristian Acero Mancipe on 14 Oct 2016
Hola cuando te muestre el mensaje de error debes desconectar tu arduino y volverlo a conectarlo y oprimir reset, así lo solucionas. saludos
Walter Roberson
Walter Roberson on 15 Oct 2016
Approximate translation:
Hello when you display the error message should disconnect your Arduino and connect it again and press reset, so you solve. regards

Sign in to comment.

More Answers (0)


No tags entered yet.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!