I'm trying to learn how to connect Mathematica to my Arduino Uno. Following the Examples in the reference documentation, I've done the following:

  • connected my Arduino to my Windows 10 pc via USB; it's on COM3.
  • attached a wire from pin 5 to the positive terminal of an LED; the LED ground goes through a small resistor and back to the Arduino GND. (The wiring is sound; I can use this configuration to blink the LED through an Arduino sketch.)
  • I connect Mathematica to the Arduino by running arduino = DeviceOpen["Arduino", "COM3"]. The output shows I've connected; my Arduino blinks as though a new sketch has been uploaded.
  • Following the documentation, I expect the command DeviceWrite[arduino,5->1 ] to set digital pin 5 to 1 and turn on the LED. But nothing seems to happen.
  • I tried running, after DeviceOpen..., the command DeviceConfigure[arduino, "PinConfigure" -> <|5 -> "Output"|>], then using DeviceWrite. But that also doesn't seem to do anything.

I've searched online for examples of how to connect Mathematica with an Arduino. Older posts, such as this one, work for me, where an Arduino sketch is uploaded through the IDE and Mathematica establishes a generic serial link. I am just trying to make the "Arduino" device connection commands work.

Can anyone point me to a basic example of using these newer commands?


To extend my comment, the 'newer' commands of yours are still relatively old...particularly since the fantastic new development of Microcontrollerkit

The new basic method for connection and then subsequent programming is much different than before, there are many examples in the documentation, and a most basic example, I will give as following:

First we Needs Micrcontrollerkit and make a Nonlinear Statespace model of some kind (an old one I used before)

ssm = NonlinearStateSpaceModel[{{}, {23.886653196329103` (-\[Pi] + \[Alpha][t]) + 2.314882341444953` Derivative[1][\[Alpha]][t] + 0.022889791281586705` Derivative[1][\[Phi]][t]}}, {}, {{\[Alpha][t], N@\[Pi]}, \[Alpha]'[t], \[Phi]'[t]}]

All functions state space models in MCK must be discrete...set a sample time, turn to discrete.

dd = 10 10^-3;
ssmd = ToDiscreteTimeModel[ssm, dd]

The NSSM has 3 analog inports, and one output...open a connection to the device and embed code (in this example we arn't connecting directly to it, just generating the code to reading) if we wanted to the device itself, we'd choose the connection port specific to OS.

mM = MicrocontrollerEmbedCode[ssmd, <|"Target" -> "ArduinoUno", "Inputs" -> {"A0" -> "Analog", "A1" -> "Analog", "A2" -> "Analog"},"Outputs" -> {"Serial"}|>, <|"ConnectionPort" -> None|>, <|"Language" -> "Wiring"|>]

And that's pretty much it. Read the documentation and the examples, they're very straight forward....as of MMA 12.2, 32bit devices are now supported!

Basic serial connection, can be as simple as this:

 StateSpaceModel[{{}, {}, {}, {{1}}}, SamplingPeriod -> 1, 
  SystemsModelLabels -> None], <|"Target" -> "ArduinoUno", 
  "Inputs" -> 0 -> "Serial", "Outputs" -> 1 -> "Serial", 
  "Serial" -> "BaudRate" -> 57600|>, "/dev/cu.usbmodem14101"]
  • $\begingroup$ Thanks for the reply and for pointing to the new Microcontrollerkit. I'm a math prof. and have experience with MMA but little with microcontrollers, so this is very new to me. I find the examples are written for those who already know much of what they need; they aren't tutorials. So to use MicrocontrollerEmbedCode, I first need to learn about System State Models; I have no idea what your nonlinear system is designed to do. I also don't understand your last example; I ran the code (replacing the last string with "COM3" for my computer) and ... I don't know what it does. $\endgroup$
    – GregH
    Dec 23 '20 at 16:55
  • $\begingroup$ @GregH Ahh Yes...well I'd suggest buying one of the kits that they use an example and literally follow it step by step $\endgroup$ Dec 23 '20 at 18:19
  • $\begingroup$ @GregH wolfram.com/language/12/microcontroller-kit/… for example...my statespace model was a LQR controller....a MISO system. $\endgroup$ Dec 23 '20 at 18:20

This is what worked for me in MMA 12.0.0 on Windows 10 with a Uno clone:
First, set up the Uno like this

 "ArduinoInstallLocation" -> "C:\\Program Files (x86)\\Arduino"]

dev = DeviceOpen["Arduino", "COM3"]

DeviceConfigure["Arduino", "Upload" -> {"BootFunction" -> <|"Code" ->
      "void bootFunc(){pinMode(5, OUTPUT);}"|>}]

Wait a few seconds while the sketch is uploaded to the Arduino. Then blink the LED on pin 5 ten times:

val = 1;
Do[(DeviceWrite["Arduino", <|5 -> val|>];
  val = 1 - val;
  Pause[0.5]), {20}]

The DeviceConfigure that set the ArduinoInstallLocation was not necessary in later tests. Keeping it is recommended, to confirm that MMA can find the required Arduino IDE software. Note that the variable dev is not used, so it doesn't really need to be defined.

DeviceWrite["Arduino", <|5 -> 1|>] and DeviceWrite["Arduino", <|5 -> 0|>] also worked to turn the pin 5 LED on and off.

The big payoff for me was finally finding a use for Do.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.