Showing results for 
Search instead for 
Did you mean: 

automating a stepper motor using labview

Hi good day


I'm trying to automate a stepper motor using labview. I am trying to make the motor turn 1 direction for 5 seconds, hold for 1 minute, then turn the other direction for 3 seconds. Then i want the process to be repeated again after 1 hour until i stop the .VI. I have followed multiple examples found in the forums and finally was able to make the motor turn for the required amount of seconds but i cant figure out how to make it hold for 1 minute and repeat after an hour. i've attached the code.



For context, i am trying to lift and lower a chamber at specific intervals.

0 Kudos
Message 1 of 11

Welcome to the Forums, @AlAlexis.  I just started a new Project that involved moving a Stepper motor (which I'd never done, so I read up on Steppers) and I wasn't sure what my colleagues were going to provide me with the electronic interface between the PC (with USB and TCP/IP connections to hardware) and the Stepper Motor (which had essentially four-6 wires connected to internal coils that energized little magnets.  A wild variety of hardware and methods go between the PC and the motor ...


Thank you for attaching (some) code to give us some idea of what you are thinking.  It also shows something about your understanding of LabVIEW as a Data Flow language (clearly the Principles of Data Flow were not emphasized sufficiently, as you are sequencing your instructions with Frame sequences (which should never be used by LabVIEW "students" -- the Error Line should be used, instead).


How are you interfacing your Stepper Motor with your PC running LabVIEW?  I see you are using the DAQ Assistant (which I usually call the "Dreaded DAQ Assistant", or DDA, as it obscures, rather than help, doing Data Acquisition tasks) and, it appears, are using it to deliver a rotating pattern of Booleans, presumably connected to some unspecified circuit to drive the 4-wire Stepper.  I'm not sure how "time" is being utilized here ...


So what hardware are you using?  What do the Boolean outputs from your DDA actually doing?  Does a True make a Voltage somewhere?  A circuit diagram would be nice.  What is the hardware device?  Is it an NI USB 60XX?  


What Stepper motor are you using?  Many inexpensive Steppers come with a "Driver", a little circuit board with a tiny chip that lets you control your motor with two TTL inputs, one that controls the direction of the motor (CW/CCW corresponding to TTL H/L) and the other to the "step", moving one step when the output goes from L to H (so if you make a transition every 10 ms, you motor will "step" 50 times/second).


Please take the time to go through the "Introduction to LabVIEW" mentioned in the Learning Center on the first page of the Forum.  Learn about LabVIEW structures (including sub-VIs),as well as interfacing with Hardware (and DAQmx, instead of the DDA),  Pay attention to the Timing functions on the Palette (although you'll probably end up doing more timing with DAQmx and the hardware that you connect to your PC, such as an NI-USB device.


Please let us know how your motor is connected to your PC -- what is the hardware you are using?


Bob Schor

0 Kudos
Message 2 of 11


Thanks for replying


I am using a 28byj-48 stepper motor connected to a NI USB 6212 OEM, it did come with a mini circuit board that can drive the motor. The VI works with and without the mini circuit board.


The 4 coil wires are connected to 4 digital outs on the DAQ, the VI applies 5 volts in sequence to the coils in order for rotation


The boolean is supposed to specify which direction the motor spin. (left or right)


I cant figure out how to use DAQmx to make a digital sequence for the coil

0 Kudos
Message 3 of 11


@AlAlexis wrote:


Thanks for replying


I am using a 28byj-48 stepper motor connected to a NI USB 6212 OEM, it did come with a mini circuit board that can drive the motor. The VI works with and without the mini circuit board.

That wouldn't be an A4988 chip, would it?  If so, hook it in as described.  Note you want an external voltage supply to Pin 16 (ground to Pin 15), and the 5V pin from the NI 6212 to Pin 10, with digital ground to Pin 9.  Read the info on the A4988 regarding Pins 1-6.  Pins 11-14, of course, are the four Motor leads.


Now you only need two Digital Out signals (though you may want to add three more if you want to use Micro-stepping).  If Pin 8 is high, the motor turns one way, and if low, it turns the other way.  Finally, if you put a pulse train on Pin 7, every rising edge will give a step (direction to be determined by Pin 8).


So (as Mathematicians say), we've reduced Stepper Motor Control to producing a Pulse Train.  How fast the Pulses arrive determines the speed of the motor (there are some practical considerations -- don't give it a 1 MHz pulse train!], and the number of Pulses in the train determine the total size of the movement.  According to the data sheet, the full step size is such that 64 steps = 1 revolution (pretty big steps!), so if you want to turn 180 degrees in 1 second, send 32 Pulses at a pulse rate of 64 Hz (180 deg/sec x 64 steps/360 deg x 1 sec = 32 steps).


Generating a Pulse Train uses what is called a "Counter/Timer", that is, generate N Pulses ("counter") at a certain rate ("timer").  And, fortunately, LabVIEW tells you how to generate just such a Pulse Train with the NI 6212.  Open LabVIEW, go to Help, Find Examples, Hardware Input and Output, Counter Output, Counter - Finite Output.  Open it, and immediately rename it, saving on your Desktop or in your My Documents, wherever you save LabVIEW code (you don't want to modify NI's example -- you might need it later).


Open it.  Like most NI Examples, it has lots of "features you don't need right now".  Plug in your 6212, open MAX, and see what Device it is -- say it is Dev 1.  Now open your example (see previous paragraph).  The Counter will probably be Dev`1/ctr0 (that is, Counter 0 on Device 1), with the output terminal Dev1/PFI 12.  Check the pinouts of the 6212 -- PFI 12 is on Pin 38.  Wire this to Pin 8 of the A-4988.  Power everything up, set up the number of pulses (say, 32) and pulse rate (say, 64 Hz) and you should get a half-turn in 1 second.


Now clean up the Example, get rid of the pieces you don't need, make it "neat", and have fun.


Bob Schor

Message 4 of 11

Thanks for the detailed reply, i am currently reading up and learning about DAQmx. I'm fairly new to LABview so i'm still learning. I will reply when i've got it working.



0 Kudos
Message 5 of 11

I have manage to get the motor working using DAQmx using NI-USB 6356. Was an adventure and massive trial and error. 


Any idea how i can reverse the motor direction? Ive done some looking around and someone said they used an H-bridge to reverse the polarity. I am trying to avoid a Hardware solution and do it via software. 



Download All
0 Kudos
Message 6 of 11

 Did you read the data sheet on the Stepper Motor driver you are using, the ULN2003?  [In my earlier response, I "guessed" you were using the A4988 driver, which has one Pin that "counts the Steps" and another pin that decides "CW" or "CCW"].


It turns out the two Driver boards do things completely differently.  Both serve to provide current drive to the Stepper Motor coils connected via the pins on the right side of the chip.  The ULN2003 has 7 input pins on the left side of the chip -- each can drive a coil (there are generally 4 coils in common Stepper Motors), so the ULN2003 uses four of the inputs, one per coil, and the other three sit idle.  It is up to User Software (such as the Stepper Library running in an Arduino) to understand how to translate the user's desire (such as "Rotate CW") into a series of inputs to the four Input Pins (turns out that this basically amounts to a direct current drive of a coil, so to turn CW, you would energize Pin 1, Pin 2, Pin 3, Pin 4, Pin1, .... and the motor would be "pulled" a tiny amount CW with each transition).


The A4988 "does it differently".  It has 8 input pins, of which 3 are such things as "Enable" and "Sleep", and are typically wired to +5 or Gnd, as appropriate.  Pins 2-4 control "micro-stepping", a technique that "slows the motor down" by making it take half, quarter, eighth, or 16ths of a step per pulse, resulting in a smoother motion.  The last two are key -- one is "Step", which takes a pulse train that causes the motor to "step" once for each pulse.  The other is "Dir", which controls the direction of the step.


To drive a Stepper using an A4988 (which, I believe, costs on the order of $10), you need 3 DO lines to control the "Microstepping" setting you want (though you can also just "decide" it and wire these pins to +5 or Gnd), and one DO line to control the direction of rotation.  You need an Analog Output line (I used AO0) to deliver a 5V Pulse Train generated by the output of the Counter-Timer, where you program the "Counter" for the number of steps you want (the Timer, of course, determines the timing of the Pulses, or the "speed" of the motor).  When you start the Counter-Timer going (refer to the Example code NI provides (Hardware Input & Output, DAQmx, Counter Output, Counter - Finite Output), it will deliver the number of pulses you specify and the motor will (or should) turn an appropriate amount in the direction you specified using the DO line for Direction.  No need for Arduino.  All done courtesy of LabVIEW.


Bob Schor


Message 7 of 11

I had a little trouble opening your code (I hate pictures, and only got the one showing the NI Example) and couldn't open your VI (LabVIEW "hung" -- you weren't sending a Virus, were you?), but I think I see why your example code doesn't work.  You are (probably) sending a (single) Pulse Train (you have a Digital Output, 1 Channel N Samples) which you run with Continuous Samples (meaning it never stops unless you push the Stop button) -- you'd need to change this to Finite Samples and compute the Pulse Train you need to "do one (finite) Rotation".


However, you are send this to only 1 pin of the ULN2003 Driver chip, meaning you are turning on one coil repeatedly.  If you run this and (gently) turn the shaft of the motor, you should feel it "resisting you" at whatever sampling rate you program -- you are commanding it to 'Stay Here!" over and over.


What you'd need is a 4-channel set of digital pulses, basically something like (0001, 0010, 0100, 1000, 0001, 0010 ...) in binary notation.  Note this is what is called a "Full Step" sequence, where you energize one coil at a time "in sequence".  If you want to turn in the other direction, you just run the numbers in the reverse order (0001, 1000, 0100, 0010, 0001, ...).


Bob Schor.

0 Kudos
Message 8 of 11

Thanks for the timely reply, Not sure why VI wasn't opening for you. I will continue working on it per your advises

0 Kudos
Message 9 of 11

Hi, Thanks for you help. Your advises were very helpful. I have managed to created a state machine with the functionality stated in my first post and everything works smoothly on the NI USB 6356. Was quite the adventure in learning all the different functions in Labview. 😫



To fix the direction issue i had, all i had to do was switch the 32-bit sequence from ascending, to descending order.

32bit.pngHowever, i changed my DAQ to what i will be actually using in my work, (NI 6212 OEM) and my VI no longer works.

NI 6212 OEM.pngCapture.PNGLooking around in the forums, i understand that certain DAQ cannot handle 'Sample clock'. So i went to the 'Find examples' section on Labview to help me understand how to use the 'On demand' function. 



The SQ-Timed output is the only example that works partially, However i cannot get the motor to turn. I have tried editing my VI to 'On Demand' timing but it only energizes 1 coil.

On demand.PNG



Do i have to use triggers in my Code? Attached is the Original VI i used for the NI USB 6356

0 Kudos
Message 10 of 11