Madhu Govindarajan, MathWorks
MATLAB® can be used to quickly analyze and visualize data collected from sensors connected to Arduino®. Collect and plot live data from a temperature sensor and then analyze the data using some of the built-in MATLAB functions.
Download the code used in this video.
I've created a script called Temperature Logging and I've broken it down into sections. To run a section of code and advance to the next one, you can use the run and advance button inside MATLAB Editor. I have a snapshot from the data sheet of the DNP 36 sensor. And we can see the temperature is directly proportional to the voltage output that it gives. This is the equation. When I run the section, we can see what the temperature reading is in this room, both in Celsius and Fahrenheit.
I'm using the same equation to collect data for a specified period of time using tic and toc. We can see that it takes a long time to collect this data. This is because MATLAB sends a serial command to the device and receives a response every time to acquire a new data point. This is causing a bottleneck, which determines the fastest speed at which we can acquire data. We can also see that the data is pretty choppy.
First, let's calculate the frequency at which MATLAB can collect the data and then come to see why the data is choppy. So the frequency is about 72 hertz. The data is choppy because the Arduino we are using is an 8-bit device and it only reads values between 0 and 1023 on its analog pins. And remember, the voltage range was 0 to 5. So when we use these values, we can see that it corresponds to a reading of about 0.5 degrees centigrade and 1 Fahrenheit. That explains a little bit on why the data is so choppy, because even a small change in the voltage value means that there is a significant change in temperature.
Oftentimes, it is helpful to observe the data values as they are being collected. And during this, I am touching the temperature sensor with my finger to see how the measured value changes and if I can influence the temperature. So the function being used is animated line, which makes it easy to stream new data to a plot. The other useful feature of this function is that the data is automatically stored in the plot and we don't have to log it. I have set up the script so when I push the button, it stops collecting data.
Let us take a look at the data that we collected. This data definitely needs some post-processing. The high-frequency noise in the temperature signal can be removed by applying a moving average filter. From the datasheet, we know that the temperature sensor has a tolerance of about 2 degrees centigrade at room temperature. So using this information, I have calculated the largest and smallest possible value and plotted both the collected and the process data.
The continuous line here is the filtered data. And the temperature sensor tolerance is represented using the dotted lines in this figure. Now that we have done all the analysis and can see the data, we might want to save the data that we collected to a file. One way to do this is to store the data in a table and then write the table itself to a file. You can see that the file has all the necessary information and the collected data itself. Thanks, everyone.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .Select web site
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.