Controlling Stepper Motor via TB6600 using two switches for CW and CCW rotation

Hello,

I am attempting to setup two push switches, each controlling one direction of rotation for my stepper motor via TB6600. However, I am still having issues with the coding and setup. Note* setup the switches without a pull down resisteor, the Arduino UNO detects the switches but vice versa. Could someone assist me and shed some light as to what went wrong.

// Pin definitions

#define DIR_PIN   2  // Direction control pin (to TB6600 DIR+)
#define STEP_PIN  3  // Step control pin (to TB6600 PUL+)
#define CW_BTN    4  // Clockwise button
#define CCW_BTN   5  // Counter-clockwise button

void setup() {
  // Set TB6600 control pins as outputs
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);

  // Set pushbutton pins as inputs (external pull-downs used)
  pinMode(CW_BTN, INPUT);
  pinMode(CCW_BTN, INPUT);
}


void loop() {
  // Read button states
  bool cwPressed = digitalRead(CW_BTN);
  bool ccwPressed = digitalRead(CCW_BTN);

  if (cwPressed && !ccwPressed) {
    digitalWrite(DIR_PIN, HIGH);  // Set direction to clockwise
    stepMotor();
  } 
  else if (ccwPressed && !cwPressed) {
    digitalWrite(DIR_PIN, LOW);   // Set direction to counterclockwise
    stepMotor();
  } 
  // If both buttons released (or both pressed), do nothing
}

// Step the motor one step (adjust delay for speed)
void stepMotor() {
  digitalWrite(STEP_PIN, HIGH);
  delayMicroseconds(500);   // Adjust for speed
  digitalWrite(STEP_PIN, LOW);
  delayMicroseconds(500);   // Adjust for speed
}

Then do they have pull-up resistors? Must be one or the other or you are dealing with chaos?

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.

An idea based on ignorance. Search Arduino/reference/input and INPUT_PULLUP.

After you figured out that you needed to change the pin numbers in your code, did you figure out that pressed buttons ring without a pulling resistor, so the motor will be commanded to change direction 100 times in one second, or go in one of two random directions, or just stop? And did you figure out after debouncing the button, it must stay pressed for the stepper to continue to move?

I edited the pin numbers to configure them for INPUT_PULLUP...

// Pin definitions

#define DIR_PIN   2  // Direction control pin (to TB6600 DIR+)
#define STEP_PIN  3  // Step control pin (to TB6600 PUL+)
// #define CW_BTN    4  // Clockwise button
// #define CCW_BTN   5  // Counter-clockwise button
#define CW_BTN    5  // Clockwise button
#define CCW_BTN   4  // Counter-clockwise button

void setup() {
  // Set TB6600 control pins as outputs
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);

  // Set pushbutton pins as inputs (external pull-downs used)
  // pinMode(CW_BTN, INPUT);
  // pinMode(CCW_BTN, INPUT);
  pinMode(CW_BTN, INPUT_PULLUP); // <-- PULLUP resistors in use - active = LOW
  pinMode(CCW_BTN, INPUT_PULLUP); // <-- PULLUP resistors in use - active = LOW
}

void loop() {
  // Read button states
  bool cwPressed = digitalRead(CW_BTN);
  bool ccwPressed = digitalRead(CCW_BTN);

  if (cwPressed && !ccwPressed) {
    digitalWrite(DIR_PIN, HIGH);  // Set direction to clockwise
    stepMotor();
  } 
  else if (ccwPressed && !cwPressed) {
    digitalWrite(DIR_PIN, LOW);   // Set direction to counterclockwise
    stepMotor();
  } 
  // If both buttons released (or both pressed), do nothing
}

// Step the motor one step (adjust delay for speed)
void stepMotor() {
  digitalWrite(STEP_PIN, HIGH);
  delayMicroseconds(500);   // Adjust for speed
  digitalWrite(STEP_PIN, LOW);
  delayMicroseconds(500);   // Adjust for speed
}
diagram.json for wokwi.com simulation
{
  "version": 1,
  "author": "",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -24, "left": -29.3, "attrs": {} },
    { "type": "wokwi-a4988", "id": "drv1", "top": -120, "left": 129.6, "attrs": {} },
    {
      "type": "wokwi-stepper-motor",
      "id": "stepper1",
      "top": -201.61,
      "left": 214.62,
      "attrs": { "size": "8", "arrow": "white", "display": "angle" }
    },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -162.44, "left": 192, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": -28.8, "left": 191.4, "attrs": {} },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -118.6,
      "left": -28.8,
      "attrs": { "color": "green", "xray": "1" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn2",
      "top": -70.6,
      "left": -28.8,
      "attrs": { "color": "green", "xray": "1" }
    }
  ],
  "connections": [
    [ "drv1:2B", "stepper1:A+", "green", [ "h0" ] ],
    [ "drv1:2A", "stepper1:A-", "green", [ "h0" ] ],
    [ "drv1:1A", "stepper1:B-", "green", [ "h0" ] ],
    [ "drv1:1B", "stepper1:B+", "green", [ "h0" ] ],
    [ "gnd1:GND", "drv1:GND.1", "black", [ "v0" ] ],
    [ "gnd1:GND", "drv1:GND.2", "black", [ "v0" ] ],
    [ "vcc1:VCC", "drv1:VMOT", "red", [ "v9.6" ] ],
    [ "vcc1:VCC", "drv1:VDD", "red", [ "v0" ] ],
    [ "drv1:SLEEP", "drv1:RESET", "green", [ "h-9.6", "v-9.6" ] ],
    [ "drv1:GND.2", "drv1:ENABLE", "black", [ "h28.95", "v-19.12", "h-86.4", "v9.6" ] ],
    [ "nano:GND.1", "btn2:2.r", "black", [ "v0" ] ],
    [ "nano:GND.1", "btn1:2.r", "black", [ "v0" ] ],
    [ "nano:2", "drv1:DIR", "green", [ "v0" ] ],
    [ "nano:3", "drv1:STEP", "green", [ "v0" ] ],
    [ "nano:4", "btn1:1.r", "green", [ "v0" ] ],
    [ "nano:5", "btn2:1.r", "green", [ "v0" ] ],
    [ "drv1:ENABLE", "drv1:MS1", "green", [ "h-9.6", "v9.6" ] ],
    [ "drv1:MS1", "drv1:MS2", "green", [ "h-9.6", "v9.6" ] ],
    [ "drv1:MS2", "drv1:MS3", "green", [ "h-9.6", "v9.6" ] ]
  ],
  "dependencies": {}
}

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