I am using the National Instruments myDAQ with MATLAB (using the Data Acquisition Toolbox) to generate a sine wave at defined frequencies at one of the output channels. The output channel is (effectively) connected to a resistor which then goes to AGND. I am measuring the voltage across the resistor with one of the input channels (AI0).
Issue- When measuring from the input channel the DAQ stops recording after a certain time and just returns noise. See examples in images. Here is the relevant code-
dq = daq("ni")
dq2 = daq("ni") % same DAQ, but used for the digital channels
% Adding analog channels to dq
chi0 = addinput(dq, 'myDAQ1', 'ai0', 'Voltage');
chi0.Range = [-10, 10];
chi1 = addinput(dq, 'myDAQ1', 'ai1', 'Voltage');
chi1.Range = [-2, 2];
cho0 = addoutput(dq, 'myDAQ1', 'ao0', 'Voltage');
cho0.Range = [-10, 10];
% Adding digital channels to dq2
% Gives a warning that channels are not synchronized, but I don't need that sync
digi = addoutput(dq2, 'myDAQ1', 'port0/line0:3', 'Digital');
% Turn off all four digital outputs
write(dq2, [0, 0, 0, 0]);
% Program variables
f = 100; % Signal frequency
fs = 200e3; % Sampling frequency
Vmax = 0.05;
Vbase = 0.0;
numberOfPeriods = 20;
requiredTime = numberOfPeriods / f;
numberOfPoints = fs * requiredTime;
% t is a vector of size (numberOfPoints x 1)
t = linspace(0, requiredTime, numberOfPoints)';
program = Vmax * sin(2 * pi * f * t) + Vbase;
% Turn on one of the digital outputs
write(dq2, [0, 0, 1, 0]);
% This line works the DAQ, runs for requiredTime amount of time
% Returns a (numberOfPoints x 2) matrix with acquired data
[data, timestamps] = readwrite(dq, program, "OutputFormat", "Matrix");
% Processing received data
channel0 = data(:, 1);
channel1 = data(:, 2);
plot(t, program, timestamps, channel0); legend('Program', 'Acquired');
% Cleanup - turn off all digital outputs
write(dq2, [0, 0, 0, 0]);
This is what I see when the code executes (f = 100Hz)-
For higher frequency, it gets worse (f = 250 Hz)-
And this is for even higher frequencies (f = 1000 Hz)-
I am really unsure what is causing this. My guess is that it has something to do with the non-synchronization of the two "devices" dq
and dq2
, but cannot say exactly.
Any insight would be appreciated.