Use Arduino as controller (HIL) for a simulink modelled physical system

29 views (last 30 days)
Hello Everyone,
I've been searching for an answer to this question and haven't found anything quite applicable in past items from this forum. Would appreciate any guidance you all can give. I have modelled a system (inverted pendulum) in Simulink and tuned a PID controller for support. My next step is to convert my current continuous controller into a discreet form and write that in the Arduino programming environment then upload to the board. That part is not an issue. What I need help with is to replace the controller sub system I have in simulink with controls sent to/from the actual Arduino itself.
What I need to do is simulate the system in Simulink and send the various state information via USB to the Arduino for processing then read that information back in to update the simulated system. Is this possible and does anyone have ideas for where to start? I have already downloaded the Arduino support module but that seems more for laying out a full system to upload onto the Arduino board and run separate from the computer. (While a useful function, it's not applicable to the project I want. I do want to program the controller separately using the standard c-based language for the arduino.)
Thank you in advance for any help!
EDIT: I forgot to mention that I'm using the Arduino Uno (Makezine version)

Answers (3)

Norehsa on 4 Apr 2015
Apologies for cluttering up the board but I was able to find the solution to this. My next step is trying to combine set of inputs (doubles for 3 state variables) and send them across. For anyone else whom wanders across this, you don't want the arduino package simulink blocks. Instead use the Serial send and serial recieve blocks found in the "Instrument Control Toolbox". For the arduino uno you will set the serial configuration block to COM3.
I configured my system for a quick check as 9600 baud, 8 bits, no parity, 1 stop bit.
Here is some Arduino code that you can use to test the connection. Simulink model was a constant source -> data type conversion -> serial send block. Serial recieve -> data type conversion -> display block. The data type conversions are critical if you want anything useful. For the configuration mentioned above you will want to use uint8.
int incomingByte = 0;
//the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(13, OUTPUT); // initialize digital pin 13 as an output
// the loop function runs repeatedly forever
void loop() {
//send data only when you recieve data
if(Serial.available() >0) {
incomingByte =; // read the incoming byte:
Serial.write(incomingByte*2); // modify and return the signal
digitalWrite(13, HIGH); //turn the LED on
delay(500); //wait a half second
digitalWrite(13, LOW); //turn the LED off
delay(1000); //wait a second
  1 Comment
Henk Argeloos
Henk Argeloos on 15 Jun 2015
Dear Norehsa,
I've been trying to accomplish the exact same thing for a school project. My goal is to simulate a cruise control system with an Arduino Uno in the loop as controller, while the 'plant' (i.e. the car dynamics) runs as a Simulink model. Also, i'm using Simulink as the way to program my Arduino.
Could you maybe share a bit more on how you've turned your Arduino into an 'external' controller. For instance, how are you dealing with the limitations of uint8 giving you only the values 0 - 255 to work with?
Thank you in advance!

Sign in to comment.

Ubaldo on 14 Jan 2022
I think this could be a solution (I haven't tried myself though).

Emanuel Feru
Emanuel Feru on 20 Jan 2019
Edited: Emanuel Feru on 20 Jan 2019
I tryied also something similar. Basically a kind of HIL system, where the controller runs on Arduino while the plant model (built with Simscape) runs on my laptop. All the comminication back and forth (closed loop) I want to do via the Serial Communication via USB (Port 0).
After spending almost 2 days, it seems that Simulink Arduino Serial Read/Write blocks are limited. They cannot be easily customized. In other words, it does not allow easy customization of the message to be transmitted.
What I was looking is to send a message like:
"u1 u2 u3 u4 \r\n"
where u1, u2, u3, u4 are values (single, int, boolean, etc.) sent as text with maximum 2 decimals.
Then decode this message with the blocks 'Input stream' or 'Serial Read' from the Instrumentation Toolbox. However, I cannot create the message above.
What is suprizing that in External mode, the arduino board communicates with Simulink via Port 0. So, Matworks must have a solution for running Closed loop systems. I wonder why is this not included as a library block, to be able to do closed loop or HiL type of simulations? Mathworks should respond.
PS: It would be really powerful to be able to do HIL simulations. It would make Arduino and Simulink a complete platform for HIL symulations.


Find more on Simulink Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!