Data Acquisition with Scilab
MicroDAQ toolbox for Scilab can be used to develop advanced data acquisitions applications. Toolbox lets you make a variety of measurements directly from Scilab without the need to convert the data or import from other software. Software includes functions for controlling analog inputs, analog outputs, digital I/O (quadrature encoders, PWM). You can visualize and analyze data, automate measurements, create wireless measurement applications.
In this article, we want to gather all common examples of data acquisition tasks and provide sample code showing how these tasks can be implemented with MicroDAQ toolbox for Scilab. Bellow is a sample code which can be used on MicroDAQ E1000/E1100 and E2000 devices. However not every MicroDAQ configuration will be capable of running all examples e.g. when you are using MicroDAQ with the 0-5V analog output you are not able to generate a negatibe voltage on analog output and you will get 'Unsupported range' error on some examples below.
Analog Inputs | Analog Outputs | Simultaneous Analog Input and Outputs | Digital Inputs and Outputs | Encoder Inputs | PWM Outputs
Analog Inputs
Single data acquisition in single-ended mode
Acquire data (8 samples) from analog input channels 1,2,3,4,5,6,7,8 with range -10 to 10V, single-ended mode.
mdaqAIRead(1:8, [-10,10], %F)
Single data acquisition in differential mode
Acquire data (8 samples) from analog input channels 1,2,3,4,5,6,7,8 with range -10 to 10V, differential mode.
mdaqAIRead(1:8, [-10,10], %T)
Single data acquisition with multi-range
Acquire data (4 samples) from analog input channels 1,2,3,4 with ranges -10 to 10V for channels 1, 4 and range -5 to 5V for channels 2,3, single-ended mode.
mdaqAIRead(1:4, [-10,10; -5,5; -5,5; -10,10], %F)
Single data acquisition with multi-range, single-ended and differential mode
Acquire data (3 samples) from analog input channels 1,3,4 where channel 1 is a differential channel, channels 3 4 are single-ended. Configure channels with the following ranges: differential channel (1) -10 to 10V, single-ended channel number 3 -5 to 5V range and -10 to 10V for channel number 4.
mdaqAIRead([1 3 4], [-10,10; -5,5; -10,10], [%T %F %F])
Acquire data from multiple channels
Acquire 10000 scans (80000 samples) and plot data from analog input channels 1,2,3,4,5,6,7,8. Acquisition with 10000 scans per second rate, with range -10 to 10V, single-ended mode. Acquisition duration is 1 second.
mdaqAIScanInit(1:8, [-10,10], %F, 10000, 1) plot(mdaqAIScan(10000, %T))
Acquire data from multiple channels with different input ranges
Acquire 10000 scans (40000 samples) and plot data from analog input channels 1,2,3,4. Acquisition with 10000 scans per second rate, with ranges -10 to 10V for channels 1, 4 and range -5 to 5V for channels 2,3, single-ended mode. Acquisition duration is 1 second.
mdaqAIScanInit(1:4, [-10,10; -5,5; -5,5; -10,10], %F, 10000, 1) plot(mdaqAIScan(10000, %T))
Acquire and save data to CSV file
Acquire 10000 scans (80000 samples) and save data to CSV file. Analog input channels 1,2,3,4,5,6,7,8. Acquisition with 10000 scans per second rate, with range -10 to 10V, single-ended mode. Acquisition duration is 1 second.
mdaqAIScanInit(1:8, [-10,10], %F, 10000, 1); data = mdaqAIScan(10000, %T); csvWrite(data, 'adc_data.csv')
Start acquisition on analog input voltage level
Start acquisition when the voltage on A1 is greater than 2V. Acquire 10000 scans (80000 samples). Acquire data from analog input channels 1,2,3,4,5,6,7,8. Acquisition with 10000 scans per second rate, with range -10 to 10V, single-ended mode. Acquisition duration is 1 second.
mdaqAIScanInit(1:8, [-10,10], %f, 10000, 1); while(mdaqAIRead(1, [-10,10], %f) < 2) then end plot(mdaqAIScan(10000, %T))
Start acquisition on DIO state change
Start acquisition when DIO7 becomes High. Acquire 10000 scans (80000 samples) from analog input channels 1,2,3,4,5,6,7,8. Acquisition with 10000 scans per second rate, with range -10 to 10V, single-ended mode. Acquisition duration is 1 second.
mdaqAIScanInit(1:8, [-10,10], %f, 1000, 1); while(mdaqDIORead(7) == %F) then end plot(mdaqAIScan(1000, %T))
Acquire data in the loop
Acquire 10000 scans (80000 samples) in the loop and plot data from analog input channels 1,2,3,4,5,6,7,8. Acquisition with 10000 scans per second rate, with range -10 to 10V, single-ended mode. When acquired the desired number of samples stop acquisition - continuous mode acquisition.
aiData = []; dataCount = 0; mdaqAIScanInit(1:8, [-10,10], %F, 10000, -1) for i=1:10 [data result] = mdaqAIScan(1000, %T); aiData = [aiData; data]; dataCount = dataCount + result; mprintf('Acquired %d scans (total: %d scans)\n', result, dataCount); end mdaqAIScanStop(); plot(aiData);
Analog Outputs
Set multiple analog output channels
Set 1V, 2V, 3V, 4V on channels 2, 4, 6, 8 respectively. Configure all used analog output channels to range 0 to 5V.
mdaqAOWrite([2, 4, 6, 8], [0, 5], [1, 2, 3, 4])
Set multiple analog output channels with different ranges
Set analog output voltage with different analog output range. Set 5V, 1V, 2V on channels 1, 2, 3 respectively. Configure channels 1, 2, 3 with with ranges -10 to 10V, -2.5 to 2.5V and -2.5 to 2.5V respectively.
mdaqAOWrite([1,2,3], [-10,10; -2.5,2.5; -2.5,2.5], [5,1,2])
Background signal generation
Generate sine waveform signal (10Hz) on analog output channel 1 (AO1) from 100 values for 5 seconds. Analog output update rate is 1000 updates per second. Use -10 to 10V range for analog output channel.
data = sin(linspace(0, 2*%pi, 100)); mdaqAOScanInit(1, data', [-10,10], %F, 1000, 5); mdaqAOScan();
Background signal generation on multiple channels
Generate sine waveform signal (10Hz) on analog output channel 1 and 2 from 100 values for 5 seconds. Analog output update rate is 1000 updates per second. Use -10 to 10V range for all used analog output channels.
data1 = sin(linspace(0, 2*%pi, 100)); data2 = sin(linspace(0, 2*%pi, 100))*2; mdaqAOScanInit([1 2], [data1' data2'], [-10,10], %F, 1000, 5); mdaqAOScan();
Start background signal generation on analog input voltage level
Start acquisition when the voltage on A1 is below 2V. Generate sine waveform signal (10Hz) on analog output channel 1 (AO1) from 100 values for 5 seconds. Analog output update rate is 1000 updates per second. Use -10 to 10V range for analog output channel.
data = sin(linspace(0, 2*%pi, 100)); mdaqAOScanInit(1, data', [-10,10], %F, 1000, 5); while(mdaqAIRead(1, [-10,10], %f) < 2) then end mdaqAOScan();
Background signal generation on multiple channels with data update
Generate sine waveform signal (10Hz) on analog output channel 1 and 2 from 100 values (for each channel) for 5 seconds. After 2 seconds change channel 2 sine waveform amplitude by loading new waveform data. Analog output update rate is 1000 updates per second. Use -10 to 10V range for all used analog output channels. Use Periodic mode.
data1 = sin(linspace(0, 2*%pi, 100)); data2 = sin(linspace(0, 2*%pi, 100))*2; mdaqAOScanInit([1 2], [data1' data2'], [-10,10], %F, 1000, 5); mdaqAOScan(); sleep(2000); data2 = sin(linspace(0, 2*%pi, 100))*3; mdaqAOScanData(2, data2', %F);
Signal generation with waveform data queuing
Generate rising amplitude sine waveform signal (1Hz) on analog output channel 1 for 5 seconds. Queue 1000 samples in blocking mode. Analog output update rate is 1000 updates per second. Use -10 to 10V range for all used analog output channels. Use Stream mode.
sine = repmat(sin(linspace(0, 2*%pi, 1000)), 1, 5); gain = (0.001:0.001:5); data = sine.*gain; mdaqAOScanInit(1, data(1:1000)', [-10,10], %T, 1000, 5); mdaqAOScan(); mdaqAOScanData(1, data(1001:2000)', %T); mdaqAOScanData(1, data(2001:3000)', %T); mdaqAOScanData(1, data(3001:4000)', %T); mdaqAOScanData(1, data(4001:5000)', %T);
Simultaneous Analog Input and Outputs
Generate signal and acquire data simultaneously
Generate sine waveform on analog output 1 and acquire data on analog input 1 simultaneously. Use same rate for analog input and output scanning - 1000 scans/updates per second.
data = sin(linspace(0, 2*%pi, 100)); mdaqAOScanInit(1, data', [-10,10], %F, 1000, 1); mdaqAIScanInit(1, [-10,10], %f, 1000, 1); mdaqAOScan(); plot(mdaqAIScan(1000, %T))
Generate signal and acquire data simultaneously with different rates
Generate sine waveform on analog output 1 and acquire data on analog input 1 simultaneously. Use same different rates for analog input and output. Generate signal with 100 updates per second and aquire data with 1000 scans per second.
data = [0 0 0 0 0 1 1 1 1 1]; mdaqAOScanInit(1, data', [-10,10], %F, 100, 1); mdaqAIScanInit(1, [-10,10], %f, 1000, 1); mdaqAOScan(); plot(mdaqAIScan(1000, %T))
Acquire, process and generate signal on analog output simultaneously.
Acquire signal from analog input 1, gain signal by factor of 2, and generated amplified signal on analog output 1 simultaneously. Perform signal processing for 5 seconds, with -5 to 5V analog input rage and -10 to 10V analog output rage. Use same scan/update per second rate (1000) for analog inputs and outputs. Use Stream mode for analog output scanning session.
mdaqAIScanInit(1, [-5,5], %F, 1000, 5); data = mdaqAIScan(1000, %T); // Signal processing - gain acquired signal data = data * 2; mdaqAOScanInit(1, data, [-10,10], %T, 1000, 5); mdaqAOScan(); for i = 1:4 data = mdaqAIScan(1000, %T); // Signal processing - gain acquired signal data = data * 2; mdaqAOScanData(1, data, %T); end
Digital Inputs and Outputs
Read DIO state
Read DIO7 state
mdaqDIORead(7)
Read DIO state
Read DIO1 state which is configured by default as a Encoder input
mdaqDIOFunc(1, %F); state = mdaqDIORead(1) mdaqDIOFunc(1, %T);
Write DIO state
Set DIO16 state to high.
mdaqDIOWrite(16, 1)
Read MicroDAQ function key state
Blink D1 LED until MicroDAQ F1 button is released.
while(mdaqKeyRead(1) == %F) mdaqLEDWrite(1, 1); sleep(100); mdaqLEDWrite(1, 0); sleep(100); end
Set MicroDAQ LED state
Blink D1 LED until MicroDAQ F1 button is released.
while(mdaqKeyRead(1) == %F) mdaqLEDWrite(1, 1); sleep(100); mdaqLEDWrite(1, 0); sleep(100); end
Encoder Inputs
Read Encoder module
Read Encoder module 1 until MicroDAQ F1 button is released.
mdaqEncoderInit(1, 0); while(mdaqKeyRead(1) == %f) disp(mdaqEncoderRead(1)) end
PWM Outputs
Set PWM period and duty
Set PWM period to 1ms and initial duty to 25% and 50% for channel A and B. After 1 second change PWM duty to 0 for channel A and B.
mdaqPWMInit(1, 1000, %F, 25, 50); sleep(1000); mdaqPWMWrite(1, 0 , 0);