Control Stepper Motor using Digital Outputs

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

Discover Devices Supporting Digital Output

Use daqlist to discover devices. This example uses a National Instruments® ELVIS II with ID Dev2.

d = daqlist
d =

  12×5 table

    VendorID     DeviceID                 Description                    Model             DeviceInfo     
    ________    ___________    __________________________________    _____________    ____________________

      "ni"      "cDAQ1Mod1"    "National Instruments NI 9205"        "NI 9205"        [1×1 daq.DeviceInfo]
      "ni"      "cDAQ1Mod2"    "National Instruments NI 9263"        "NI 9263"        [1×1 daq.DeviceInfo]
      "ni"      "cDAQ1Mod3"    "National Instruments NI 9234"        "NI 9234"        [1×1 daq.DeviceInfo]
      "ni"      "cDAQ1Mod4"    "National Instruments NI 9201"        "NI 9201"        [1×1 daq.DeviceInfo]
      "ni"      "cDAQ1Mod5"    "National Instruments NI 9402"        "NI 9402"        [1×1 daq.DeviceInfo]
      "ni"      "cDAQ1Mod6"    "National Instruments NI 9213"        "NI 9213"        [1×1 daq.DeviceInfo]
      "ni"      "cDAQ1Mod7"    "National Instruments NI 9219"        "NI 9219"        [1×1 daq.DeviceInfo]
      "ni"      "cDAQ1Mod8"    "National Instruments NI 9265"        "NI 9265"        [1×1 daq.DeviceInfo]
      "ni"      "Dev1"         "National Instruments PCIe-6363"      "PCIe-6363"      [1×1 daq.DeviceInfo]
      "ni"      "Dev2"         "National Instruments NI ELVIS II"    "NI ELVIS II"    [1×1 daq.DeviceInfo]
      "ni"      "Dev3"         "National Instruments PCIe-6363"      "PCIe-6363"      [1×1 daq.DeviceInfo]
      "ni"      "Dev4"         "National Instruments PCIe-6363"      "PCIe-6363"      [1×1 daq.DeviceInfo]

d{10, "DeviceInfo"}
ans = 

ni: National Instruments NI ELVIS II (Device ID: 'Dev2')
   Analog input supports:
      7 ranges supported
      Rates from 0.0 to 1250000.0 scans/sec
      16 channels ('ai0' - 'ai15')
      'Voltage' measurement type
   Analog output 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 IO supports:
      39 channels ('port0/line0' - 'port2/line6')
      'InputOnly','OutputOnly','Bidirectional' measurement types
   Counter input supports:
      Rates from 0.1 to 80000000.0 scans/sec
      2 channels ('ctr0','ctr1')
      'EdgeCount' measurement type
   Counter output 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 motor (5V 18 degree unipolar stepper ). 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 DataAcquisition and add 4 digital channels on port 0, lines 0-3. Set the measurement type to OutputOnly. These are connected to the 4 control lines for the stepper motor.

dq = daq("ni");
Warning: Added channel does not support clocked sampling: clocked operations are
disabled. Only on-demand operations are allowed. 

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 cycle 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 write to output the sequence to turn the motor 72 degrees counterclockwise.


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

for motorstep = 1:50

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


Turn Off All Outputs

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

write(dq,[0 0 0 0]);