Go Down

Topic: Arduino to Matlab temperature data logger (Read 3781 times) previous topic - next topic

ZBay

Hey guys, I thought I would share this with everyone. I was originally looking to write a csv file and import it to excel to plot or otherwise manipulate the data however that proved a bit more difficult than I previously thought so I found and modified some code online which allows the Matlab to read the serial output. I believe octave is very close to Matlab's syntax so you may be able to use that if you don't have or want to purchase Matlab. So basically you set the circuit as described by Adafruit using the max31855 breakout board and a K type thermocouple. load this code into the arduino and leave the usb connected to the computer

Arduino:

Code: [Select]
#include <SPI.h>
#include "Adafruit_MAX31855.h"

// Default connection is using software SPI, but comment and uncomment one of
// the two examples below to switch between software SPI and hardware SPI:

// Example creating a thermocouple instance with software SPI on any three
// digital IO pins.
#define DO   3
#define CS   4
#define CLK  5
Adafruit_MAX31855 thermocouple(CLK, CS, DO);


void setup() {
  Serial.begin(9600);
  // wait for MAX chip to stabilize
  delay(3000);
}

void loop() {

   double c = thermocouple.readCelsius();
   if (isnan(c)) {
     Serial.println("Something wrong with thermocouple!");
   } else {
   
     // Comment out if you want Farenheit     
     Serial.println(c);
   
   }
   // Uncomment below to read in Farenheit
   //Serial.println(thermocouple.readFarenheit());

   // For longer test increase delay to avoid memory problems
   delay(0);
}


then copy this script into a matlab I believe the only thing you will need to change is the COM port you are currently using.

Matlab:

Code: [Select]

% MatLab <--> Arduino ,datalogger
tic;
clc;
numSec = 120;                    % Controls duration of test in seconds
Time = [];
Temp = [];

s1 = serial('COM4');            % define serial port (COM#)
s1.BaudRate = 9600;             % define baud rate
set(s1, 'terminator', 'LF');    % define the terminator for println
fopen(s1);

try                             % use try catch to ensure fclose
                                % signal the arduino to start collection
w = fscanf(s1,'%g');            % must define the input %d,%g, or %s, etc.
if (w == 'A')
    display('Collecting data');
    fprintf(s1,'%s\n','A');     
                               
end

i=0;
t0=tic;
while (toc(t0) <= numSec)
    i = i + 1;
    Time(i) = toc(t0);
    Time(i) = Time(i) - Time(1);
    Temp(i) = fscanf(s1,'%g');       % must define the input %d, %g or %s, etc.
end
fclose(s1);

% Plot Temp Vs Time with parameters
figure
plot(Time , Temp)
title('Temp Vs. Time')
xlabel('Time [Sec]')           
ylabel('Temp [Deg C]')             
axis([0 numSec min(Temp)-10 max(Temp)+10])


catch me
    fclose(s1);                 
end
toc


couple of quick notes on settings:

1) The delay at the bottom of the Arduino sketch controls the frequency in which data points are sent to Matlab

2) In Matlab the duration of the test is selected (in seconds) by the variable numSec. With the current settings it takes 1776 data points in a 2 min time frame. So this two settings may need to be adjusted for longer duration tests.

In the future I would like to get the everything to run of a GUI which can control start, stop, and different trials. It would also be nice if it had a graph which can be cycled through a few different graphs.

Thanks for reading, hopefully this post will help someone trying to tackle a similar project. I also welcome any feedback. 

 

francescofabbro89

hi! nice project!

i'm running a similar one fro "precision temperature measuring purpouses", but i'm still in the troubleshooting phase.

may i ask you a technical question? how accurate is your measure? I mean did you have the chance to run a test which compares the temperature you get with a pt100 or in a thermostatic bathe ?

i'm asking this because i acquire data from a k thermocouple as well but i use an AD595C IC instead, i just wonted to know how "competitors IC" behave :smiley-grin: .

Again, nice project!
cheers,
Francesco

ZBay

I have not had a chance to calibrate this yet however on a different different project running the same set up I was within 1 degree of much more expensive set up in the lab. So due to the large range a thermocouple operates in it wouldn't be the best choice for precision but depending on how exact it needs to be it will normally get by just fine. however if you go through the calibration process it should get closer but will probably make your code a bit more difficult. check out this link when you get the chance it may be helpful

https://learn.adafruit.com/calibrating-sensors/so-how-do-we-calibrate

rpurser

Another option would be to use the ThingSpeak Communication Library for Arduino, send the data to ThingSpeak, and then use its built in MATLAB support to do your analysis!
Visit www.mathworks.com/iot to learn more about analyzing your IoT data.

ZBay

Awesome rpurser I will have to give that a try too. I'm currently playing with a new version of the code which allows me to capture temp real time however it's not working like exactly how I would like. there seems to be a huge delay on when the change in temp is applied and when it logs it on the graph.

rpurser

Remember that ThingSpeak is rate limited to accepting data once every 15 seconds.  The graphs also poll once every 5 seconds (I think), so you may see a lag of up to 20 seconds.
Visit www.mathworks.com/iot to learn more about analyzing your IoT data.

Go Up