Reading from a sensor using MATLAB

I have an Arduino Uno connected to a printed circuit board that has a BME 280 commercial environmental sensor attached (see here) to measure the ambient pressure, temperature, and relative humidity. The C++ code that controls the BME sensor on the Arduino was obtained from here. It also requires you to download this. I can access the data through the Arduino IDE, but I want to be able to do everything in MATLAB as well. Basically, the question is, how can I command MATLAB to read from this sensor when it’s not directly connected to any of the Arduino board output pins?

The process for setting up the MATLAB-Arduino connection is described here. The first step (add-on package folder) is easy enough. The second step (MATLAB add-on class) pretty confusing. The last step (C++ header file) I can’t understand at all, since it’s been a very long time since I used C++ regularly. I’ll go in order to keep things simple, so first I’ll ask questions about the MATLAB add-on class file (this):

  • What are the “dependent libraries”? Are those the lines at the beginning of the .h file (there’s “Arduino.h”, “WProgram.h”, <Adafruit_Sensor.h> and <Wire.h>). Am I supposed to list those in the DependentLibaries line? Where are these files in the first place, i.e. does MATLAB need to know where to look for <Wire.h> somehow? If I was forced to guess, I’d leave this part blank, but I wanted to be sure.
  • I think “ArduinoLibraryHeaderFiles” is where you list <Adafruit_Sensor.h>, but how does MATLAB know where to look for this other .h file? Do I create another add-on package folder, even if I don’t use it directly? Also, <Adafruit_Sensor.h> calls its own .h files (namely, <stdint.h> and “Print.h”). What should I do about those?
  • Can the Resource Owner and Destructor things be ignored in my case? I would assume yes.
  • The GitHub link includes a .cpp file and a .h file. The .h file goes in the add-on package folder, but what do I do with the .cpp file? Am I supposed to transfer all of its processes and calculations (where it converts the voltage reading to temperature and pressure) to the .h file?

Any help would be greatly appreciated.

I know that wasn’t really a great question, so let me try to be more specific. Let me ask this:

The BME sensor is on a printed circuit board, which is connected to the Arduino Uno. The BME pins (data sheet) are not directly connected to any of the Arduino’s pins (I didn’t design the circuit), but the BME outputs each control a transistor, and the outputs from the transistors can be read at A4 and A5.

When I run the system through the Arduino IDE, I simply define the BME object then use bme.readTemperature(). The readTemperature() function is defined in the .cpp file that was downloaded from GitHub:

float Adafruit_BME280::readTemperature(void)
    int32_t var1, var2;

    int32_t adc_T = read24(BME280_REGISTER_TEMPDATA);
    if (adc_T == 0x800000) // value in case temp measurement was disabled
        return NAN;
    adc_T >>= 4;

    var1 = ((((adc_T>>3) - ((int32_t)_bme280_calib.dig_T1 <<1))) *
            ((int32_t)_bme280_calib.dig_T2)) >> 11;
    var2 = (((((adc_T>>4) - ((int32_t)_bme280_calib.dig_T1)) *
              ((adc_T>>4) - ((int32_t)_bme280_calib.dig_T1))) >> 12) *
            ((int32_t)_bme280_calib.dig_T3)) >> 14;

    t_fine = var1 + var2;

    float T = (t_fine * 5 + 128) >> 8;
    return T/100;

So for example, the coefficient bme280_calib.dig_T1 is defined as read16_LE(BME280_REGISTER_DIG_T1) in the .cpp file, and in turn BME280_REGISTER_DIG_T1 is assigned a hexadecimal number (0x88) in the .h file. I understand that the list of hexadecimal commands needs to be added to the MATLAB add-on class file.

When I run the Arduino through through MATLAB, I am under the impression I would use these commands (via sendCommand) to calculate the temperature (and pressure and humidity). The question is, where does the calculation go? Do I put the .cpp file somewhere in my MATLAB folders and tell the system to look at that? Do I transfer all the calculations done in the .cpp file to the .h file? Or do I do the calculations in the MATLAB script itself?

The Adafruit function is using I2C

to communicate with the BOSCH part. The Arduino Uno (with its ATmega328p) has I2C on the pins you listed (A4 and A5). The Adafruit software will use those pins to send I2C commands. I don't know anything about MATLAB but suspect it was designed with the ideas of standard programmable instruments (e.g. SCPI) in mind. These sort of instruments take textual commands and send back textual data (e.g. send "read", get back "1.10056"). If that is true then you want the Arduino to mimic SCPI (or something like it), so that they play nice.

I feel obligated to inform you that Python (rather than MATLAB) is probably a better choice to learn. I tend to use standard commands with JSON responses with Python, but it is possible to do outlandish things like remote procedure calls (RPC).