Вы находитесь на странице: 1из 76

2016.10.

05

Data Logging and Analysis


with Arduino and MATLAB

Hans-Petter Halvorsen, M.Sc.


System Overview
Arduino

Acquiring Data
Sensors
Data Logging Cloud/Network
Your PC
OPC
Analysis

OPC DA/UA Servers


MATLAB
Keywords
Sensors and Actuators Overview in general
Overview of Temperature Sensors
Arduino and Microcontrollers
MATLAB
OPC DA and OPC UA
Data Analysis
Basic Data Acquisition (DAQ), Data Logging
Calibration, Uncertainty, Resolution, Accuracy, Range, etc.
Lowpass Filter implementation in Software
Network Communication
Reading Data sheets
Learning Goals
Learn more Programming, Arduino and MATLAB
Programming
Learn about Microcontrollers (Arduino)
Learn more about Sensors and Actuators
Learn about Hardware-Software Interactions
Learn more about OPC, especially OPC UA
Learn more about Electronics and Electrical Components
Learn Practical Skills and Implementations
Learn more about Troubleshooting and Debugging
Lab Assignment Overview
1. Get Sensor Data using Arduino IDE
2. Get Sensor Data from Arduino into
MATLAB
3. Use of OPC DA and OPC UA in MATLAB
4. Plotting and Analysis of Data in
MATLAB/LabVIEW
See next slides for details...
Software Software

MATLAB

Arduino IDE
Matrikon OPC
Hardware
Hardware

Arduino/
Genuino Breadboard

Your Personal Computer


https://www.arduino.cc/

Sensors and
Actuators NTC
Thermistor
Potentiometer

Network Equipment (if needed) LED TMP36


Push Button
The teacher have not done Very often it works on one computer but
not on another. You may have other
all the Tasks in detail, so he versions of the software, you may have
may not have all the installed it in the wrong order, etc...
answers! That's how it is in In these cases Google is your best friend!
real life also!

The Teacher dont have all the answers (very few actually L)!! Sometimes you just need to Google in
order to solve your problems, Collaborate with other Students, etc. Thats how you Learn!
Troubleshooting & Debugging
Visual Studio Use available Resources
Use the Debugging Tools in your such as User Guides,
Programming IDE. Datasheets, Text Books,
Visual Studio, LabVIEW, etc. have great Tutorials, Examples,
Debugging Tools! Use them!! Tips & Tricks, etc.
My System
is not
Working??
Google It!
Your hardware device most likely works, so
You probably will find the you don't need a new device! Still not working
answer on the Internet after Troubleshooting & Debugging? Fill out
an Equipment Error Form

Check your electric circuit, electrical cables, DAQ device, etc. Check if
Another person in the world probably the wires from/to the DAQ device is correct. Are you using the same
had a similar problem I/O Channel in your Software as the wiring suggest? etc.
Sensors and Actuators

Hans-Petter Halvorsen, M.Sc.


Sensors and Actuators Theory

A Sensor is a converter that measures a physical quantity and


converts it into a signal which can be read by an observer or by
an (today mostly electronic) instrument.
An Actuator is a type of motor for moving or controlling a
mechanism or system. It is operated by a source of energy,
typically electric current, hydraulic fluid pressure, or pneumatic
pressure, and converts that energy into motion. An actuator is
the mechanism by which a control system acts upon an
environment.

http://en.wikipedia.org/wiki/Sensor http://en.wikipedia.org/wiki/Actuator
Sensors Theory

Calibration: A comparison between measurements. One of


known magnitude or correctness made or set with one device
and another measurement made in as similar a way as possible
with a second device. The device with the known or assigned Accuracy: How close the
correctness is called the standard. The second device is the unit measured value is the the
under test, test instrument, or any of several other names for actual/real value, eg., 0.1 %
the device being calibrated.

Resolution: The smallest change it can detect in the


quantity that it is measuring. The followig formula may be
used (where S is the measurment span, e.g., 0-100deg.C):

In the assignment you need to deal with these parameters. You find information about these parameters in the Data
sheet for your device
http://en.wikipedia.org/wiki/Calibration
http://en.wikipedia.org/wiki/Measurement_uncertainty http://en.wikipedia.org/wiki/Accuracy_and_precision
Measurements and Sensors

Measurement Fundamentals: http://www.ni.com/white-paper/4523/en/

Sensor Fundamentals: http://www.ni.com/white-paper/4045/en/

Sensor Terminology: http://www.ni.com/white-paper/14860/en/


Arduino

Hans-Petter Halvorsen, M.Sc.


Arduino is an open-source physical computing platform
designed to make experimenting with electronics and
programming more fun and intuitive.
Arduino has its own unique, simplified programming language
and a lots of premade examles and tutorials exists.
With Arduino you can easily explore lots of small-scale sensors
and actuators like motors, temperature sensors, etc.
The possibilities with Arduino are endeless.
http://www.arduino.cc
Arduino UNO
https://www.arduino.cc/en/Guide/HomePage

http://www.arduino.cc

Pin Overview: http://pighixxx.com/unov3pdf.pdf


Arduino Software Software
Programming with Arduino is simple and intuitive!
Arduino Sketch IDE
Example:
// include the TinkerKit library
#include <TinkerKit.h>

// creating the object 'led' that belongs to the 'TKLed' class


TKLed led(O0);

void setup()
{
//do something here
}

void loop()
{
led.on(); // set the LED on
delay(1000); // wait for a second
led.off(); // set the LED off
delay(1000); // wait for a second
}

The syntax is similiar to C programming This program makes a LED blink

Software Installation: http://arduino.cc/en/Main/Software


Arduino Uno Board
Arduino Basics Breadboard

Sensors and Actuators, etc.

18
The Arduino Kit
Arduino Uno Board

Small-size Sensors and Actuators

Getting Started with Arduino: http://arduino.cc/en/Guide/HomePage


The Arduino Kit
Ardiono Home Page: http://arduino.cc
The Arduino Starter Kit:
http://arduino.cc/en/Main/ArduinoStarterKit
Starter Kit Videos:
https://www.youtube.com/playlist?feature=edit_ok&list=PLT6
rF_I5kknPf2qlVFlvH47qHvqvzkknd
Getting Started
Arduino with Breadboard Sensors/Actuators:
LED https://www.arduino.cc/en/Tutorial/BuiltInExamples

Push Button Install the Arduino Sketch and Explore some of


these Sensors & Actuators, i.e., make 2-3
Potentiometer Examples.
Use the breadboard for creating your circuits.
etc. Use Arduino Sketch in order to create the
Programs that interface with the Sensors &
Actuators
Just don't copy the Examples! Make it from scratch in your
own way! You need to understand what's happens! Play
and Explore! Add Value to your code!

The Arduino IDE (File->Examples) comes with lots of examples and at www.arduino.cc you find more!
Temperature Sensors

Make the following Temperature Sensors work with Arduino:

NTC Thermistor TMP36

Make sure to compare results from these Sensors


Small-scale Temperature Sensors
TMP36

https://www.sparkfun.com/products/10988
https://www.elfa.se/elfa3~eu_en/elfa/init.do?item=73-889-29&toc=0&q=73-889-29

NTC Thermistor

https://www.elfa.se/elfa3~eu_en/elfa/init.do?item=60-260-41&toc=0&q=60-260-41
Tutorial: http://garagelab.com/profiles/blogs/tutorial-using-ntc-thermistors-with-arduino
Theory

TMP36 These sensors use a solid-state technique to determine the


temperature. That is to say, they don't use mercury (like old
thermometers), bimetalic strips (like in some home thermometers or
stoves), nor do they use thermistors (temperature sensitive resistors).
Instead, they use the fact as temperature increases, the voltage across
a diode increases at a known rate. (Technically, this is actually the
voltage drop between the base and emitter - the Vbe - of a transistor.)
By precisely amplifying the voltage change, it is easy to generate an
analog signal that is directly proportional to temperature. There have
been some improvements on the technique but, essentially that is how
temperature is measured.

Because these sensors have no moving parts, they are precise,


never wear out, don't need calibration, work under many
environmental conditions, and are consistant between sensors
and readings. Moreover they are very inexpensive and quite
easy to use.

https://learn.adafruit.com/tmp36-temperature-sensor
Datasheet Calculations Theory

From the plot we have:

(x1, y1) = (750mV, 25C)


(x2, y2) = (1000mV, 50C)
Linear relationship:

You have to find a (slope) and b (intercept):


y-25C = ((50C-25C)/(1000mV-750mV)) * (x-750mV)
This gives: y[C] = (1/10)*x[mv]-50
Voltage-based Sensors
According to the TMP36 datasheet, the relation of the output voltage
to the actual temperature uses this equation: TMP36
y[C] = (1/10)*x[mv]-50
Where the voltage value is specified in millivolts.
However, before you use that equation, you must convert the integer value that the
analogRead function returns into a millivolt value.
10-bit analog to digital converter
You know that for a 5000mV (5V) value span the analogRead function will return
1024 possible values:
voltage = (5000 / 1024) * output
Where mV

output = analogRead(aichannel)
0-1023 A0-A5
TMP36 Temperature Wiring
TMP36 Temperature Sensor Example
// We'll use analog input 0 to read Temperature Data

const int temperaturePin = 0;

void setup()
{

}
Serial.begin(9600); Serial Monitor
void loop()
{
float voltage, degreesC, degreesF;

voltage = getVoltage(temperaturePin);

// Now we'll convert the voltage to degrees Celsius.


// This formula comes from the temperature sensor datasheet:

degreesC = (voltage - 0.5) * 100.0;

// Send data from the Arduino to the serial monitor window


Serial.print("voltage: ");
Serial.print(voltage);
Serial.print(" deg C: ");
Serial.println(degreesC);

delay(1000); // repeat once per second (change as you wish!)


}

float getVoltage(int pin)


{
return (analogRead(pin) * 0.004882814);

// This equation converts the 0 to 1023 value that analogRead()


// returns, into a 0.0 to 5.0 value that is the true voltage
Just don't copy the Example! Make it from scratch in
}
// being read at that pin.
your own way! You need to understand what's
happens! Play and Explore! Add Value to your code!
Resistance-based Sensors Theory

The problem with resistance sensors is that the Arduino analog


interfaces cant directly detect resistance changes. Thermistor
This will require some extra electronic components. The easiest way to
detect a change in resistance is to convert that change to a voltage
change. You do that using a voltage divider, as shown below.

By keeping the power source output constant, as the


resistance of the sensor changes, the voltage divider circuit
changes, and the output voltage changes. The size of resistor
you need for the R1 resistor depends on the resistance range
generated by the sensor and how sensitive you want the
output voltage to change.

E.g., the Steinhart-Hart Equation can Generally, a value between 1K and 10K ohms works just fine
be used to find the Temperature: to create a meaningful output voltage that you can detect in
your Arduino analog input interface.
NTC Thermistor Example
// Read Temerature Values from NTC Thermistor
const int temperaturePin = 0;

void setup()
{ Steinhart-Hart Equation:
Serial.begin(9600);

Serial Monitor
}

void loop()
{
int temperature = getTemp();
Serial.print("Temperature Value: ");
Serial.print(temperature);
Serial.println("*C");
delay(1000);
}

double getTemp()
{
// Inputs ADC Value from Thermistor and outputs Temperature in Celsius

int RawADC = analogRead(temperaturePin);


long Resistance;
double Temp;

// Assuming a 10k Thermistor. Calculation is actually: Resistance = (1024/ADC)


Resistance=((10240000/RawADC) - 10000);

// Utilizes the Steinhart-Hart Thermistor Equation:

// Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3}


// where A = 0.001129148, B = 0.000234125 and C = 8.76741E-08 Just don't copy the Example! Make it from
Temp =
Temp =
log(Resistance);
1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
scratch in your own way! You need to
Temp =
return
Temp - 273.15; // Convert Kelvin to Celsius
Temp; // Return the Temperature understand what's happens! Play and
}
Explore! Add Value to your code!
Discrete Lowpass Filter
Lowpass Filter Transfer function:
Create and use a Lowpass
Filter together with the
Temperature Sensors.
We define: Implement the Lowpass
Filter as a separate Function
Inverse Laplace gives the differential Equation:

This gives:
We use the Euler Backward method:

This gives: Filter output Noisy input signal

This algorithm can be easly implemented


in a Programming language
Congratulations! - You are finished with the Task
MATLAB

Hans-Petter Halvorsen, M.Sc.


MATLAB + Arduino
MATLAB Support Package for Arduino
Hardware
MATLAB OPC Toolbox
Arduino Support from MATLAB
Arduino Support in MATLAB:
http://se.mathworks.com/hardware-support/arduino-
matlab.html
Installation (Video):
http://se.mathworks.com/videos/install-the-matlab-and-
simulink-support-packages-for-arduino-
106497.html?type=shadow
Temperature Sensor (Video):
http://se.mathworks.com/videos/using-matlab-and-
arduino-to-acquire-analog-signals-
100739.html?type=shadow
Arduino Support from MATLAB
Search for Arduino in MATLAB Help

Initializing the Device:


a = arduino('comX', 'uno');

Reading Digital Values:


readDigitalPin(...)

Writing Digital Values:


writeDigitalPin()

Reading Analog Values:


readVoltage()
MATLAB OPC Toolbox
The MATLAB OPC Toolbox supports both OPC
DA and OPC UA
http://se.mathworks.com/products/opc/
Acquire Sensor Data
with MATLAB

Hans-Petter Halvorsen, M.Sc.


Arduino + MATLAB
Controlling a LED a = arduino('comX', 'uno');

writeDigitalPin(a, 'D11', 0);


pause(2);
writeDigitalPin(a, 'D11', 1);

a = arduino('comX', 'uno');

brightness_step = (5-0)/20;

for i = 1:20
writePWMVoltage(a, 'D11', i*brightness_step);
pause(0.1);
end

for i = 1:20
writePWMVoltage(a, 'D11', 5-i*brightness_step);
pause(0.1);
end

http://se.mathworks.com/help/supportpkg/arduinoio/examples/getting-started-with-matlab-support-package-for-arduino-hardware.html?prodcode=ML
Arduino + MATLAB
Reading Temperature Values
TMP36

a = arduino('comX', 'uno');

voltage = readVoltage(a, 'DA0');

temperature = (voltage*100)-50

http://se.mathworks.com/videos/using-matlab-and-arduino-to-acquire-analog-signals-100739.html?type=shadow
Analog Readings Arduino IDE vs. MATLAB
Arduino IDE:
analogRead(pin) -> Ouput is between 0-1023, which
must must be converted to voltage and then again to
proper engineering units.
https://www.arduino.cc/en/Reference/AnalogRead
MATLAB Arduino Support Package:
readVoltage(a, pin) -> Output is between 0-5V, which
must be converted to proper engineering units.
http://se.mathworks.com/help/supportpkg/arduinoio/ref/readvoltage.html
Acquiring Sensor Data in MATLAB

Acquire Sensor Data from 2 different


Temperature Sensors in MATLAB
Create and use a Lowpass Filter
Compare and Discuss
Add Value!
Discrete Lowpass Filter
Lowpass Filter Transfer function:
Note! Implement a
Lowpass Filter as a
We define: separate Function (m file)

Inverse Laplace gives the differential Equation:

This gives:
We use the Euler Backward method:

This gives: Filter output Noisy input signal

This algorithm can be easly implemented


in a Programming language
Congratulations! - You are finished with the Task
OPC

Hans-Petter Halvorsen, M.Sc.


Theory

What is OPC?
OPC - Open Process Control/Open Platform Communications
A standard that defines the communication of data between
devices from different manufactures
Requires an OPC server that communicates with the OPC clients
OPC allows plug-and-play, gives benefits as reduces installation
time and the opportunity to choose products from different
manufactures
Different standards: Real-time data (OPC DA), Historical data
(OPC HDA), Alarm & Event data (OPC AE), etc.
Theory
Data Acquisition
PLC, PAC, DCS, SCADA
Process Data
OPC-Server
Driver
Actuators Sensors
Process

Network

OPC-Client
OPC-Client
OPC-Client
OPC Specifications
We will implement and use both in this Assignment

Classic OPC Next Generation OPC

OPC DA
OPC HDA OPC UA

OPC A&E
... (Many others)
MATLAB OPC Toolbox

The MATLAB OPC Toolbox works with both OPC DA and OPC UA

For more information about the MATLAB OPC Toolbox:

www.mathworks.com/products/opc
OPC in MATLAB
Send the Sensor Data to OPC Servers
1. DA Server (Matrikon OPC Simulation Server)
2. OPC UA Server (LabVIEW)
In this case you need to create the OPC Server as
well (using the OPC UA palette in LabVIEW)
OPC DA
DA Direct Access

Hans-Petter Halvorsen, M.Sc.


Matrikon OPC Explorer Connect to Server
Problems with Matrikon Installation?
Try Disabling the Firewall

52
Matrikon OPC Explorer - Add Tags
4
3

1
5

Finished
2
Double-click

Note! Use the BucketBrigade Items because they can be used for both reading and writing
clear
clc
% Connect to OPC Server
da = opcda('localhost', 'Matrikon.OPC.Simulation.1');
OPC DA Write
connect(da);

% Create Group
grp = addgroup(da, 'DemoGroup');

%Add Tags
itmIDs = {'Bucket Brigade.Real8'};
itm = additem(grp, itmIDs);

% Write Data
data = {23}; % Should come from your sensor
write(grp, data)
This simple Example Write only
%Clean Up one value to the Server
disconnect(da)
delete(da)
clear
clc
% Connect to OPC Server
da = opcda('localhost', 'Matrikon.OPC.Simulation.1');
OPC DA Read
connect(da);

% Create Group
grp = addgroup(da, 'DemoGroup');

%Add Tags
itmIDs = {'Bucket Brigade.Real8'};
itm = additem(grp, itmIDs);

% Retrieve Data
data = read(grp);
opcdata = data.Value
This simple Example reads only
%Clean Up one value from the Server
disconnect(da)
delete(da)
Congratulations! - You are finished with the Task
OPC UA
UA Unified Architecture

Hans-Petter Halvorsen, M.Sc.


Next Generation OPC
Theory

COM/DCOM XML, HTTP, SOAP


OPC Classic Next Generation OPC OPC UA
OPC DA Windows only Cross-platform All specifications
Windows, Linux, Mac,
OPC HDA Embedded, VxWorks
collected in one (DA,
OPC A&E HDA, A&E)
Protocols: UA Binary or UA XML

OPC DA Server Simpler!!


OPC Client OPC UA Client
OPC HDA Server OPC UA Server
(DCOM)
OPC A&E Server (everything built into one)
OPC UA with MATLAB
UA Unified Architecture

Hans-Petter Halvorsen, M.Sc.


OPC UA in MATLAB
Send the Sensor Data to an OPC UA Server from MATLAB. Use one
of the alternatives below.

Alt 1: Use the OPC UA Simulation Server


Write Data from MATLAB to the OPC UA Simulation
Server
Alt 2: Create an OPC UA Server from scratch using LabVIEW
Use the OPC UA palette in LabVIEW

OPC UA in LabVIEW: You need to install the LabVIEW DSC


Module or LabVIEW Real-Time Module See next slides for details...
OPC UA Simulation Server
http://se.mathworks.com/help/opc/examples/install-an-opc-ua-
simulation-server-for-opc-ua-examples.html?prodcode=OT&language=en
https://opcfoundation.org/developer-tools/developer-kits-unified-
architecture/
Make sure to start the OPC UA Server
OP UA Dashboard
Browse Items in OPC UA Server
clear, clc

opcuaserverinfo('localhost');
uaClient = opcua('hansph_laptop', 62547);
connect(uaClient)

if (isConnected(uaClient)==1)

nodes = browseNamespace(uaClient)

[Values, Timestamps, Qualities] = readValue(uaClient, nodes)

end

disconnect(uaClient);
Write to OPC UA Server from MATLAB
clear, clc

opcuaserverinfo('localhost');
uaClient = opcua('hansph_laptop', 62547);
connect(uaClient)

if (isConnected(uaClient)==1)
newValue = 25;
node = opcuanode(2, '1:LC1001?SetPoint');
writeValue(uaClient, node, newValue);
end

disconnect(uaClient);
Read from OPC UA Server from MATLAB
clear, clc

opcuaserverinfo('localhost');
uaClient = opcua('hansph_laptop', 62547);
connect(uaClient)

if (isConnected(uaClient)==1)
node = opcuanode(2, '1:LC1001?SetPoint');
[value, timestamp, quality] = readValue(uaClient, node)
end

disconnect(uaClient);
Congratulations! - You are finished with the Task
OPC UA with LabVIEW
UA Unified Architecture

Hans-Petter Halvorsen, M.Sc.


OPC UA in LabVIEW
http://zone.ni.com/reference/en-XX/help/371618J-01/TOC9.htm

DSC Datalogging and Supervisory Control

Note! You need to install


the LabVIEW DSC
Module or LabVIEW
Real-Time Module
OPC UA Server
Example in LabVIEW

69
OPC UA Client
(Read Values)
Example in LabVIEW

70
Congratulations! - You are finished with the Task
Data Analysis

Hans-Petter Halvorsen, M.Sc.


Data Analysis in MATLAB/LabVIEW
Get Values from OPC
Plot Temperature Data
Do some Data Analysis and Statistics, calculate Average,
etc.
Plot Data with and without Lowpass Filter and compare
Which sensor gives the most accurate values?
etc.
Add Value!
Congratulations! - You are finished with the Task
Congratulations! - You are finished with all the Tasks in the Assignment!
Hans-Petter Halvorsen, M.Sc.

University College of Southeast Norway


www.usn.no

E-mail: hans.p.halvorsen@hit.no
Blog: http://home.hit.no/~hansha/

Вам также может понравиться