MathWorks - Mobile View
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
MathWorks
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out

Videos and Webinars

  • MathWorks
  • Videos
  • Videos Home
  • Search
  • Videos Home
  • Search
  • Contact sales
  • Trial software
  Register to watch video
  • Description
  • Full Transcript
  • Code and Resources

Plotting Live Data of a Temperature Sensor using Arduino and MATLAB

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.

Download Code and Files

Download Example Code

Related Products

  • MATLAB

Feedback

Featured Product

MATLAB

  • Request Trial
  • Get Pricing

Up Next:

3:38
Using MATLAB and Arduino to Acquire Analog Signals

Related Videos:

4:21
Using MATLAB and Arduino for Motor Control
33:09
Using Arduino with MATLAB and Simulink
5:42
Using MATLAB and Raspberry Pi with an I2C Sensor
2:59
How to Use Basic Plotting Functions

View more related videos

MathWorks - Domain Selector

Select a Web Site

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:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • Contact sales
  • Trial software

Explore Products

  • MATLAB
  • Simulink
  • Student Software
  • Hardware Support
  • File Exchange

Try or Buy

  • Downloads
  • Trial Software
  • Contact Sales
  • Pricing and Licensing
  • How to Buy

Learn to Use

  • Documentation
  • Tutorials
  • Examples
  • Videos and Webinars
  • Training

Get Support

  • Installation Help
  • Answers
  • Consulting
  • License Center
  • Contact Support

About MathWorks

  • Careers
  • Newsroom
  • Social Mission
  • Contact Sales
  • About MathWorks

MathWorks

Accelerating the pace of engineering and science

MathWorks is the leading developer of mathematical computing software for engineers and scientists.

Discover…

  • Select a Web Site United States
  • Patents
  • Trademarks
  • Privacy Policy
  • Preventing Piracy
  • Application Status

© 1994-2021 The MathWorks, Inc.

  • Facebook
  • Twitter
  • Instagram
  • YouTube
  • LinkedIn
  • RSS

Join the conversation