Stepper Motor Control Problem


I have a project where i want to control a stepper motor with a 2-way toggle switch for direction and a potentiometer for the adjustment of its speed.I havent yet written the code for the speed control.

The problem that i am facing is that when i connect the toggle switch as shown in the attachment Switch_1.jpg the motor rotates randomly, no matter the position of the switch.

I cant figure out whether i made a circuit or programming mistake.

The program that i am using is attached.

Thanks in Advance!

P.S. I dont have much knowledge on arduino programming and electronic circuits

Stepper_Direction_v1.ino (590 Bytes)


I think that the problem is the way that the switch is wired. When the switch is in one position the digital input is pulled high on that side, but the other side of the switch is floating (indeterminate state) so it could read high or low. Put a 10K resistor from the Dir1 pin to ground and another 10K resistor from Dir2 to ground. That will allow the input to be pulled up when the switch is closed on that side and the other input will be tied to ground. Do not connect the common on the switch to Vin, connect it to Vcc (5V on an Uno). If Vin is more than Vcc + 0.5V the pin(s) will be damaged.

Thanks for the reply!

That seemed to solve my problem.I did put two 5.6KΩ resistors in series to ground on both sides of the switch and it seems to work for the setSpeed(60); It seems though that it has a delay of 1-2 seconds untill it changes direction from the moment that i change the switch to the opposite position.

For some reason when i change the value of setSpeed(*); it doesnt move at all.Any suggestions on that?


Please post your new code. Please read the "how to use the forum-please read" stickies to see how to format and post code so that we don't have to download it.

It seems though that it has a delay of 1-2 seconds untill it changes direction

The step function is "blocking". When you call it the function will not allow anything else to happen until the motor has completed the number of steps specified. The solution is to step once each time through loop() as long as the condition to make it step is true. Then it only blocks long enough to complete one step. You may will need a delay to slow the stepping down so that the steps don't arrive to quickly and stall the motor. The AccelStepper library is more versatile.

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your updated circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.... :)