Reading from an Sensor
This example shows how to collect data from a Vernier sensor connected to the SensorDAQ using the Support Package.
Contents
Create a Data Acqusition Toolbox session
This line will always be the same for any Support Package function that uses the Session-based interface (all but digital io).
s = sdaq.createSession;
Add sensor to the session
This line adds a thermocouple sensor attached to Ch.1 of the device to the session. If the sensor was connected to another channel, we could change the 1 to a 2 or 3 to reflect that. If the sensor were another type of sensor, we could change the specified sensor type to reflect that. For more information on sensor types, see README.pdf or type "help sdaq.Sensors" in the Command Window.
sdaq.addSensor(s,1,sdaq.Sensors.Thermocouple);
Retrieve a scaling function
When we read data from the session, it will be unscaled voltage data. To scale this voltage data into sensor data (ie. temperature in this case), we need a special scaling function. This line creates this scaling function and gives it the name "scale".
scale = sdaq.getScaleFun(sdaq.Sensors.Thermocouple);
Get one sample of data and display it
The first line retrieves one sample of voltage data from the device and stores it in data. The second line scales the voltage date to sensor data. The third line tells MATLAB to display the result, which is in degrees Celsius.
data = s.inputSingleScan;
data = scale(data);
data %#ok<NOPTS>
data = 20.8652
Note that we could have combined the first two lines into:
data = scale(s.inputSingleScan);
They were seperated out here for clarity.
Clean up
Deleting the session object prevents it from interfering with other session objects. Always do this when you're done with the channel (including from within a script or function). This will prevent odd errors elsewhere.
delete(s);
Note that if you're planning on acquiring multiple samples in one session, you don't have to delete the session until you're done. In fact, it's much faster to not have to create a new session every time you want data.
MATLAB Support Package for Vernier SensorDAQ Version 1.0
