# Stepper Motor Spinning Slow

Posted on
Page
of 2
/ 2
• Btw, code in post #20 has to be changed to make proper direction changes in all step phases.

Current code has a step sequence discontinuity / hick-up when changing direction in lines 36 and 43.

After issuing a stop, there is also a small issue: if stopping in an 'instable' step phase - where two magnetic fields 'fight' for the rotor and move the rotor between the fighting poles - it may come to rest in either pole position - back or forward after digitalWrite of 0b000 / taking the power off. To compensate for that when continuing, power with the same last step phase has to be applied before moving on... - of course for the proper amount of time. Implementation depends on whether the setInterval(...) invokes the function the first time at beginning or end of the first interval, which I do not know.

Simplest resolution is to remove the stop() in the direction change, change the stop, and - most important - change the sequence in the backward steps sequence and adjust the step adjustment in direction change.

This are the 4 changed lines:

16.`var stBW = [0b1001,0b0001,0b0011,0b0010,0b0110,0b01­00,0b1100,0b1000];`

36.`} else { if (stT) { st = Math.abs(st - 7); } if (!stI) { st--; } setI((stT = t),stFW," FW"); }`

43.`} else { if (stT) { st = Math.abs(st - 7); } if (!stI) { st--; } setI(-(stT = t),stBW," BW"); }`

49.`if (stI) { stI = clearInterval(stI); stI = null; }`

Resulting code:

``````// stPs stepper pins
// st   step 0..8
// stT  step Time in milliseconds [ms]
// stI  step Interval (from setInterval() and for clearInterval()
// sts  steps 0001,0011,0010,... pin signals
// stBW sts - steps Backwards
// stFW sts - steps Forward
// dmy  ...because of (cond) ? exprT : exprF needs something to assign to
var run = false;
var st = 0;
var stT = 0;
var stI = null;
var sts = null;
var stSt =  0b0000;
var stFW = [0b1000,0b1100,0b0100,0b0110,0b0010,0b00­11,0b0001,0b1001];
var stBW = [0b1001,0b0001,0b0011,0b0010,0b0110,0b01­00,0b1100,0b1000];
var stPs = [C6,C7,C8,C9];

// setI setInterval(i,stsC) i in [ms] with (optionl) step Change (if not null),
// and direction info (string)
var setI = function(t,stsN,d) {
console.log("t = ",t, d);
if (stI) clearInterval(stI);
if (stsN) sts = stsN;
run = true;
stI = setInterval(stp,t);
};

// stp step
var stp = function() { digitalWrite(stPs, sts[st = ++st % 8]); };

// _sFW step ForWard
var _sFW = function(t) {
console.log("FW w/ " + t);
if (stT > 0) { setI((stT = t),null," ~F");
} else { if (stT) { st = Math.abs(st - 7); } if (!stI) { st--; } setI((stT = t),stFW," FW"); }
};

// _sBW step BackWards
var _sBW = function(t) {
console.log("BW w/ " + t);
if (stT < 0) { setI(-(stT = t),null," ~B");
} else { if (stT) { st = Math.abs(st - 7); } if (!stI) { st--; } setI(-(stT = t),stBW," BW"); }
};

// stop
var stop = function() {
console.log("stop");
if (stI) { stI = clearInterval(stI); stI = null; }
console.log(stI);
run = false;
digitalWrite(stPs, stSt);
};

// run function - t is stepping interval in [ms]
var r = function(t) {
if (typeof t === "undefined" ) {
if (stT) {
console.log((stT > 0) ? "F>B" : "B>F");
r(-stT);
} else {
console.log("What ?");
}
} else {
dmy = (t) ? (t>0) ? _sFW(t) : _sBW(t) : stop();
}
};
``````
• Old thread, pointed here by @allObjects due to some work I'm doing with this same stepper.

Some quick summary precautions to add that are covered here, but buried in all the dialog:

• The 28BYJ-48 can not handle steps faster than about 2ms apart.
• Sufficient power is critical. Running the motor driver (usually a ULN2003 chip) directly from the MCU board is very dicey, and will have lots of problems. You should use a separate 5V power supply for the driver board.
• ...specs may even ask for 12V... but heat / heat dissipation becomes an issue, that's why stepper drivers apply different currents at different times. Usually higher current on signal change, lowering towards the phase end, an lower on hold (when stopped).

2ms is about right... which also puts constraints on how much else code can be executed next to controlling the stepper phases, including the code 'in the loop' to determine when to cease stepping.

In my 2-dim 'mega' plotter I built - will pub later - all calculations are done beforehand and only simple decrement and 'falsy' checks are applied... (plus x/y ratio, whether the 'slower' moving axis has to step or not... For the very same performance reason - and repeatable, timing insensitive - 'precision', I have only one interval going...)

The experience made me seriously consider to apply slave devices - other than JS / Espruino controlled - MCS, one per axis... or 'hardware' PWM (timer directly controlling pin...)

I like the acceleration / deceleration approach, but as said, code has not to slow down rhe 'intervalled' stepping code execution.