I have a Nema 23 motor that I am trying to drive with a TB6600 driver. I am trying to get this simple program to work where the motor moves some distance and return back. But the motor keeps moving only in one direction. The output I get on the serial monitor is just "Before direction change loop" which is also very weird. Can someone help me with this?
Edit: I realized that this problem occurs only when I am using the ESP32 controller. When I use a Arduino Uno, the code works fine!
// Include the AccelStepper Library
#include <AccelStepper.h>
const int dirPin = 0;
const int stepPin = 4;
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin);
void setup() {
// set the maximum speed, acceleration factor,
// and the target position
// myStepper.setEnablePin(enPin);
Serial.begin(115200);
myStepper.setMaxSpeed(1000.0);
myStepper.setAcceleration(50.0);
myStepper.moveTo(50);
}
void loop() {
Serial.print("Before direction change loop");
if (!myStepper.run()) { // run() returns true as long as the final position has not been reached and speed is not 0.
Serial.print("Inside Direction change");
myStepper.moveTo(-myStepper.currentPosition());
}
}
This driver uses 5v Signal logic. Your ESP32 uses 3v logic. You may need a level shifter between the two in order to get it to work correctly.
Also, which pins on the ESP32 do you have wired to the driver? This driver doesn't have a simple direction pin. It looks like it has a Clockwise and Counter-clockwise pin. Also, are you pulling the enable pin high? You would have to set that pin separately with accelstepper.
Also, be careful. This driver does not have the normal arrangement of A+, A-, B-, B+ pins for the motor pins.
I am using pin 15 on the ESP32 for direction and pin 4 for stepping.
Isn't the clockwise and counter-clockwise pin same as the direction pin? I tried another TB6600 driver that has exactly the same layout but the label says direction pin instead of clk. I see the same problem with that though. No change in direction.
I haven't connected the enable to anything.
The motor rotates properly in one direction. So I am assuming the motor pins are connected properly to the driver.
Anything you think I should do to trouble shoot further?
I have solution for anyone struggling with this issue. You have 2 options. Both worked for me. Essentially, the problem is that ESP32 boards use 3V logic (@ryancasler thanks for pointing that out) but TB6600 need a 5V signal.
Instead of connecting your driver to the 5V pin on the ESP32, connect it to the 3.3V pin. So when your IO pin sends a 3.3V signal to the direction pin, it makes the potential difference between the +5V and the dir pin zero which is what will make the motor change direction.
Keep using the 5V pin on the ESP32 but use a level shifter. I bought a few off of Amazon.
Is that an accurate picture of your driver? And are you 100% sure that it's a TB6600? Because the pinout doesn't match anything I've seen for that driver.