MATLAB Answers

1

Sending information from Simulink to Arduino over Serial

Asked by staticrain87 on 17 Jan 2017
Latest activity Edited by 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
Masoom

  2 Comments

Hello Masoom,
One possible issue could be in the enabled subsystem. A good thing to test would be to have a "Repeating Sequence Stair" block send to the ICT "Serial Send" block in your host model. Then on your Arduino, just have "Serial Receive" block send to the LED (Pin 13). This way you can test that you see the LED blinking on the Arduino. If the LED does blink then this gives a good indication that something is not working as expected with your enabled subsystem.
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.

4 Answers

Answer by laredj karaoui on 29 Apr 2017

me too, Im trying to send a signal to arduino but I failed

  1 Comment

Sign in to comment.


Answer by Sinan Ibrahim Bayraktar on 10 Aug 2017

I have the same problem too, did you find a solution?

  0 Comments

Sign in to comment.


Answer by 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;
foo.number[0]=dataToSend;
foo.number[1]=dataToSend;
Serial.print('\n');
for (int i=0; i<8; i++)
{
Serial.write(foo.bytes[i]);
}
Serial.print('\n');
end
Serial print values must same terminator and header.Only datas between /n can read by simulink.

  0 Comments

Sign in to comment.


Answer by 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];
//////////////////////////////////////////////////////////////////////
//MAIN FUNCTION
void getDatasFromSimulink() {
if(Serial.available()>0){
Serial.readBytes(inputBufer,1);
myFoo.bytes[0]=inputBufer[0];
Serial.readBytes(input,1);
myFoo1.bytes[0]=input[0];
Serial.readBytes(inputBuf,1);
myFoo2.bytes[0]=inputBuf[0];
Serial.readBytes(inputBu,1);
myFoo3.bytes[0]=inputBu[0];
}
int value1=myFoo.num[0];
int value2=myFoo1.num[0];
int value3=myFoo2.num[0];
int value4=myFoo3.num[0];
}
}

  1 Comment

good evening
i am a french teacher and i would like to know how to send 3 float from simulink to arduino.
thanks

Sign in to comment.