Using MATLAB and Arduino to Acquire Analog Signals

In this demo, we are going to see how to measure temperature using an Arduino board and MATLAB. The same technique can also be used to measure other analog signals. For this, I have MATLAB and MATLAB Support Package for Arduino pre-installed. If you do not have the support package installed, then you can go to Add-ons tab and click on Get Hardware Support Package to download and install the support package.

I am using an Arduino Due, TMP 36 temperature sensor, and a USB-Micro B cable for this demo. To measure the temperature using MATLAB, connect your TMP 36 sensor. Pin 1 to 5 volt on the Due. Pin 3 to GND. And Pin 2 to A7 on the Due. Remember, the USB cable must be connected to the programming port on the Due.

To get started with MATLAB Support Package for Arduino, let us go to the documentation home using one of the many ways. Type doc on MATLAB command window, then scroll down on the page that just opened up inside the hub browser. In the bottom-right corner, a list of all the installed support packages and links to their homepage is available.

I'm going to click on Getting Started and find out how I can connect to Arduino board from MATLAB. I'm going to copy the code snippet that is given here and paste it on MATLAB command window to establish a connection with the first available Arduino board that is connected to this PC.

Our next task is to measure the temperature sensor's analog signal using MATLAB. Search Arduino analog. The first suggestion provided by MATLAB is lead voltage function, which can read analog voltage from Arduino boards. Let us go to MATLAB command window and see if we can measure the analog signal using this function.

I repeated the function a few times to make sure that I'm reading the stabilized voltage from the sensor. Now, the next task is to convert this value of voltage to temperature. For this, I'm going to explain when you can use a function in MATLAB. Functions must be used to perform a repetitive task such as this one for different input values. I have a function here called voltToFa, which will convert the voltage values of the temperature sensor to Fahrenheit.

The first line in this function converts the voltage to Celsius using the formula given in the manual for the sensor. The second line converts this Celsius value using the standard formula to a Fahrenheit value. A script, on the other hand, can be used to perform a repetitive task. However, it cannot take in input values.

I have a script here called temp-from-sensor that uses read voltage function that we just learned about and the function voltToFa to measure the voltage from the sensor and convert it to temperature. To show the effectiveness of the temperature sensor, I've connected a webcam to monitor the thermostat in my office. I obtained a stream of images from the webcam, and I am displaying it along with the temperature's value. For this, I'm using the Preview function available in MATLAB Support Package for webcams.

With that, we have seen how to measure temperature or any analog signal using MATLAB and an Arduino board.

Product Focus