Control Stepper Motor using Digital Outputs

This example shows how to control a stepper motor using digital output ports.

Discover Devices Supporting Digital Output

To discover a device that supports digital output:

  • Issue daq.getDevices in the Command window.

  • Click on the device name in the list returned by the command.

devices = daq.getDevices
devices = 

Data acquisition devices:

index Vendor Device ID           Description           
----- ------ --------- --------------------------------
1     ni     cDAQ1Mod1 National Instruments NI 9205
2     ni     cDAQ1Mod2 National Instruments NI 9263
3     ni     cDAQ1Mod3 National Instruments NI 9234
4     ni     cDAQ1Mod4 National Instruments NI 9201
5     ni     cDAQ1Mod5 National Instruments NI 9402
6     ni     cDAQ1Mod6 National Instruments NI 9213
7     ni     cDAQ1Mod7 National Instruments NI 9219
8     ni     cDAQ1Mod8 National Instruments NI 9265
9     ni     Dev1      National Instruments PCIe-6363
10    ni     Dev2      National Instruments NI ELVIS II

This example uses a National Instruments® ELVIS II with ID Dev2. Verify that its digital subsystem supports the OutputOnly measurement type.

devices(10)
ans = 

ni: National Instruments NI ELVIS II (Device ID: 'Dev2')
   Analog input subsystem supports:
      7 ranges supported
      Rates from 0.0 to 1250000.0 scans/sec
      16 channels ('ai0' - 'ai15')
      'Voltage' measurement type
   
   Analog output subsystem supports:
      -5.0 to +5.0 Volts,-10 to +10 Volts ranges
      Rates from 0.0 to 2857142.9 scans/sec
      2 channels ('ao0','ao1')
      'Voltage' measurement type
   
   Digital subsystem supports:
      39 channels ('port0/line0' - 'port2/line6')
      'InputOnly','OutputOnly','Bidirectional' measurement types
   
   Counter input subsystem supports:
      Rates from 0.1 to 80000000.0 scans/sec
      2 channels ('ctr0','ctr1')
      'EdgeCount' measurement type
   
   Counter output subsystem supports:
      Rates from 0.1 to 80000000.0 scans/sec
      2 channels ('ctr0','ctr1')
      'PulseGeneration' measurement type
   


Hardware Setup Description

This example uses a Portescap 20M020D1U 5V 18 Degree Unipolar Stepper Motor. The TTL signals produced by the digital I/O system are amplified by a Texas Instruments ULN2003AIN High Voltage High Current Darlington Transistor Array, as shown in this schematic:

Add Digital Output Only Channels

Create a session, and add 4 digital channels on port 0, lines 0-3. Set the measurement type to OutputOnly. These are connected to the four control lines for the stepper motor.

s = daq.createSession('ni');
addDigitalChannel(s,'Dev2','port0/line0:3','OutputOnly')
Warning: A channel that does not support clocked sampling was added to the
session. Clocked operations using startForeground and startBackground will be
disabled. Only on-demand operations using inputSingleScan and outputSingleScan
can be done. 

ans =

Data acquisition session using National Instruments hardware:
   Clocked operations using startForeground and startBackground are disabled.
   Only on-demand operations using inputSingleScan and outputSingleScan can be done.
   Number of channels: 4
      index Type Device   Channel   MeasurementType Range Name
      ----- ---- ------ ----------- --------------- ----- ----
      1     dio  Dev2   port0/line0 OutputOnly      n/a
      2     dio  Dev2   port0/line1 OutputOnly      n/a
      3     dio  Dev2   port0/line2 OutputOnly      n/a
      4     dio  Dev2   port0/line3 OutputOnly      n/a
   


Define Motor Steps

Refer to the Portescap motor wiring diagram describing the sequence of 4 bit patterns. Send this pattern sequentially to the motor to produce counterclockwise motion. Each step turns the motor 18 degrees. Each cycle of 4 steps turns the motor 72 degrees. Repeat this sequence five times to rotate the motor 360 degrees.

step1 = [1 0 1 0];
step2 = [1 0 0 1];
step3 = [0 1 0 1];
step4 = [0 1 1 0];

Rotate Motor

Use outputSingleScan to output the sequence to turn the motor 72 degrees counterclockwise.

outputSingleScan(s,step1);
outputSingleScan(s,step2);
outputSingleScan(s,step3);
outputSingleScan(s,step4);

Repeat sequence 50 times to rotate the motor 10 times counterclockwise.

for motorstep = 1:50
    outputSingleScan(s,step1);
    outputSingleScan(s,step2);
    outputSingleScan(s,step3);
    outputSingleScan(s,step4);
end

To turn the motor 72 degrees clockwise, reverse the order of the steps.

outputSingleScan(s,step4);
outputSingleScan(s,step3);
outputSingleScan(s,step2);
outputSingleScan(s,step1);

Turn Off All Outputs

After you use the motor, turn off all the lines to allow the motor to rotate freely.

outputSingleScan(s,[0 0 0 0]);
Was this topic helpful?