Using an Arduino Mega as a Stepper Driver With LinuxCNC?

So I recently built a small CNC with a few old unipolar steppers and wired it up with ULN2003 darlington arrays to step up my i/o signals to 12 volts. I have been trying to decide whether to use my Arduino Mega (GRBL) or LinuxCNC to control my machine. Then it hit me: why not use both! A standard setup of LinuxCNC sends out simple step and direction pulses from a paralell port, not the actual phase sequence that I need to drive my steppers (I know the software can be setup to produce phase sequences, but it is a fairly advanced task for a beginner like me). So what I want to do is feed the outputs from my paralell port to input pins on my Arduino. The Arduino would then interpret these step and direction signals and convert them into the phase sequence to be sent to the darlington arrays.

So my question:

a). Has anyone already written an arduino sketch capable of accomplishing a task like this? Would you be willing to share it?

b). Does anyone have any suggestions as to how I should go about writing this sketch myself?

Any thoughts or suggestions would be great!

Why not get proper stepper motor driver boards for your motors. Boards such as the Pololu A4988 take step and direction signals and have the ability to limit the maximum current so that the motors can be driven at higher voltage for better performance.

The ULN2003 is a very poor choice for a stepper driver as it doesn't have the ability to limit the current in the stepper coils.


Well, I'm 17 y/o and a little strapped for cash. I figure why buy drivers when I already have an arduino? I've used the ULN's with TurboCNC and they seem to work alright. My steppers were salvaged from old floppy drives anyway.

A4988 drivers start at $3.50/each on eBay. With these drivers (as compared to the ULN arrays) you can use a much higher input voltage to drive the motors and get more speed without sacrificing torque.

If LinuxCNC sends step and dir signals to the stepper driver that is equivalent (in very slow motion) to having a push button and a switch as inputs to your Arduino. Every time the button is pressed your code should do whatever is needed for another step. And the direction is determined by the setting of the switch.

When you get this working you just need to replace the button and switch connections with the outputs from LinuxCNC.


You can use interrupt handlers on the step pins (timing is more critical for steppers
than for a push button of course), and the interrupt handler just has to read the
relevant direction pin and step the motor one way or the other depending on that

So something like

void setup ()
  attachInterrupt (0, Xmotor_handler, RISING) ;  // or whatever interrupt channel for X
  attachInterrupt (1, Ymotor_handler, RISING) ;
  attachInterrupt (2, Zmotor_handler, RISING) ;

volatile byte Xmotor_phase = 0 ;  // value 0, 1, 2 or 3

const byte Xmotor_outputs [] = { ?, ?, ?, ? } ; // pin numbers for X motor phases

void Xmotor_handler ()
  if (digitalRead (Xdirection))
    Xmotor_phase ++ ;
    Xmotor_phase -- ;
  Xmotor_phase &= 3 ;
  for (byte i = 0 ; i < 4 ; i++)
    digitalWrite (Xmotor_outputs [i], i == Xmotor_phase) ; // wave drive