I wrote code for data acquisition with the Adafruit 10-DOF sensor a while back. I will share that code, with explanation, so that you can adapt it yourself.
Mathematica + Adafruit == TRUE
It's awesome and easy to connect to an Arduino with Mathematica:
Open a connection to Arduino and specify the paths to the Adafruit libraries (I put the library folders in the same directory as my notebook):
(* Connect to the Arduino. *)
arduino = DeviceOpen["Arduino", "COM6"]
(* Required libraries. *)
DOF = NotebookDirectory[] <> "Adafruit_10DOF";
LSM = NotebookDirectory[] <> "Adafruit_LSM303DLHC";
SEN = NotebookDirectory[] <> "Adafruit_Sensor";
BMP = NotebookDirectory[] <> "Adafruit_BMP085_Unified";
OTH = NotebookDirectory[] <> "Adafruit_L3GD20_U";
Define the initialization code, which is prepended to the Arduino sketch by Mathematica (initializes the sensors):
(* Initialization code. *)
initCode = "
/* Assign a unique ID to the sensors */
Adafruit_10DOF dof = Adafruit_10DOF();
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(18001);
byte initQ = 0;
void initSensors() {
accel.begin();
mag.begin();
bmp.begin();
}";
You can tell Mathematica to include any Arduino function in the sketch. In my case I wanted a function for retrieving the Euler Angles (using the 10-DOF library from Adafruit). You define an Arduino-function like this (note that initSensors() from the initialization code is called the first time):
(* Function for retrieving euler angles. *)
getEuler = ArduinoCode[<|
"ArgumentTypes" -> {Integer},
"ReturnType" -> Real,
"Code" -> "
double getAngles(int t)
{
if (!initQ)
{
initQ = 1;
initSensors();
}
sensors_event_t accel_event;
sensors_event_t mag_event;
sensors_event_t bmp_event;
sensors_vec_t orientation;
accel.getEvent(&accel_event);
dof.accelGetOrientation(&accel_event, &orientation);
mag.getEvent(&mag_event);
dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation);
double currentReading[] = {orientation.roll, orientation.pitch, orientation.heading};
return currentReading[t];
}"|>];
The function takes an integer argument, which specifies whether it should return the roll, pitch, or heading. There's room for improvement here if you want to return all three values at once - but to me it didn't matter (and it won't to you since you're looking to acquire a single value).
Next, configure the Arduino, upload libraries plus sketch:
(* Configure the Arduino. *)
DeviceConfigure[arduino, "Upload" -> {
"Libraries" -> {"Wire", DOF, LSM, SEN, BMP, OTH},
Initialization -> initCode,
"Functions" -> <|
"GetEulerAngles" -> getEuler
|>
}];
That's it! You're ready to acquire data. I wrote a function to collect all angles, and created a table of values over 5 seconds:
(* Define function to get values. *)
allAngles :=
DeviceExecute[arduino, "GetEulerAngles", #] & /@ {0, 1, 2};
data = Table[Pause[0.1]; allAngles, {i, 50}];
Once done, don't forget to close the connection to the Arduino (or the COM port will stay busy):
(* Close connection. *)
DeviceClose[arduino];
Here's an example of my output (I was doing loops with the Arduino, like a flying ace):
ListPlot[Transpose @ data, Joined -> True]
You wanted live data acquisition which is equally easy, just call the function using dynamic or manipulate or whatever suits your need. If precision is important (e.g. for comparing the pressure at two different times) I recommend RunScheduledTask.