Setting up Your Machine

The code generation is handled completely within the Wolfram System. However, to compile and download the code it invokes an external toolchain.

First load the package.

Load the package.

Needs["MicrocontrollerKit`"]

If you are working with a microcontroller on an Arduino board, (PackageLink[MicrocontrollerEmbedCode, paclet:MicrocontrollerKit/ref/MicrocontrollerEmbedCode, MicrocontrollerKit Package Symbol]) looks for the Arduino IDE. To see if the compiler and other programs can be successfully invoked run a test to blink the LED on the board. For example, if you have an Arduino Uno run the following code after modifying the last argument to match the connection port of your machine.

Blink the LED on the Arduino Uno.

sys=(NonlinearStateSpaceModelFull[Which[x>0,0,True,1], x, x, \[Null], 1, {}Automatic, ]);
μc=<|"Target"->"ArduinoUno","Outputs"->13->"Digital"|>;
p="/dev/cu.usbmodem14101";

MicrocontrollerEmbedCode[sys,μc,p]

If the LED is blinking every second then the Arduino toolchain is being correctly invoked. If not, it must be explicitly specified.

Specify the path to the compiler.

ardCompInstl=FileNameJoin[{$RootDirectory,"Applications","Arduino.app","Contents","Java","hardware","tools", "avr","bin"}];

FileExistsQ[FileNameJoin[{ardCompInstl,"avr-gcc"}]]

MicrocontrollerEmbedCode[sys,μc,p,<|"CompilerInstallation"->ardCompInstl|>]

If an avr-gcc toolchain has been installed elsewhere, that can be used as well.

Use a different avr-gcc compiler.

compInstl=FileNameJoin[{$RootDirectory,"usr","local","bin"}];

MicrocontrollerEmbedCode[sys,μc,p,<|"CompilerInstallation"->compInstl|>]

Explicitly specify the compiler's name.

MicrocontrollerEmbedCode[sys,μc,p,<|"CompilerInstallation"->compInstl,"CompilerName"-> "avr-gcc"|>]

The path "/usr/local/bin" is in the system path.

Environment["PATH"]

If it is in the system path, the path need not be explicitly given.

Use the avr-gcc compiler that is in the system path.

MicrocontrollerEmbedCode[sys,μc,p,<|"CompilerInstallation"->Sequence[]|>]