Jog a NEMA 34 with a DM542T and an E-stop
Jog a NEMA 34 stepper both ways with push buttons, set the speed with a knob, and stop it with a fail-safe E-stop.
Motors · Advanced
In a real product: CNC milling machine (Industrial): Jog buttons and an E-stop control a large stepper on each axis.
Every machine has a jog mode for moving an axis by hand, and an emergency stop. This project builds both around a DM542T, a digital stepper drive that runs from 20–50 V. A 48 V supply drives a big NEMA 34 motor.
What you'll learn
- Common-anode wiring of opto-isolated inputs, where a LOW pin means "on"
- Pulses per revolution, including non-binary settings like 1000 or 5000
- Fail-safe design: why an E-stop must stop the machine when its wire breaks
- Avoiding a stray pulse at start-up
How it works
The drive's + terminals (PUL+, DIR+, ENA+) all go to 5 V. The pins pull the − terminals low (common anode), so each input is on while its pin is LOW. The sketch tells AccelStepper so with setPinsInverted.
- Hold JOG+ (pin 8) or JOG− (pin 9) to run the motor one way or the other. The speed comes from the knob on A0, from 100 to 3200 pulses a second.
- The E-stop is the slide switch on pin 10. Slid left, it joins pin 10 to GND. Slid right, or with a broken wire, the pull-up takes pin 10 HIGH. The sketch then turns ENA on, which disables the drive and lets the motor coast. The machine stops whenever the switch circuit is broken, not only when someone presses the stop. That is what "fail-safe" means.
A pin that becomes an output starts LOW, and with common-anode wiring LOW means "on". If PUL went LOW at start-up, the drive would take a stray microstep. So the sketch makes every pin HIGH first and only then lets AccelStepper turn them into outputs (the false in its constructor holds that back).
The drive's pulses per revolution are set to 1600 (8 microsteps a full step). The DM542T also has settings like 1000 and 5000, which make positions in decimal units easy to work out.
Try this
- Set the drive to 1000 pulses a turn: one turn is now a round number of pulses.
- Add a second E-stop in series with the first. Either one must stop the machine.
- Count the pulses while jogging and print the position in degrees.
sketch.ino
// Jog a NEMA 34 stepper on a DM542T (1600 pulses a turn), wired common anode: PUL+, DIR+, ENA+ to 5V, and
// PUL- pin 5, DIR- pin 6, ENA- pin 7 (an input is on while its pin is LOW). JOG+ pin 8, JOG- pin 9 (buttons to GND),
// speed knob on A0, E-stop on pin 10 (closed to GND = run; open = stop).
#include <AccelStepper.h>
const int PUL = 5, DIR = 6, ENA = 7, JOG_FWD = 8, JOG_REV = 9, ESTOP = 10;
// Outputs not enabled yet: a pin made an OUTPUT is LOW, which would turn a common-anode input on (a stray pulse).
AccelStepper stepper(AccelStepper::DRIVER, PUL, DIR, 0, 0, false);
const char *state = "";
void show(const char *s) {
if (s != state) { state = s; Serial.println(s); }
}
void setup() {
Serial.begin(115200);
pinMode(JOG_FWD, INPUT_PULLUP);
pinMode(JOG_REV, INPUT_PULLUP);
pinMode(ESTOP, INPUT_PULLUP);
// Every input off (pin HIGH) before the pins become outputs.
digitalWrite(PUL, HIGH);
digitalWrite(DIR, HIGH);
digitalWrite(ENA, HIGH);
pinMode(ENA, OUTPUT);
stepper.setPinsInverted(true, true, false); // common anode: LOW turns PUL and DIR on
stepper.enableOutputs();
stepper.setMinPulseWidth(3);
stepper.setMaxSpeed(3200);
}
void loop() {
if (digitalRead(ESTOP) == HIGH) {
digitalWrite(ENA, LOW); // ENA on: the drive is disabled and the motor coasts
stepper.setSpeed(0);
show("E-STOP: drive disabled");
return;
}
digitalWrite(ENA, HIGH);
float speed = map(analogRead(A0), 0, 1023, 100, 3200);
if (digitalRead(JOG_FWD) == LOW) { stepper.setSpeed(speed); show("JOG+"); }
else if (digitalRead(JOG_REV) == LOW) { stepper.setSpeed(-speed); show("JOG-"); }
else { stepper.setSpeed(0); show("Ready"); }
stepper.runSpeed();
}