Skip to content
MathWorks - Mobile View
  • Sign In to Your MathWorks AccountSe connecter
  • Access your MathWorks Account
    • Mon compte
    • Mon profil
    • Mes licences
    • Se déconnecter
  • Produits
  • Solutions
  • Le monde académique
  • Support
  • Communauté
  • Événements
  • Obtenir MATLAB
MathWorks
  • Produits
  • Solutions
  • Le monde académique
  • Support
  • Communauté
  • Événements
  • Obtenir MATLAB
  • Sign In to Your MathWorks AccountSe connecter
  • Access your MathWorks Account
    • Mon compte
    • Mon profil
    • Mes licences
    • Se déconnecter

Vidéos et webinars

  • MathWorks
  • Vidéos
  • Vidéos
  • Recherche
  • Vidéos
  • Recherche
  • Contactez l'équipe commerciale
  • Version d'essai
3:45 Video length is 3:45.
  • Description
  • Full Transcript
  • Code and Resources

Plotting Live Data of a Temperature Sensor using Arduino and MATLAB

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

Bridging Wireless Communications Design and Testing with MATLAB

Read white paper

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: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (简体中文)
  • 中国 (English)

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

  • Contactez l'équipe commerciale
  • Version d'essai

MathWorks

Accelerating the pace of engineering and science

MathWorks est le leader mondial des logiciels de calcul mathématique pour les ingénieurs et les scientifiques.

Découvrir…

Découvrir les produits

  • MATLAB
  • Simulink
  • Version étudiante
  • Support Hardware
  • File Exchange

Essayer ou acheter

  • Téléchargements
  • Version d'essai
  • Contacter un commercial
  • Tarifs et licences
  • Comment acheter

Se former

  • Documentation
  • Tutoriels
  • Exemples
  • Vidéos et webinars
  • Formation

Obtenir de l'aide

  • Aide à l'installation
  • MATLAB Answers
  • Services de consulting
  • Centre de gestion des licences
  • Contacter le support technique

La société

  • Offres d'emploi
  • Actualités
  • Mission sociale
  • Témoignages clients
  • La société
  • Select a Web Site United States
  • Trust Center
  • Marques déposées
  • Charte de confidentialité
  • Lutte anti-piratage
  • État des applications

© 1994-2022 The MathWorks, Inc.

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

Rejoignez la conversation