Modelica-Arduino
Modelica-Arduino copied to clipboard
Simulate circuits and sketches on a virtual Arduino Uno in Modelica
Simulate Arduino Sketches in Modelica
With the Arduino Modelica library you can simulate your circuits and sketches on a virtual Arduino Uno and connect your Modelica models to real-world circuits using the Firmata protocol.
Prerequisites
- Dymola or OpenModelica
- Visual Studio 2015 or later
- CMake
Getting Started
- download the latest release and extract the archive
Simulating a Sketch with Dymola
- in Dymola select
File > Load...and openArduino/package.mofrom the extracted files - open
Arduino.Components.ArduinoUnoand set the default values for parameterscmakeandgeneratorto match your installation, e.g.
model ArduinoUno "Virtual Arduino Uno"
// ...
parameter String cmake = "C:\\Program Files\\CMake\\bin\\cmake.exe";
parameter String generator = "Visual Studio 17 2022";
// ...
- select
File > Saveto save your changes - open
Arduino.Examples.Blink - select the
Simulationribbon and clickSimulateto run the simulation - after the simulation has finished click the
Playbutton in theAnimation Controlsection and watch the LEDLblink

Simulating a Sketch with OpenModelica
- in OMEdit select
File > Load libraryand choose theArduinofolder from the extracted files - open
Arduino.Internal.buildSketchOMand set the default values for parameterscmakeandgeneratorto match your installation, e.g.
encapsulated function buildSketchOM
// ...
input String cmake = "C:/Program Files/CMake/bin/cmake.exe" "Absolute path to the CMake executable";
input String generator = "Visual Studio 17 2022" "CMake generator to build the Sketch";
// ...
- select
File > Saveto save your changes - open
Arduino.Examples.Blink - select
Tools > OpenModelica Compiler CLIand run
Arduino.Internal.buildSketchOM("Blink.ino")
to build the Arduino binary
- select
Simulation > Simulateto run the simulation - in the
Viewtoolbar click on the gears icon (Diagram Window) - in the
Variables Browserclick the play button and watch the LEDLblink

Simulate your own Sketch
To simulate your sketch MySketch you have to perform the following steps.
- save your sketch as
Arduino/Resources/Sketches/MySketch.ino - add the block
Arduino.Components.ArduinoUnoto your model - double-click the block and set the parameter
sketchto"MySketch.ino"
In Dymola:
- click
Translateto re-translate the model when the sketch has changed
In OpenModelica:
- run
Arduino.Internal.buildSketchOM("MySketch.ino")in the Compiler CLI when the sketch has changed
Limitations
-
currently you can use only one instance of ArduinoUno in your Modelica model
-
the whole Arduino API is supported except for the following functions
- analogReadResolution()
- analogWriteResolution()
- shiftOut()
- shiftIn()
- isWhitespace()
- Serial (
print(),println()andwrite()are supported) - Stream
- Keyboard
- Mouse
Arduino Libraries
The following libraries are included and can be used directly.
| Name | Description |
|---|---|
| Servo.h | Servo library to control RC (hobby) servo motors |
| PID_v1.h | PID controller library for Arduino |
To use an external library in your sketch copy its header files (*.h) and source files (*.cpp) to the Libraries folder. If the library contains .cpp files you have to add them to the Arduino project by dragging them from the Libraries folder in the file browser onto the Source Files folder in the solution explorer.
How it works
The ArduinoUno model is driven by an ExternalObject that contains the compiled sketch and an implementation of the Arduino API. The external object is synchronized at every sample step with the Modelica model.
When a model that contains the ArduinoUno block is translated the external object is automatically re-built through the preInstantiate=Arduino.Internal.buildSketch() directive in its annotation.
This function writes a new Sketch.cpp to Arduino/Resources/Source/Arduino that includes the sketch currently selected in the ArduinoUno component, calls CMake to generate the Visual Studio Solution, and builds the shared library Arduino/Resources/Library/{win32|win64}/ModelicaArduino.dll that contains the implementation of the external object.
Debugging a Sketch
- translate and simulate the model you want to debug
- open
Arduino/Resources/Source/Arduino/Win32/Arduino.sln(orArduino/Resources/Source/Arduino/x64/Arduino.slnifAdvanced.CompileWith64 = 2) - select
Debugas configuration - open the settings of the
ModelicaArduinoproject and underDebugsetCommandtodymosim.exeandWorking Directoryto your current Dymola working directory - open the sketch in Visual Studio and set a breakpoint
- start debugging
Copyright and License
Copyright © 2022 Dassault Systèmes. The code is licensed GPLv3, the documentation CC BY-SA 4.0.