Clear Filters
Clear Filters

Using Arduino libraries in MATLAB

29 views (last 30 days)
I know this question has been asked before, but I'm very new to this and I don't understand the answers (like here and here and here and here), to be honest.
I have an Arduino Uno connected to a custom-made circuit board. The Arduino code for controlling it has already been written, and the necessary .h and .cpp files are all included and working. I want to see if I can operate the device with MATLAB, but I need to tell Arduino to include these extra libraries (one is for a real-time clock, one is for a pressure / temperature / humidity sensor, etc.). Is there are very simple explanation for how to do this? When I read this, I don't understand what parts I'm supposed to change for my specific case, what I need to keep, and what I can throw away.
Patrick Flanigan
Patrick Flanigan on 15 Nov 2018
I think I can pose a better, more specific question now.
I'm trying to run an Arduino Uno device through MATLAB. The Arduino is connected to a circuit board that has a Bosch BME 280 environmental sensor to measure the ambient pressure, temperature, and relative humidity. The guy who was in charge of this project before me got the sensor to work in Arduino code by downloading a .cpp and a .h file from GitHub (the files are a few years old, so they're not from here, but I guess they basically do the same thing).
I want MATLAB to read and save the data from this sensor. I understand that you're supposed to start from here. The add-on package folder is simple enough. For the MATLAB add-on class, I made the .m file, and I think I transferred over the commandID's correctly (they were in the .h file). When I get to the Library Specification part, there's a line for "DependentLibaries". The header file includes "Arduino.h", <Adafruit_Sensor.h> and <Wire.h> -- are these the dependent libraries I list in the .m file? For the second one, I have a Adafruit_Sensor.h file in my Arduino folder, but where does it go in the MATLAB add-on folder? What am I supposed to do about the "Arduino.h" and <Wire.h> things? I don't understand what's going on in the CppClassName line -- what is this supposed to be?
That's it for now, once I get past this I'll have some more questions about the next section.
Patrick Flanigan
Patrick Flanigan on 17 Dec 2018
Take 3. Let me try a better question:
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 directly.
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?

Sign in to comment.

Accepted Answer

Siddharth Bhutiya
Siddharth Bhutiya on 25 Oct 2018
As mentioned here, the add on library interface functions as a server client model.
You send a command using the "sendCommand" function from MATLAB and provide a commandID. This is sent to the Arduino which reads the commandID, runs the appropriate function from the C++ file and then uses the "sendResponseMsg" command to return any data that needs to be sent back.
The basic interface for these functionalities are provided in the arduinoio.LibraryBase class in MATLAB and librarybase.h in C++. In your add on you will build on top of these so that you can run your commands.
If we consider the simple HelloWorld example provided over here
First, we create a folder structure as mentioned in the link.
Second inside the C++ code we include LibraryBase.h and any other libraries used by our code. Then we create a class that inherits from the base class and inside the constructor we provide a name for the library and we register it so that the Arduino server knows that such a library exists.
After this, inside the command handler, we use switch case which will decide what piece of code will run when "sendCommand" sends a given commandID. So for each commandID you call the appropriate functions from your file and do whatever you want (get data from sensor, turn on a LED, etc.) This will complete the Arduino part of the add on.
After this, we set up the MATLAB interface that will provide the actual functions that we will call from MATLAB, to run our code on the Arduino.
To do this we create a class that inherits from the "arduinoio.LibraryBase" class. Inside it we define the commandIDs for each command, define any constants and specify the location of the header files.
The methods of this class will send a commandID to the arduino server, which in turn will run the appropriate function on the Arduino.
In the HelloWorld example, the read() function in HelloWorld class sends commandID 0x01 to the server. The server checks the commandID (inside the command handler) and then sends "Hello World" back to the read() function which in turn prints it.
The basic architecture for your code will be the same just the functions in the MATLAB Class will correspond to the functions that you want to execute and you will have to add the corresponding code inside the command handler.
Hope this helps.
  1 Comment
Patrick Flanigan
Patrick Flanigan on 30 Oct 2018
Thanks for giving so much help. To be honest, I'm still not totally sure of what direction I want to go with this; I'll try to come up with a more clear specific question if I can. It may be that I don't need to worry about adding on libraries.

Sign in to comment.

More Answers (0)


Find more on Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!