Create a Custom Arduino Add-On Library - MATLAB
Video Player is loading.
Current Time 0:00
Duration 6:01
Loaded: 4.97%
Stream Type LIVE
Remaining Time 6:01
 
1x
  • Chapters
  • descriptions off, selected
  • captions off, selected
      Video length is 6:01

      Create a Custom Arduino Add-On Library

      Learn how to write a custom Arduino® add-on library. Using the DHT22 temperature and humidity sensor as an example, learn how to create the add-on library to interface your Arduino hardware with MATLAB®. A custom Arduino add-on allows you to use your Arduino hardware and attach shields or sensors easily in MATLAB.

      Published: 27 Jan 2023

      This video shows how to create a custom Arduino add-on library. As an example, I'll show you how to write an add-on library for the DHT22 Temperature and Humidity Sensor. An add-on library allows you to interface your MATLAB code with the Arduino hardware.

      Before we start, make sure you have the MATLAB Arduino support package installed, and also downloaded the third-party Arduino libraries you will use, and drag them into your default folder path. Depending on your operating system, the default folder path may be different.

      For the add-on library, you only need to write two files-- a C++ header file, which is downloaded to the Arduino, and the second file is a MATLAB class to send commands to the Arduino and get responses. There are three steps to create an add-on. The first step is to set up the folders. The next step is to write the C++ header file. And the last step is to write the MATLAB add-on class file.

      Firstly you must create a folder called +arduinoioaddons with a subfolder whose name starts with a plus. This is where you'll keep your MATLAB class file, in another subfolder called src, which has the C++ header file.

      Now, let's look at how to write the C++ header file. Be sure to include LibraryBase.h to extend the class needed to create your add-on library. Also, include any header files needed for your custom code. In my case, I included DHT.h.

      Next, define a set of command IDs for later use, to deal with commands sent from MATLAB. As you can see, I have defined these four command IDs for my sensor. Now, define your add-on class and make it inherit from library base, since it's a requirement for the add-on framework. You can also define any members needed for your class. For the constructor, make sure the name matches your class, and then define the name of your add-on library.

      Next, override the command handler method, which is called every time MATLAB sends a command to the Arduino. We are passed the command ID, data in, and the size of that data. The Arduino uses the command ID to determine which method to execute. Here you can wrap the third-party methods.

      In my case, I wrapped the methods from the Adafruit DHT library. When you send a command from MATLAB, you can pass data to use here. In this example, I had MATLAB send the sensorID and the pin, to create the new sensor object.

      It is required by the framework that, for every command you send, you must send back data to MATLAB using the method sendResponseMsg. If you'd like to get debug information, you can also define debug strings and send the information back to MATLAB using debugPrint. In my code, I do the same for all the command IDs-- create, delete, read temperature, and read humidity.

      Now, you'll write the MATLAB class. Create the file in the +Adafruit folder. This class was inherited from LibraryBase. Set these properties as the same command IDs we used in the header file. These properties are also required for the framework. Replace the LibraryName with your library, add any dependent and third-party libraries, and change these to match your C++ header file and class name. You can also add more properties, depending on what you need for your add-on.

      Write a constructor to initialize the add-on. You must assign the Arduino object as the add-on object's parent, which is inherited from library base. Here, I do some error handling. Then, I initialize the Pin property, configure the Pin to digital input, and call the method to create the sensor.

      For each method you want to execute on the Arduino, you must use sendCommand. You must pass the command ID, library name, and any necessary data to the Arduino. Here, I pass the SensorID and terminal. To receive data back from the Arduino, save the return data from sendCommand. For example, in the readHumidity method, I saved the humidity value that is returned.

      After writing those two files, in the command window use addpath to add the location of your add-on folder. You can check by using the command listArduinoLibraries, to see if your library is available. Connect the Arduino hardware to MATLAB using the Arduino method with a port, board, and library. To see debug messages use Traceon, true.

      Now, connect to the sensor using the add-on method. In my case, I will pass the Arduino objects, library, and pin. We can call the readTemperature and readHumidity methods to read the measurements from the sensor and use the data in MATLAB.

      That's all you need to do to write your own add-on libraries. For more help, refer to the documentation on how to create a custom add-on library for Arduino.

      View more related videos