Monday, February 17, 2014

Vernier sensors and the Wolfram Language

Here’s another guest post from Allison at Wolfram Research. Today we’re looking at how to interface external sensors from Vernier Software & Technology to the Pi using the Wolfram Language.

Even though we only released the Wolfram Language on the Raspberry Pi a few months ago, Bob LeSuer is already a power user. He’s an Associate Professor of Chemistry at Chicago State University and is naturally a big proponent of incorporating technology into his teaching. In his lab classes, he uses instruments from Vernier, a company that creates a wide array of sensors for collecting data, from accelerometers and barometers to CO2 and pH sensors. But what particularly piqued our interest was that Bob came up with the idea to connect these sensors to the Raspberry Pi with the Wolfram Language! So of course we had to share.

Equipment you will need:

First, download the Vernier Software Development Kit for Linux and follow the instructions for unpacking the file. A few additional lines of code will need to be added to get it to run on the Pi.

In config.in, add:

AC_CONFIG_MACRO_DIR([m4])

And to Makefile.am, add:

ACLOCAL_AMFLAGS= -I m4

The following C code and MathLink template were used to create the functions getLibVersion, getDeviceInfo, and getSimpleMeasurements, which can be used in Mathematica. Download the two files and compile them with the following command:

mcc -o vernier vernier.c vernier.tm -I/usr/include/GoIO -lGoIO

Make sure the GoIO library is linked and that the /usr/include/GoIO directory is included in the search path.

And now, switching to the Wolfram Language, you can dynamically update the readings from your sensor with the following code:

link = Install["/mnt/documents/Pi/vernier/Vernier"]

Dynamic[{Column[{Row@StringSplit[getDeviceInfo[], ","][[{4, 5}]],
getSimpleMeasurement[]}], Clock[{1, 1}, 2]}]

The code will even continuously update with the appropriate readings as you swap sensors!

Similarly, you can make a plot update in real time, as shown below using a light sensor:

info = StringSplit[getDeviceInfo[], ","]

Dynamic@ListPlot[data, PlotLabel -> info[[4]],
Frame -> {True, True, False, False}, FrameLabel -> "Time (s)",
"Light " <> info[[5]]]

And this is just the start! There are an infinite number of ways now for this data to be analyzed and optimized with the Wolfram Language. You tell us—where would you go from here?

Bob's web page also has some more great ideas for using the Wolfram Language and the Raspberry Pi for experiments (like building a spectrometer using Lego!).

No comments:

Post a Comment