Data Logging and Analysis With Arduino and MATLAB
Data Logging and Analysis With Arduino and MATLAB
05
Acquiring Data
Sensors
Data Logging Cloud/Network
Your PC
OPC
Analysis
MATLAB
Arduino IDE
Matrikon OPC
Hardware
Hardware
Arduino/
Genuino Breadboard
Sensors and
Actuators NTC
Thermistor
Potentiometer
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
http://en.wikipedia.org/wiki/Sensor http://en.wikipedia.org/wiki/Actuator
Sensors Theory
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
http://www.arduino.cc
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
}
18
The Arduino Kit
Arduino Uno Board
The Arduino IDE (File->Examples) comes with lots of examples and at www.arduino.cc you find more!
Temperature Sensors
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
https://learn.adafruit.com/tmp36-temperature-sensor
Datasheet Calculations Theory
output = analogRead(aichannel)
0-1023 A0-A5
TMP36 Temperature Wiring
TMP36 Temperature Sensor Example
// We'll use analog input 0 to read Temperature Data
void setup()
{
}
Serial.begin(9600); Serial Monitor
void loop()
{
float voltage, degreesC, degreesF;
voltage = getVoltage(temperaturePin);
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
This gives:
We use the Euler Backward method:
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');
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
This gives:
We use the Euler Backward method:
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
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
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
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
opcuaserverinfo('localhost');
uaClient = opcua('hansph_laptop', 62547);
connect(uaClient)
if (isConnected(uaClient)==1)
nodes = browseNamespace(uaClient)
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
69
OPC UA Client
(Read Values)
Example in LabVIEW
70
Congratulations! - You are finished with the Task
Data Analysis
E-mail: hans.p.halvorsen@hit.no
Blog: http://home.hit.no/~hansha/