Intermittent stepper motor issue

I have an intermittent problem with a stepper motor project as follows:

The project comprises a stepper motor being controlled by 7 push buttons moving to any one of the selected step positions. The selected position are equally spaced and the selection can be forward or backward.

The hardware comprises an Arduino Uno, a Makerverse Motor Driver, a Mercury Motor SM-42BYG011-25 and a pushbutton panel with associated LEDs. A gear wheel attached to the motor follows a curved linear rack gear to the required alignment.

The Arduino sketch is attached.

Prior to startup the motor/gear set is free and is manually moved to initialise the ‘zero’ position.

Now, the problem. The project works perfectly as required moving accurately to the selected alignment, however occasionally and intermittently on start up the motor moves to the selected position but with a small error. This constant error is then maintained throughout all subsequent movements (in both directions), i.e. it is not cumulative.

I would really appreciate any advice or guidance.

Thanks, in anticipation


Stepper Motor Traverser Control.pdf (35.1 KB)

1 Like

Hi Toni,

In future, make sure you enclose your code in 3 backticks (```) either side. that’ll make it much easier for us to read or copy into an editor. As it stands your PDF doesn’t copy well into the Arduino IDE, it’s missing a lot of the spacing. Here’s what will show up on the right side of the editor if you do it right:

* Stepper Motor Traverser Control (pushbutton and led panel)
* This programme drives a unipolar or bipolar stepper motor for use with a model rai track
* Created 19 November 2022
* Amended 2 January 2023
* Amended 17 January 2023
 // Libraries required
 // define motor characteristics
const int stepsPerRevolution = 200;
Stepper DMProject(stepsPerRevolution,8,9,10,11);
//const float trackspace = 118.125; // steps between tracks = 5.4/32*700
const float trackspace = 116.25; // steps between tracks = 5.4/32*700
 // define arrays and variables
const int ledPin[7] = {22,24,26,28,30,32,34};
const int buttonPin[7] = {50,48,46,44,42,40,38};
const int location[7] = {-3*trackspace,-2*trackspace,-
 int currentbuttonState[7] = {0,0,0,1,0,0,0};
 int currentledState[7] = {0,0,0,1,0,0,0};
 int nextlocation[7] ={0,0,0,0,0,0,0};
 int nextbuttonState[7] = {0,0,0,0,0,0,0};
 int nextledState[7] = {0,0,0,0,0,0,0};
 int currentlocation = 3;
void setup(){
 // initialize the serial port
 DMProject.setSpeed(20); // recheck in situ
 //initialise led and button pins
 for (int p=0;p<7;p++)
 pinMode(ledPin[p], OUTPUT);
 pinMode(buttonPin[p], INPUT_PULLUP);
void loop()
 Serial.println("START LOOP");
 int testposition;
 int origin = currentlocation;
 int b; // location variable (button loop counter)
 Serial.print("old currentlocation = ");

 // initialise leds
 // initialise arrays
 for (int a = 0; a<7; a++)
 currentbuttonState[a] = 0;
 currentledState[a] = 0;
 nextbuttonState[a] = 0;
 nextledState[a] = 0;
 nextlocation[a] = 0;
 currentledState[currentlocation] = 1;

 //Check for activation of button
 for ( b=0 ; b<7 ;b++)
 Serial.print("b = ");
 testposition = (digitalRead(buttonPin[b]));
 Serial.print("testposition = ");

 //test if change to button position
 if (testposition == 0 ) //&& currentlocation != b)
 // turn off led

 // Amend arrays and variables for next position/situation
 currentledState[b] = 1;
 currentbuttonState[b] = 1;
 nextledState[b] = 0;
 nextbuttonState[b] = 0;
 nextlocation[b] = 0;
 currentlocation = b;

 // Serial print new values
 Serial.print("b = ");
 Serial.print("origin = ");
 Serial.print("neworigin = ");
 Serial.print("new currentlocation = ");

 //Ammend pins

 // step to new location on button state changes
 int steps = location[b] - location[origin];
 Serial.print("steps = ");

Could you explain why the start location is 3? Have you been playing around with the value to get it bang on? If not, that might explain your error,

Typically, systems like yours have a limit switch on one end, which when activated by your moving thing bumping into it, gives your microcontroller an accurate zero.

Have you tried stripping back your project to just movement by a set amount and gradually adding in bits to see if any of your logic breaks it?

Could it be backlash? How much does your table move back and forth with the stepper trying to hold it in a particular position?

How much error are you actually seeing? Have you measured it with calipers and worked out how many steps that is?


How small is the error? If it is very small then it is likely that the stepper is being positioned between a step, and the first move is therefore slightly greater or smaller than one step.

1 Like

Thanks for your quick replies, I will pull out the callipers and try to measure the error tomorrow, convert it to steps (allowing for the gear ratio) and let you know. Today it has worked perfectly all day - sod’s law.

The method being used to initialise the zero point is a pin hole and a pin which aligns with the centre of the centre rail track.


1 Like

Hi Jeff and James

Since you raised the issue I’ve measured both the magnitude of the misalignment and the spacing of the tracks between each other giving a measured error of 2.7%.
Applying this to the number of motor steps being used for each track spacing gives an error of 3.14 motor steps. Do you think this is significant?

On the other questions you raised:
The number 3 as the start location is purely a ‘name’ for that alignment as used by the operator.
The base frame etc is quite solid with no perceptive movement. This may also be confirmed by the accuracy of the 2nd and subsequent movements where the error, or lack thereof, does not change.
Oops, sorry about the format of the sketch I sent.

So it’s too big to be a problem of aligning on a part step. But it’s also not a whole step, so something mechanical is most likely. If the problem can’t be identified you could add an ‘initialise’ button that did a +1 and -1 move, to allow the operator to confirm the setup before proceeding

Thanks Jeff Before adding another push button I’ll modify the sketch to do a one-off +/- 1 move sequence in the setup and see how we go on Monday,


Hi All

Sounds very close to Pi (3.1416) and related to the circle. Does that give any clues?
Cheers Bob

Thanks Bob

If the figure of 3.14 measured was the physical error then I should look into this, however as it is the motor step error I can’t see how pi comes into it. Perhaps I’m missing something.



Hi Toni
All OK. As you seem to be turning a circular thing I thought it was a bit of a coincidence and could be related that is all.

Pi comes into everything to do with a circle.
Cheers Bob

Hi Jeff,

Thanks for your suggestions.

I added the +1/-1 commands to the initialisation section of the sketch but this didn’t solve the problem, then noted your suggestion of a button operation prior to the sketch running. I guess this is a manual alternative to the buffer stop/microswitch method which I know is used in many products and processes (including my 3d resin printer!).

I will let you know how that goes.

Firm believer in the old adage that if you can’t solve a problem start again at the beginning.



When you do the move back and forth, does the mechanism return to the correct zero point, or does the error appear? The purpose of the jog is to indicate to the operator that the initial setup was wrong, but actually fixing the error would require the operator to adjust the initial position. I suggested the button because this allows the operator to see the result of the jog and make the adjustment if necessary, and repeat until it disappears. If the error isn’t appearing at that point than the procedure will serve no purpose.

1 Like

Sorry, I forgot to let you know the outcome of this issue.

The problem appeared to be that somehow an error was being introduced with the first movement of the stepper motor. All following movements were correct.

The solution I came up with was to put a 1 step movement in the setup section of the sketch which then occurred once only prior to the main loop.

I still haven’t find out what causes this problem but the work around works.



1 Like