Arduino and Simulink not working together over serial
14 views (last 30 days)
Show older comments
I am trying to read arduino produced gyro data into simulink but am having major troubles doing so.
This is my arduino code that prints out the gyro data. This is working properly as you can see in the first image this is my serial plotter, which is giving correct data. I know it is just a decoding error in simulink. I have searched everywhere for a solution but so far have come up empty. If you have any ideas on how to fix this please reply.
Serial.print(imu.calcGyro(imu.gx), 2);
Serial.print('\n');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168273/image.png)
This image is my scope on simulink. The data is completely useless.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168275/image.png)
This is what I have in simulink. The Baud rate and everything is correct and matches what I have in my arduino code. I have tried it with and without the ASCII decoder block and have gotten similar results.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168277/image.png)
This is my settings in Serial Receive block in simulink.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168279/image.png)
This is my settings in ASCII decode block in simulink.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168280/image.png)
This is my settings in Serial Configuration block in simulink.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168282/image.png)
2 Comments
Nicolas Schmit
on 16 Oct 2017
What is the output type of imu.calcGyro(imu.gx)? Is it an integer? A float?
Answers (3)
Nicolas Schmit
on 19 Oct 2017
Here is an example of code on the Arduino side that casts at float into an array of bytes then sends it over the serial port.
typedef union
{
float number;
uint8_t bytes[4];
} FLOATUNION_t;
FLOATUNION_t myFloat;
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
}
void loop()
{
// Send a float
myFloat.number = -1.234;
for (int i=0; i<4; i++)
{
Serial.write(myFloat.bytes[i]);
}
Serial.print('\n');
delay(1000);
}
On the Simulink side, when using the Serial Receive block, set the Data Type to "single" so that MATLAB casts the bytes array back to a float.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178726/image.png)
3 Comments
Nazmi Rosly
on 24 Aug 2021
Can i use this to read multiple sensor form serial monitor. For an example i have 3 thermocouples to read. From your code i have succesfully read 1 thermocouple. Now I want to try to read 3 thermocouples at the same time. Is it possible?
Mada Nusa
on 24 Dec 2021
So I have same case with you, sending three yaw, pitch, roll from IMU to Simulink. Use "DEMUX" block to parting those data. Thank's
Prashant Arora
on 18 Oct 2017
Hi Michael,
Have you tried sending data using Serial.write()?
Also, check out the Serial Receive block from the Support Package for Arduino Hardware. I believe that is specifically designed for Arduino hardware.
0 Comments
Nicolas Schmit
on 19 Oct 2017
Edited: Nicolas Schmit
on 19 Oct 2017
The block https://www.mathworks.com/help/supportpkg/arduino/ref/serialreceive.html is used to receive data from the serial port on the Arduino side, when generating Arduino code from Simulink. Its purpose is not to receive data on the Simulink side.
To receive data on the Simulink side, you can.
- Use the Serial Receive block from the Instrument Control Toolbox.
- Establish a serial connection with the serial() command inside a S-Function
0 Comments
See Also
Categories
Find more on Modeling in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!