04-01-2011 06:46 AM
Good evening,
I am trying to output a sine using usb 6008 analog output. Could anyone help me on this please?
Thanks
04-04-2011 03:10 AM
Hi Takou,
To use the 6008 with matlab, you need the daqmx driver,which must be consistent with your OS.
I invite you to read the information found from our website on this subject:
http://joule.ni.com/nidu/cds/view/p/id/2214/lang/fr
http://zone.ni.com/devzone/cda/tut/p/id/3005
http://digital.ni.com/public.nsf/allkb/7076035AEA7835FE8625707300520720?OpenDocument
Best reagards,
Rémi D.
National Instruments France
04-04-2011 04:19 AM
Thanks you for your reply.
In fact I have looked at the demos provided by Mathworks but any of them treats the case of the NIDAQmx usb 6008. In fact, for this card it is somehow particular because it does not contain an internal clock. So we can not use the functions putdata/start, we can only use "putsample". I have tried to output my signal like this:
First of all, I have configured my analog output:
%Create an analog output object
AO = analogoutput('nidaq',boardId);
%Add hardware channels to analog output object
chans = addchannel(AO,0);
% Set the SampleRate
set(AO,'SampleRate',100)
% Obtain the actual rate set in case hardware limitations
% prevent using the requested rate
ActualRate = get(AO,'SampleRate');
Then I tried to output my sine like this:
%Number of samples
duration=nb_period/frequency;
nb_samples = ActualRate*duration;
data = sin(linspace(0,2*pi*nb_period,nb_samples))+amplitude;
for i=1:length(data)
t = timer('TimerFcn', ' putsample(AO,data(i))',...
'StartDelay',0);
start(t)
end
with frequency is the frequency of the signal to output and nb_period is the number of periods to generate. With this I got a sine as an output but its frequency is different from what I want (meaning different from the value frequency specified). Given that I want to generate sines in the range [0.01 Hz 0.5Hz], it seems to me that the sample rate is good. Could you help me on this please?
Thank you very much
Takoua
04-04-2011 07:01 AM
Hi Takou,
Is the sine magnitude seems Ok?
With this configuration what frequency and "actualRate" value do you have (ActualRate = get(AO,'SampleRate');)?
If you need plotting N period of the sine try this:
t = linspace(0, N*(1/Freq));
data = sin(2*pi*Freq*t)+amplitude;
plot(t,y);
Could you say me if the plotting seems OK in this case, and with your configuration?
In fact with the Usb 6008, you can perform a sine output only with software-timed updates of up to 150 samples per second.
http://digital.ni.com/public.nsf/allkb/6F2C2B49A89D685C8625711D007BDD64?OpenDocument
Best regards,
Rémi D.
National Instruments France
04-04-2011 07:36 AM
Thank you for the fast reply again.
I don't need more than 150 samples per second given that the range frequency of my sine is [0.01Hz 0.5Hz]
Besides, I am not plotting the sine on matlab. I am visualizing the output with an oscilloscope. The sine magnitude is ok but not its frequency.
For example if I choose frequency=0.1 (frequency=0.1;) and set the sample rate to 100Hz (set(AO,'SampleRate',100); wich respect largely Shannon), I don't get the good frequency on my oscilloscope.
I think that in order to write the sine and output it, we need to use the sample rate of the card. That is why I use it in the calcul of my sine.
I do not see where is the problem.
Best regards,
04-04-2011 08:18 AM
Hi Takou,
For this application you need a software timed loop to output a sine with 6008 because this board is limited due to the digital analog converter inside the device.
- Frequency of 0.5Hz = 2S period.
- Linespace = 100 points
If you need to output one period with 100 point (within Linespace) at 0.5Hz, you need a delay inside the For loop to perform this generation.
So in the timer function, put the delay at 0.2S between two points.
t = linspace(0, (1/Freq));
data = sin(2*pi*Freq*t)+amplitude;
for i=1:length(data)
t = timer('TimerFcn', ' putsample(AO,data(i))',...
'StartDelay',0.2);
start(t)
end
Best Regards,
Rémi D.
National Instruments France
04-05-2011 03:08 AM
Thank you Remi.
It was helpfull. In fact, I did not put the delay in the 'startdealy' but in the 'period' of the object timer and I didn't use a for loop but have incremented the variable i in my timer function and it has worked 🙂 .
Now the problem is that I have glitches on my signal. When looking in the datasheet of the card, I found this:
Minimizing Glitches on the Output Signal
When you use a DAC to generate a waveform, you may observe glitches in
the output signal. These glitches are normal; when a DAQ switches from
one voltage to another, it produces glitches due to released charges. The
largest glitches occur when the most significant bit of the DAC code
changes. You can build a lowpass deglitching filter to remove some of
these glitches, depending on the frequency and nature of the output signal.
Refer to ni.com/support for more information about minimizing
glitches.
Have you an idea how to implement thislowpass deglitching filter?
Thank you again. All of your remarks were very helpfull 🙂
04-05-2011 03:37 AM
Hi Takou,
Since the glitch is a transient event this leads designers to believe that a simple low pass filter can be used to eliminate or reduce the size of the glitch. Indeed, you can build a lowpass deglitching filter to remove some of these glitches, depending on the frequency and nature of the output signal. This circuit is external to the device.
Understanding Glitch in a High Speed D/A Converter
http://www.ingelec.uns.edu.ar/pds2803/Materiales/HDatos/Harris/tb325.pdf
Best regards,
Rémi D.
National Instruments France
03-24-2014 07:46 AM
i have the same problem with this code
%%get connected devices d = daq.getDevices %create session s = daq.createSession('ni') s1= daq.createSession('ni') %add analog channel s.addAnalogInputChannel('ID',channel num, 'measurement type') % s.addAnalogInputChannel('Dev1',0:3, 'Voltage')%irradiance s.addAnalogInputChannel('Dev1','ai0', 'Voltage')%irradiance s.addAnalogInputChannel('Dev1','ai1', 'Voltage')%Temperature s.addAnalogInputChannel('Dev1','ai2', 'Voltage')%current s.addAnalogInputChannel('Dev1','ai3', 'Voltage')%voltage % s.addAnalogInputChannel('Dev1','ai3', 'Voltage')%Temperature %set(s.Channels,'InputType','SingleEnded') %set(s.Channels,'Range',[-10 10]) %s.addAnalogInputChannel('Dev1','ai6', 'Voltage')%Temperature AO=s1.addAnalogOutputChannel('Dev1', 'ao0', 'Voltage') % set rate of scan 1000 scans/second , run for 30 seconds s.Rate=1000; s.DurationInSeconds =10 v.TerminalConfig = ' Differential'; %v.TerminalConfig = ' SingleEnded'; %_____________________________ %% %aquire data and process lh = s.addlistener('DataAvailable', @plotData); %store data %h = s.addlistener('DataAvailable', @storedata); % Analog Output % duty cycle control signal x amplification %for i=1:5 e= 18 - Vref %end IE=sum(e); %Integral of Error C = pid(20,10,0,'InputName','e','OutputName','u') % calculate control signal of pid u=C*er for i=1:length(u) t = timer('TimerFcn', ' putsample(AO,u)',... 'StartDelay',0.2); start(t) end s.startBackground() s.wait() delete (lh) s.release()
it gives me error message
Error while evaluating TimerFcn for timer 'timer-1840'
Undefined function 'putsample' for input arguments of type 'daq.ni.AnalogOutputVoltageChannel'.
12-02-2021 12:14 AM
Can we change the sampling rate of the analog output by MatLab code for NI DAQ 6008?