Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Generate Pulse Width Modulated Signals Using NI Devices

This example shows how to generate a pulse width modulated signal to drive a stepper motor.

Create a Counter Input PulseWidth Channel

Create a DAQ Session, and add a counter output channel with PulseGeneration measurement type. An analog input channel is added to monitor the pulse generated by the counter output channel. For this example, use CompactDAQ chassis NI c9178 and module NI 9402 with ID cDAQ1Mod5.

s = daq.createSession('ni');
s.addAnalogInputChannel('cDAQ1Mod1', 0, 'Voltage');
s.addCounterOutputChannel('cDAQ1Mod5', 0, 'PulseGeneration');
s
s = 

Data acquisition session using National Instruments hardware:
   Will run for 1 second (1000 scans) at 1000 scans/second.
   Number of channels: 2
      index Type  Device   Channel MeasurementType      Range       Name
      ----- ---- --------- ------- --------------- ---------------- ----
      1     ai   cDAQ1Mod1 ai0     Voltage (Diff)  -10 to +10 Volts
      2     co   cDAQ1Mod5 ctr0    PulseGeneration n/a
   

Determine the Terminal of the Counter Output Channel

To connect the output signal to the correct terminal, examine the Terminal property of the counter channel. The terminal is determined by the hardware.

s.Channels(2).Terminal
ans =

PFI0

Clocked Counter Output

Use counter output channel 0 to generate a fixed pulse width modulated signal on terminal PFI0. Trigger the motor after 0.5 seconds, with a 75% duty cycle.

ch = s.Channels(2);
ch.Frequency = 10;
ch.InitialDelay = 0.5;
ch.DutyCycle = 0.75;
s.Rate = 1000;
s.DurationInSeconds = 1;

% StartForeground returns data for input channels only. The data variable
% will contain one column of data.
[data, time] = s.startForeground();
plot(time, data);

Was this topic helpful?