Compile Simulink generated code in Arduino IDE?

167 views (last 30 days)
I'm trying to figure out how to get code created by Embedded Coder to build in the Arduino IDE. I have created a model in Simulink that I can build directly to the arduino board and it runs. However, I'd like to be able to make some modifications so it can run on some Arduino 3rd party boards.
I tried using Embedded Coder to generate the code, but it doesn't package it as an arduino .ino file. I tried adding all the generated files to the sketch, but arduino requires you to use the setup and loop functions and it was not obvious how to connect the generated code into the sketch.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Apr 2017
The inability to compile Simulink's generated code within the Arduino IDE is expected. The Arduino IDE uses its own language (the files are INO-files) which are then converted to C-code. This can often go unnoticed as Arduino's language is so similar to C (it is actually a subset of C).
The Simulink Support Package is generating C-code directly (bypassing the INO-file).  However, there is a way to modify the generated C-code before deploying an application to the target.
1) Select the option to "Generate code only" in the Simulink Code Generation Configuaration Parameters and generate code for the Arduino embedded target.  
2) Open and modify that generated C-code using any C-code IDE.  
3) Use the MK-file generated by Simulink to compile and build the application for the Arduino.  
4) Use the 'avrdude' tool to deploy the application to the Arduino.
The following website provides a good explanation of the Arduino IDE programming/compilation process (and how to bypass using the INO-file).
  1 Comment
Vadim Galkin
Vadim Galkin on 21 May 2020
I installed a trial version of 2020a and I can not find the folder where is the C code generated?

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!