Note : this code is now obsolete, superseded by the built-in Mathematica Arduino driver starting in version 10.1
Hello all, today I am going to demonstrate the use of Arduino Drivers I recently developed using the new functions in Mathematica 10. This demonstrations uses the following:
All of the hardware components can be found in the Official Arduino Starter Kit and almost all similar Arduino kits.
The functions I developed should in theory be system independent, i.e. run on Linux, Mac OS X, Windows, etc., but I used Windows to develop the functions, so I can only confirm that it works on Windows. I would love for others to try this out on Linux and Mac OS X and post their results.
The first step will be to upload the sketch I developed for the Arduino to your Arduino. You can do this using the Arduino software.
You can download the sketch that is attached to this post. The filename was changed from a .ino to a .txt, so you may need to change it back before the Arduino software recognizes it.
Now select your Arduino board (I developed this for the Uno, so can only confirm that the Uno works, but again in theory should work on any Arduino), and open the .ino file you downloaded and upload it to the board.
After uploading the sketch to the Arduino, connect your Arduino and determine which Serial port your Arduino is attached on. One easy way to do this is to open the Arduino software and go under Tools -> Serial Port. On Windows machines this is a COMXX, where XX is some non-negative integer. On Macintosh machines, it is generally "/dev/tty.usbmodemXXXXXX" or "/dev/tty.usbserialXXXXXX", where X is some string of letters or numbers. In Linux, it is generally "/dev/ttyXXXX",where XXXX is some string of letters and numbers. Keep this port in mind as we will need it in a second.
Now the last setup step is to install the Driver package. It can be downloaded as an attachment to this post. Download it and open up Mathematica 10 and run
<< "(path_to_file)/ArduinoDriver.m"
Where (path_to_file) is wherever you downloaded the file to, for instance on Windows this might be C:\Users\Ian\Downloads\ArduinoDriver.m
Now, to open a connection to the device we use
arduinoObject = DeviceOpen["Arduino", "COM4"]
Where "COM4" is my serial port, replace that with whatever port your Arduino is attached on.
Now for some basic device control, we can use
DeviceWrite[arduinoObject, <|pinNumber -> 1|>]
To control the digital state of pinNumber, which should be an integer between 2 and 13, or a string corresponding to one of the analog pins, i.e. "A0" through "A5". Note that the strings must be capitalized.
Now attach an LED as shown in the Fritzing diagram below.

We can now blink the LED with the following code.
Do[(
DeviceWrite[arduinoObject, <|pinNumber -> 1|>];
Pause[1];
DeviceWrite[arduinoObject, <|pinNumber -> 0|>];
Pause[1]),
{5}]
This will blink an LED attached to pinNumber 5 times, for one second each time. Note that pinNumber which must be defined elsewhere. For the circuit above it should be equal to 11.
We can also use the "analog" write functionality of the Arduino with DeviceWrite, using the following syntax
DeviceWrite[arduinoObject, {pinNumber, "analogOutput", state}]
Where pinNumber is a valid pin value, and state is an integer between 0 and 255, corresponding to the 8 bit resolution of the PWM pins.
If your LED was attached as in the picture above, using the following will allow you to adjust the brightness of the attached LED. Note that as the value is dynamically changed, the brightness jumps around, this is a known bug.
Manipulate[
DeviceWrite[arduinoObject, {11, "analogOutput", analogWriteValue}],
{{analogWriteValue, 0, "Value"}, 0, 255, 1}]
We can also read values from pin's states from the Arduino using DeviceRead. Before we can read values though, we need to configure the values using the function DeviceConfigure. To configure pinNumber as a digital input, we can use
DeviceConfigure[arduinoObject, <|pinNumber -> "digitalInput"|>]
This also works with analog pins, with the same syntax, except with "analogInput" instead of "digitalInput". Note that this is expandable to multiple pins, as it uses an association. In that case, we would just add another key-value pair to the association. This also works to configure a pin to be an output with "digitalOutput". Now that the pin is configured correctly, the Arduino will begin outputting values over serial, and the Serial TX light should light up on your device.
Let's try reading a pin with the following simple circuit, using a button and a resistor. 
With the circuit built, to read values, we use
DeviceRead[arduinoObject, pinNumber]
This returns the value of the pin as either a 0, corresponding to a low voltage, or a 1, corresponding to a high voltage.
Evaluating the previous command while holding down the button should return a 1, while without holding down the button should return a 0, if the circuit is built as described. The Serial read buffer may take two evaluations to be updated the very first time this is evaluated, but after that it should change properly.
If pinNumber was an analog input, this would return a value between 0 and 1023, corresponding to the 10 bit resolution of the analog to digital converter inside the Arduino. This function is also compatible with lists of pin numbers, where an association of pins to values is returned. Note that pinNumber can be either a non-negative integer between 2 and 13, or a string of the type "AX", where X is a non-negative integer between 0 and 5. This is done as follows
DeviceRead[arduinoObject, {pinNumber1, pinNumber2, ...}]
This returns an association of the type <|pinNumber1->state1, pinNumber2->state2, ... |>.
Let's also set up another circuit to demonstrate the analog input functionality. This would be ideally done with a joystick, but it can be done just as easily with two potentiometers (which is basically all a joystick is).

The following code will display a circle at the coordinates, which are derived from the two potentiometers. The first thing we need to do is setup the pins to be analog inputs. We can get both values from DeviceRead as an association, which we then get the values from using Values, then divide the coordinates by 1023 to map the circle's coordinates to between 0 and 1. We also subtract the values from 0.5 to center the circle at the origin.
DeviceConfigure[arduinoObject, <|"A0" -> "analogInput", "A1" -> "analogInput"|>];
Dynamic[Graphics[{Blue,
Circle[ (Values[DeviceRead[arduinoObject, {{"A0", "A1"}}]])/1023 - .5, 0.1]},
Axes -> True, ImageSize -> {800, 800},
PlotRange -> {{-.75, .75}, {-.75, .75}}],
UpdateInterval -> .5]

Thanks!
Attachments: