A4983 driver having trouble switching directions

Hey all,

I am having a curious problem and hoping to get some advice here. I am running 2 Stepper Driver A4983s with an Arduino. Currently, the drivers are powered by their onboard voltage regulators, however I am sharing a common ground between the drivers and the Arduino. I also have the EN, STEP and DIR pins wired up to my digital pins 4, 5, 6 and 7, 8, 9.

I have quite a bit of code running but I will post the chunks which actually pertain to driving the two motor drivers.

*  Set up motor motion and control

void executeG00(){
  setDirection(DIRM1, rotationM1);  
  setDirection(DIRM2, rotationM2);


* moveMotors()
* inputs - steps 
* outputs - none
* function - rotates stepper motors
void moveMotors(unsigned int stepsM1, unsigned int stepsM2){
  unsigned int x = stepsM1;
  unsigned int y = stepsM2;
  time1 = micros();
  time2 = micros();
    if(micros()>=time1+10000 && x>0){
      time1 = micros();
      digitalWrite(STEPM1, HIGH);
      digitalWrite(STEPM1, LOW);
    if(micros()>=time2+10000 && y>0){
      time2 = micros();
      digitalWrite(STEPM2, HIGH);
      digitalWrite(STEPM2, LOW);
    if(x<=0 && y<=0) moving = false;   

* setDirection()
* inputs - motor number, direction 
* outputs - none
* function - sets rotation direction of motor
void setDirection(int motor, boolean dir){
  digitalWrite(motor, dir);

So, on to my problem. When I pass the stepCount variables on to my moveMotors function, all executes with no problems and the motors move the correct number of steps. However, when I submit my next set of values, which involves switching the direction of the second motor, the driver just doesnt seem to listen and only changes direction, halfway through my moveMotors function.

for example:
command: both motors CCW and 1000 steps.
result: both motors CCW and 1000 steps.
2nd command: both motors CW and 1000 steps
2nd result: one motor moves CW 1000 steps, second motor moves CCW 600 steps and CW 400 steps.

This whole wacky scenario does not occur if I have a volt meter attached to my DIR pin. This tells me that when the Arduino switches the pin to ground, the voltage present at that pin does not seem to dissipate until 3-4 whole seconds later.

So my question is. In the product diagrams and the 4983 data sheet, it is shown to connect the DIR, STEP and EN pins directly to the micro, but in reality is this the proper way to wire this up? And what exactly is the current draw on these pins? Any insight would be appreciated.


It sounds like you probably have not configured the Arduino pins connected to DIR as outputs.

- Ben

I think I am doing it correctly…

#define ENABLEM1 4
#define STEPM1 5
#define DIRM1 6
#define ENABLEM2 7
#define STEPM2 8
#define DIRM2 9

float targetThetaM1;
float targetThetaM2;
float currentThetaM1;
float currentThetaM2;
unsigned int stepCountM1=0;
unsigned int stepCountM2=0;
boolean rotationM1 = HIGH;
boolean rotationM2 = HIGH;
boolean moving = false;
unsigned long time1; // call the micros() function to find out how long a process takes
unsigned long time2; // (temp variables)

void setup(){
  //Setup Communications
  Serial.println("Start program...");
  //Setup pin Configurations
  pinMode(ENABLEM1, OUTPUT);
  pinMode(STEPM1, OUTPUT);
  pinMode(DIRM1, OUTPUT);
  pinMode(ENABLEM2, OUTPUT);
  pinMode(STEPM2, OUTPUT);
  pinMode(DIRM1, OUTPUT);
  //Setup more
  digitalWrite(ENABLEM1, LOW);
  digitalWrite(ENABLEM2, LOW);
  digitalWrite(DIRM1, rotationM1);
  digitalWrite(DIRM2, rotationM2);
  currentThetaM1 = 90.0; 
  currentThetaM2 = -180.0;

You’re setting DIRM1 as an output twice, and never setting DIRM2 as an output. This would pretty much exactly explain the behavior you’re seeing: the pin is pulled high when you call digitalWrite(DIRM2, HIGH), but it is floating when you call digitalWrite(DIRM2, LOW), which in your case is causing it to stay high for a while and gradually drift low.

- Ben

Aaahh, seems like a colossal brain fart on my side. Thank you so much for catching that!

No problem, I think we’ve probably all made mistakes like that at one time or another. I’m just happy it was such an easy bug to identify and fix. A lot of times it can be so much more complicated!

- Ben