Sending information from Simulink to Arduino over Serial

48 views (last 30 days)
staticrain87 on 17 Jan 2017
Edited: pascal on 11 Feb 2019
I am writing in this group to seek some help with sending PWM signal to Arudino over serial via Simulink
Problem description :
I am working on a personal project which involves building closed loop PID control loop, where I need to send PWM signals to fan, in order to control the position of a ball at a fixed height. The feedback signal is generated via a USB camera, which detects the position of the ball.
Since Arduino can't process the image, I am running my model in Simulink and sending the signal via serial to Arduino. But, the signal somehow doesn't reach or gets processed by the board.
Here is a step by step information on the process I followed to test debug
1. I upload a serial recieve model on the Arduino. It probes the serial port for the data. Once data is found, it is routed to the pin 9 as shown in the image
2. In the simulink environment, to debug, I configured a serial send port (from instrument control toolbox) to transfer the desired PWM signal. However, this does not work. I tried debugging the pin 9, but no Voltage signal was received.
3. In the model above, I added a serial recieve block from the instrument control toolbox. This somehow slowed down the simulation. Each time step was being executed with some delay, but the PWM signals were getting transferred. The blower speed could be changed in accordance with the PWM signal. However this method is too slow for my control loop to work with
Can someone please help me point out the possible error that I am making. I am very curious to find a workaround
staticrain87 on 26 Jan 2017
Hello Jordan
Thank you for your response. I tried the steps that you had suggested. For more clarity, I have listed the steps below, complemented with images
a. For the model that I flash to the Arduino, I have replaced the enabled subsystem block with a Switch. If no data is arriving, then a stair sequence makes the LED connected to pin 9 to blink
b. Next, on the host computer, I modified by simulink model to send PWM signals with frequency in accordance to the stair sequence. This sequence is "almost" a copy of the one used for the serial receive block, but with one increased frequency. So, ideally when I play the model I should see a similar but one bright sequence. However, when I run the model, the LED turns OFF. This is how my model looks for sending the data
Based on the scheme of things it looks like something is happening during data transmit from the host model (simulink) to how it is being received inside Arduino. Possibly as "zero", since the LED does not blink at all.
Is there anything special we should do with the data types, etc? I already make sure to convert it to uint8 in the host model
Any further suggestions, or diagnosis that I should look into?

Sign in to comment.

Answers (4)

Güven Seçkin
Güven Seçkin on 26 Apr 2018
Firts part arduino to simulink
header and terminator means only /n.../n this types values can read by serial recieve Data size[1 2] 2 value can read if you want 3 you adjust [1 3]
Serial configuration baund rate must be same arduino's baund rate.
Arduino side
if true
typedef union
float number[2];
uint8_t bytes[8];
} myFoo;
myFoo foo;
for (int i=0; i<8; i++)
Serial print values must same terminator and header.Only datas between /n can read by simulink.

Güven Seçkin
Güven Seçkin on 26 Apr 2018
Second part simulink to arduino
Thanks to serial send you send datas to arduino but you have to send uint type.You can use data type conversion block in simulink.And you have to transform your data discreate.You can translate you data use Zero-order hold block in simulink.For example you want send 3 values to arduno.First translate these data to uint as ı said send put all datas in mux block so you have one line.Than put this line to zero order hold block finaly you can connect end of line to serial send you dont have some adjustment in serial send block.
Arduino codes. Thanks to these code ı get 4 int number from simulink
//Global Side
typedef union {
int num[3];
uint8_t bytes[3];
} foo;
foo myFoo;
typedef union {
int num[3];
uint8_t bytes[3];
} foo1;
foo1 myFoo1;
typedef union {
int num[3];
uint8_t bytes[3];
} foo2;
foo2 myFoo2;
typedef union {
int num[3];
uint8_t bytes[3];
} foo3;
foo3 myFoo3;
uint8_t input[2];
uint8_t inputBufer[2];
uint8_t inputBuf[2];
uint8_t inputBu[2];
void getDatasFromSimulink() {
int value1=myFoo.num[0];
int value2=myFoo1.num[0];
int value3=myFoo2.num[0];
int value4=myFoo3.num[0];
  1 Comment
pascal on 11 Feb 2019
good evening
i am a french teacher and i would like to know how to send 3 float from simulink to arduino.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!