Welcome! Log In Create A New Profile

Advanced

Help me understand A4988 drivers

Posted by stamasd 
Help me understand A4988 drivers
November 20, 2015 09:41PM
Okay, this is not about a 3D printer (though I have one and it works fine) but my project uses very similar electronics and I figured the members here have the most experience with them.

I'm building a spool winder for coils with a large number of turns, and after examining a number of options I've settled on a design where the spool will be spun by a stepper motor. I have steppers and A4988 drivers as spare parts for my printer so it would cost me next to nothing to implement it. The drivers I have are Chinese clones of the StepStick design with 0.1ohm sense resistors, and work fine with the steppers in my printer. I plan on using these with an Arduino (I like Pro Minis, and have a large number of them) to control the winder.

I haven't built from scratch a stepper circuit before, so I whipped a quick model on a breadboard this afternoon using this tutorial: [www.instructables.com]

However the example code in step 3 does not do what it's supposed to do. I played with the delays in the loop a bit, and came to the conclusion that what happens is that when the "step" signal is high, the stepper is being driven to turn continuously by the A4988 instead of turning just one step. For instance, if I drive the "step" pin high for 2000ms and then low for 2000ms, the motor turns continuously for 2s, then stops for 2s and so on. But that's not what it's supposed to do; I would have expected that it would turn just one step, then stop. Instead it goes through many steps during the time that the "step" signal is high.

FWIW I have a couple of original Pololu drivers as well, and when I swapped them into the circuit they do the same thing.

What am I doing wrong?
Re: Help me understand A4988 drivers
November 21, 2015 02:51AM
The instructions and code is correct. Also your assumption about the step-pin behavior is right.
You might have a wiring problem?
-Olaf
Re: Help me understand A4988 drivers
November 21, 2015 07:30AM
I can't find this behavior in the datasheet. The minimum time of the step pulses should be 1us. If it's get a repeat mode like you said you need to make sure the pulses are at most 1.9us. See page 6 of the datasheet.


--
Kind regards
Imqqmi

NFAN CoreXY printer:
[reprap.org]
Re: Help me understand A4988 drivers
November 21, 2015 07:50AM
Quote
imqqmi
I can't find this behavior in the datasheet. The minimum time of the step pulses should be 1us. If it's get a repeat mode like you said you need to make sure the pulses are at most 1.9us. See page 6 of the datasheet.

I see the 1us minimum pulse time on page 6 in the datasheet, but I don't see where the 1.9us maximum pulse time comes from. I have looked at other designs, and many have pulses far longer than 1.9us.

Quote
o_lampe
The instructions and code is correct. Also your assumption about the step-pin behavior is right.
You might have a wiring problem?
-Olaf

I checked the wiring again, it's correct. I will try to change the pins on the Arduino and see if I get the same behavior. If that happens, I will trace the signals with an oscilloscope to try and figure what's going on.

Edited 2 time(s). Last edit at 11/21/2015 08:06AM by stamasd.
Re: Help me understand A4988 drivers
November 21, 2015 09:04AM
Quote
stamasd
However the example code in step 3 does not do what it's supposed to do.

You have not told us what it actually does.

Edited 1 time(s). Last edit at 11/21/2015 09:04AM by dc42.



Large delta printer [miscsolutions.wordpress.com], E3D tool changer, Robotdigg SCARA printer, Crane Quad and Ormerod

Disclosure: I design Duet electronics and work on RepRapFirmware, [duet3d.com].
Re: Help me understand A4988 drivers
November 21, 2015 09:54AM
Quote
dc42
Quote
stamasd
However the example code in step 3 does not do what it's supposed to do.

You have not told us what it actually does.

It's supposed to send 200 pulses to the motor, which would make it spin for 1 complete turn, then pause for 1 second. It's all in the code that I linked to. Here's a copy below. It doesn't do that.

// Run a A4998 Stepstick from an Arduino UNO.
// Paul Hurley Aug 2015
int x; 
#define BAUD (9600)


void setup() 
{
  Serial.begin(BAUD);
  pinMode(6,OUTPUT); // Enable
  pinMode(5,OUTPUT); // Step
  pinMode(4,OUTPUT); // Dir
  digitalWrite(6,LOW); // Set Enable low
}

void loop() 
{
  digitalWrite(6,LOW); // Set Enable low
  digitalWrite(4,HIGH); // Set Dir high
  Serial.println("Loop 200 steps (1 rev)");
  for(x = 0; x < 200; x++) // Loop 200 times
  {
    digitalWrite(5,HIGH); // Output high
    delay(10); // Wait
    digitalWrite(5,LOW); // Output low
    delay(100); // Wait
  }
  Serial.println("Pause");
  delay(1000); // pause one second
}

Now back to my testing. I changed the output pins on the arduino, but got the same result. I then traced the signals with an oscilloscope, and this is where it gets weird.

At the beginning of the loop, where it sends the "digitalWrite(5, HIGH)" I actually see a negative 5V pulse, i.e. -5V on the "step" pin. At this point the motor starts spinning repeatedly. Then at the end of the loop it sends a positive pulse of +5V, and the motor stops spinning.

I don't know why the code behaves this way. I can't understand where the -5V comes from, as there is no -5V supply, but it's there on the oscilloscope screen.
Re: Help me understand A4988 drivers
November 21, 2015 01:21PM
Are you sure the scope is setup correctly? There's no way an AVR MCU outputs a negative voltage.

The time of 1.9us, it's not in the datasheet. It's derived from the fact that a high part of the pulse is 1us, then it should be at least 1us low, so if you finish the pulse within 2us time, it may not go into 'repeat' mode. Another advantage is that you can now better control the delays between pulses and make it programmatically clearer.

I'd use the delayMicroseconds() function for the step delays. I'd make a few changes to the code. Use #defines for all pin assignments with clear names, that makes debugging and changing pins later on much easier.

I use pic mcu's mostly. Here's the function I use to pulse a step to the step stick:
PulseStep()
{
    PIN_STEP = 1;
    __delay_us(1);
    PIN_STEP = 0;
    __delay_us(1);
    
}

Rewriting it for arduino:


int x; 
#define BAUD (9600)
#define PIN_STEP 5
#define PIN_DIR 4
#define PIN_EN 6


//Function declarations
PulseStep();

void setup() 
{
  Serial.begin(BAUD);
  pinMode(PIN_EN,OUTPUT); // Enable
  pinMode(PIN_STEP,OUTPUT); // Step
  pinMode(PIN_DIR,OUTPUT); // Dir
  digitalWrite(PIN_EN,LOW); // Set Enable low
}

void loop() 
{
  digitalWrite(PIN_EN,LOW); // Set Enable low
  digitalWrite( PIN_DIR,HIGH); // Set Dir high
  Serial.println("Loop 200 steps (1 rev)");

  for(x = 0; x < 200; x++) // Loop 200 times
  {
    PulseStep(); // Pulse one step to motor
    delay(10); // delay between steps in milliseconds. This will determine the speed at which the motor runs 200x10ms = 2 seconds per revolution.
  }

  Serial.println("Pause");
  delay(1000); // pause one second
}

//Pulse one step to stepstick
PulseStep()
{
    digitalWrite(PIN_STEP,HIGH); // Output high
    delayMicroseconds(1); // Wait
    digitalWrite(PIN_STEP,LOW); // Output low
    delayMicroseconds(1); // Wait
}


--
Kind regards
Imqqmi

NFAN CoreXY printer:
[reprap.org]
Re: Help me understand A4988 drivers
November 21, 2015 01:38PM
I actually tried pulses as low as 1us using delayMicroseconds(1); the motor wouldn't step at all with this. Yes, I am quite sure there is a -5V pulse at the beginning; the oscilloscope is correctly calibrated and shows a distinctive -5V pulse with absolutely no positive deflection at the beginning of the loop, and a +5V pulse at the end. I don't understand either how can an AVR board generate -5V, but it's there. I would attach pictures but I'd have to have 3 hands to take one, and I don't have that many. smiling smiley

I will try to swap a different Arduino and see if I get the same result.
Re: Help me understand A4988 drivers
November 21, 2015 02:00PM
What does the rest of the schematic look like? Is the Arduino powered from the same PSU and is the step stick decoupled with 0.1uF capacitors? Maybe it's a flyback current from the motor coils you're seeing? See the typical application in the datasheet. If you don't use acceleration, starting the motor at a high speed instantly produces some current spikes. You can try using the x variable for the delay so that it speeds up and slows down? Something like
for (x=0; x<200; x++)
{
  if( x<100) delay( 100-(x/2) ); //speeding up
  else delay( (x/2)-100 ); // slowing down
}

There are also acceleration libraries for stepper motors you can use.


--
Kind regards
Imqqmi

NFAN CoreXY printer:
[reprap.org]
Re: Help me understand A4988 drivers
November 21, 2015 02:33PM
Quote
imqqmi
What does the rest of the schematic look like? Is the Arduino powered from the same PSU and is the step stick decoupled with 0.1uF capacitors? Maybe it's a flyback current from the motor coils you're seeing? See the typical application in the datasheet. If you don't use acceleration, starting the motor at a high speed instantly produces some current spikes. You can try using the x variable for the delay so that it speeds up and slows down? Something like
for (x=0; x<200; x++)
{
  if( x<100) delay( 100-(x/2) ); //speeding up
  else delay( (x/2)-100 ); // slowing down
}

There are also acceleration libraries for stepper motors you can use.



Separate power for the Arduino and motor; no extra decoupling except for a 47uF cap on the 12V line; I wouldn't expect to see any current spikes from the motor transmitted back to the driver input. Besides they are too square shaped and exactly 5V.

Anyway I have connected an Arduino Uno instead, and it gives me the correct pulse trains on the STEP pin. The motor turns but very inconsistently and erratically; it sometimes change direction too.

Edited 2 time(s). Last edit at 11/21/2015 02:35PM by stamasd.
Re: Help me understand A4988 drivers
November 21, 2015 03:17PM
Ok, so the arduino was faulty?

What happens if you increase the delay between steps? Did you try changing motor current? Is microstepping turned on? Try without it, or 2x microstepping.

Edited 2 time(s). Last edit at 11/21/2015 03:19PM by imqqmi.


--
Kind regards
Imqqmi

NFAN CoreXY printer:
[reprap.org]
Re: Help me understand A4988 drivers
November 21, 2015 03:29PM
I tried all of your suggestions already. I went up to 1us pulse with 20ms between them, no change; I tried longer pulses, up to 10ms, no change. Microstepping is off, no need for that in my application. I did vary the current a bit, not very far from what I know works for these motors in my printer (usually 0.8A, I went from about 0.65 to 1A). None of this made it better.
Re: Help me understand A4988 drivers
November 21, 2015 04:49PM
If you try the step stick and motor on your printer, does it work then? Did try replacing the step stick? Did you try your code on your printer? If you have a known good, a working situation you can try eliminating any variables and see what is causing the problem.


--
Kind regards
Imqqmi

NFAN CoreXY printer:
[reprap.org]
Re: Help me understand A4988 drivers
November 21, 2015 05:18PM
It's in my first post. These are the same drivers and motors that I'm using in the printer. They work well together there. This setup is for a different project that I'm building up.
Re: Help me understand A4988 drivers
November 22, 2015 03:04AM
What I mean is are these parts from your printer and have been running prints or have these parts never been used, laying in a spare parts container? Do you assume they are the same or is it possible they are faulty? I'd check them to know for sure the parts are working correctly.


--
Kind regards
Imqqmi

NFAN CoreXY printer:
[reprap.org]
Re: Help me understand A4988 drivers
November 22, 2015 11:35AM
The a4988 drivers have been tested in the printer. The motor has not but it came from the same box as the motors I'm using in the printer. The Arduino Pro Mini and the Uno have been used in other projects before and work fine.
Re: Help me understand A4988 drivers
November 22, 2015 06:49PM
Did you check the dir signal with the scope? Is it a clean signal or is there a lot of noise? That might explain the reversing of direction, and if the step line is similarly noisy there could be a ground loop, or a wonky psu or some electromagnetic interference etc. did you try connecting the dir pin of the step stick to ground directly? Did that stop the motor ftom changing direction? How does the arduino psu look like on the scope? Are both psus connected to the same power distribution block?
Did you try powering the arduino from bateries?

Edited 1 time(s). Last edit at 11/22/2015 06:56PM by imqqmi.


--
Kind regards
Imqqmi

NFAN CoreXY printer:
[reprap.org]
Re: Help me understand A4988 drivers
November 22, 2015 07:19PM
Connect your grounds together and see what happens.
Re: Help me understand A4988 drivers
November 23, 2015 07:22AM
Unfortunately I won't have time to work on the project again until some time next week. The Arduino power is clean, it comes from my computer (USB ).

Edited 1 time(s). Last edit at 11/23/2015 07:23AM by stamasd.
Re: Help me understand A4988 drivers
November 23, 2015 09:23AM
Can it run stand alone from a different power source? Computer power is far from clean though, especially when connecting another PSU.


--
Kind regards
Imqqmi

NFAN CoreXY printer:
[reprap.org]
Sorry, only registered users may post in this forum.

Click here to login