Stepper Motor keeps rotating without a command

Hello there,

I am having issues with my code/set up. I have a TB6600 driver for my nema 23 stepper motor. My wiring is as shown in the reference image. I also have 2 switches, one at pin 4 and one at pin 5. The problem I am having is the stepper motor keeps rotating as if its fighting itself without any command. When I push either button, they work as is. Is there something wrong with my code that makes the motor run automatically when power is supplied?

/* Example sketch to control a stepper motor with TB6600 stepper motor driver and Arduino without a library: number of revolutions, speed and direction. More info: https://www.makerguides.com */

// Define stepper motor connections and steps per revolution:
#define dirPin 2
#define stepPin 3
#define downPin 4
#define upPin 5

void setup() {
  // Declare pins as output:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(downPin, INPUT);
  pinMode(upPin, INPUT);
}

void loop() {

 if(digitalRead(downPin) == LOW) {
    digitalWrite(dirPin, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(50);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(50);
  }
 
if(digitalRead(upPin) == LOW) {
    digitalWrite(dirPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(50);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(50);
    }
    
    }

Try correctly connecting the enable pins and your problem may go away.

"... the enable pins (ENA- and ENA+) disconnected means the enable pin is always LOW and the driver is always enabled..." therefore

Look at page 3 here:

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project. See About the Installation & Troubleshooting category.

How are your switches connected? Show a complete schematic, not only the stepper.

Try declaring downPin and upPin as INPUT_PULLUP.

Hi, @Project_GL

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Your picture in post #1 is not showing all your project.
Show how you have you buttons wired? (They aren't in your picture)

Tom... :smiley: :+1: :coffee: :australia:

Throw in some serial prints to see what your code is doing.

My suspicion here is there is no pull up on the switch and nothing to debounce, the capacitance no doubt is causing oscillation.

My money is on some parasitics causing your headache, in the pin declaration set the input pins to INPUT and PULLUP using the internal resistors.

Arduino will let you declare this so you won't need to go poking registers, they have made it easy(er).

I would use a timer for the pulse, create a PWM and see if you can get a baseline, if you need to move a specific amount of steps afterwards you can work on you code, get it spinning first as you want it to perform then dive deeper.

Happy to help if you get stuck bud, just holla, everyone on here started somewhere!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.