AccelStepper library (1.58.0) and Adafruit's TB6612 Breakout Board

136 views
Skip to first unread message

Robert Cox

unread,
Feb 19, 2019, 7:44:21 PM2/19/19
to accelstepper
Hi all, 

I have an interesting behavior in my sample program that I would like to run by this group.

Perhaps I am doing something wrong, or not understanding something - I've search this group and read the documentation and I can't figure it out.

I've actually made a short video that demonstrates the issue and hopefully makes it easier to understand.


I have written a small stepper test program (sketch) that initializes a stepper by rotating counter clockwise until a microswitch is engaged and then it rotates clockwise until the other microswitch is engaged. Once the initialization is complete, the user is prompted to enter the position between 0 and some number which is the clockwise limit when enter is pressed, the stepper will move to that position. 

The issue is that the stepper, when told to rotate counter clockwise to a position, will sometimes rotate clockwise a few steps and then reverse and go to the destination. This never happens when the stepper is told to go in the clockwise direction. 

The library used is: AccelStepper 1.58.0
The stepper driver board is: Adafruit TB6612 breakout board (https://www.adafruit.com/product/2448)
The stepper is a NEMA-17 200 step 12v motor (https://www.adafruit.com/product/324)
The Arduino that I'm using is the Mega 2560

Thanks in advance for your help.

-Rob

The full code for the sketch is here:

#include "AccelStepper.h"


// AccelStepper Setup
AccelStepper stepper(AccelStepper::FULL4WIRE, 36, 37, 38, 39);  


// Define the Pins used
#define  cw_home_switch 31 // Pin 31 connected to home micro switch (this will be the 0 position)
#define  ccw_home_switch 30 // Pin 30 connected to initial homing micro switch (this will be variable)


// Stepper Travel Variables
long destination;        // Used to store the X value entered in the Serial Monitor
int move_finished=1;     // Used to check if move is completed
long initial_homing=-1;  // Used to home the cw rotation of the stepper at startup




void setup() {
   
Serial.begin(9600);   // Start the Serial monitor with speed of 9600 Bauds
   
   pinMode
(ccw_home_switch, INPUT_PULLUP);
   pinMode
(cw_home_switch, INPUT_PULLUP);
   
   
//  Set Max Speed and Acceleration of each Steppers at startup for homing
  stepper
.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper
.setAcceleration(100.0);  // Set Acceleration of Stepper


 
// Start Homing procedure of Stepper Motor at startup


 
Serial.print("Stepper is homing counter clockwise. . . . . . . . . . . ");


 
while (digitalRead(ccw_home_switch)) {  // Make the Stepper move CCW until the switch is activated  
    stepper
.moveTo(initial_homing);  // Set the position to move to
    initial_homing
--;  // Decrease by 1 for next move if needed
    stepper
.run();  // Start moving the stepper
    delay
(5);
 
}


  stepper
.setCurrentPosition(0);  // Set the current position as zero for now
  stepper
.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper
.setAcceleration(100.0);  // Set Acceleration of Stepper
  initial_homing
=1;


 
while (!digitalRead(ccw_home_switch)) { // Make the Stepper move CW until the switch is deactivated
    stepper
.moveTo(initial_homing);  
    stepper
.run();
    initial_homing
++;
    delay
(5);
 
}
 
  stepper
.setCurrentPosition(0);
 
Serial.println("Counter clockwise homing completed");
 
Serial.println("");
  stepper
.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Faster for regular movements)
  stepper
.setAcceleration(100.0);  // Set Acceleration of Stepper


  delay
(1000);    // Pause for one second before homing the counter clockwise rotation.


 
Serial.print("Stepper is homing clockwise. . . . . . . . . . . ");


 
while (digitalRead(cw_home_switch)) {  // Make the Stepper move CCW until the switch is activated  
    stepper
.moveTo(initial_homing);  // Set the position to move to
    initial_homing
++;  // Decrease by 1 for next move if needed
    stepper
.run();  // Start moving the stepper
    delay
(5);
 
}


  stepper
.setCurrentPosition(initial_homing);  // Set the current position as zero for now
  stepper
.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper
.setAcceleration(100.0);  // Set Acceleration of Stepper


 
while (!digitalRead(cw_home_switch)) { // Make the Stepper move CW until the switch is deactivated
    stepper
.moveTo(initial_homing);  
    stepper
.run();
    initial_homing
--;
    delay
(5);
 
}
 
  stepper
.setCurrentPosition(initial_homing);
 
Serial.println("Clockwise homing Completed");
 
Serial.println("");
 
Serial.print("Initial Homing: ");
 
Serial.println(initial_homing);
 
  stepper
.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Faster for regular movements)
  stepper
.setAcceleration(100.0);  // Set Acceleration of Stepper


// Print out Instructions on the Serial Monitor at Start
 
Serial.print("Enter destination position between 0 and ");
 
Serial.println(initial_homing);
}


void loop() {


 
while (Serial.available() > 0)  { // Check if values are available in the Serial Buffer


  move_finished
=0;  // Set variable for checking move of the Stepper
 
  destination
= Serial.parseInt();  // Put numeric value from buffer in destination variable
   
Serial.print("Initial value of destination: ");
   
Serial.println(destination);
 
if (destination < 0 || destination > initial_homing) {  // Make sure the position entered is not beyond the HOME or MAX position
   
Serial.println("");
   
Serial.print("Please enter a value greater than zero and smaller or equal to ");
   
Serial.print(initial_homing);
   
Serial.println(".....");
   
Serial.println("");
   
} else {
   
Serial.print("Moving stepper into position: ");
   
Serial.println(destination);
    stepper
.moveTo(destination);  // ******************* Set new moveto position of Stepper
   
Serial.print("The target position is: ");
   
Serial.println(stepper.targetPosition());


    stepper
.setSpeed(5000.0);
    stepper
.setAcceleration(500.0);
    delay
(1000);  // Wait 1 seconds before moving the Stepper
   
}
 
}


 
if (destination >= 0 && destination <= initial_homing) {
   
// Check if the Stepper has reached desired position
   
if ((stepper.distanceToGo() != 0)) {   // *******************
     
Serial.print("Current position is: ");
     
Serial.print(stepper.currentPosition());
     
Serial.print(" - distance to target position: ");
     
Serial.println(stepper.distanceToGo());
     
     stepper
.run();  // ******************* Move Stepper into position
   
}


   
// If move is completed display message on Serial Monitor
   
if ((move_finished == 0) && (stepper.distanceToGo() == 0)) {
     
Serial.println("COMPLETED!");
     
Serial.println("");
     
Serial.print("Enter destination position between 0 and ");
     
Serial.println(initial_homing);
      move_finished
=1;  // Reset move variable
   
}
 
}
}

Mike McCauley

unread,
Feb 20, 2019, 1:18:03 AM2/20/19
to accels...@googlegroups.com
Are you sure you have your stepper wired correctly?  Do you see the same behavior with the example sketches eg bounce?

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Geoff Smith

unread,
Feb 20, 2019, 3:27:23 AM2/20/19
to accelstepper
Further to Mike's comments...

My first thought is that it could possibly be a 'back emf' issue caused by an initial surge of current which then collapses in the opposite direction causing the initial step reversal. I had a quick look at the datasheets and at first glance, couldn't see any current limiting control on the driver, (some one else with more experience might explain). The driver datasheet states average current limiting at 1.2A and 3.2A peak and continuous 20ms pulsing at 2A. I see the motor is rated at 350ma per phase.

Does the motor and/or driver get hot or has been hot at some stage?

Is it possible that the driver has been subject to a current overload and the 'built-in' back emf diodes have been 'fritzed'. Perhaps try another 'new' driver board if you haven't already done so, but before you do that can I suggest checking the current draw on the existing setup first.

Robert Cox

unread,
Feb 20, 2019, 3:58:43 PM2/20/19
to accelstepper
Hi Mike - 

Thanks for your response. I just tried your bounce sketch and it worked as expected. I even added the currentPosition() and distanceToGo() debug lines in the loop() to make sure that I wasn't missing any steps when the motor reversed direction.

I assume that means that the stepper is wired correctly, yes?

Best,
-Rob

Robert Cox

unread,
Feb 20, 2019, 4:11:52 PM2/20/19
to accelstepper
Hi Geoff - thanks for your thoughtful feedback, these are certainly items that I haven't thought about. To answer some of your questions:

 - Does the motor and/or driver get hot or has been hot at some stage?
        The motor gets warm, my infrared thermometer says it is about 100 degrees F. The driver is not hot at all.

 - Is it possible that the driver has been subject to a current overload?
        That is certainly possible, I do have another driver breakout board that I can plop in and see if the behavior is the same.
        It is interesting to me that the bounce example sketch doesn't exhibit this strange behavior.

 - I suggest checking the current draw on the existing setup first.
        The current of the entire system (Arduino, two variable DC-DC voltage regulators, 4x20 LCD, and a few other components) is 400mA.
        The NEMA-17 motor itself is drawing about 100mA according to my bench DC power supply.

Thanks for your help, and I'd welcome any further ideas. I'll replace the driver board and report back. I also have some Sparkfun EasyDrivers that came in the mail today that I may try too.

Best,
-Rob 

Robert Cox

unread,
Feb 20, 2019, 6:44:32 PM2/20/19
to accelstepper
Hi Geoff - 

Just a quick update. I replaced the Adafruit TB6612 with a brand-new fresh out of the static-bag one and the stepper acts exactly the same. It still reverses exactly 11 steps clockwise and then reverses to go back counter clockwise through its original position and to the destination position.

As I mentioned before, the bounce sketch works as expected. I can't explain this... there must be something in my code that is causing the issue, but I sure can't find it.

I look a little more and then if I don't find the issue, I'll try the EasyDriver from Sparkfun and see what happens.

Best,
-Rob


On Wednesday, February 20, 2019 at 4:11:52 PM UTC-5, Robert Cox wrote:
... I'll replace the driver board and report back. I also have some Sparkfun EasyDrivers that came in the mail today that I may try too.

Geoff Smith

unread,
Feb 20, 2019, 8:12:26 PM2/20/19
to accelstepper
Thanks for the reply Robert,

It does sound like all is ok with your hardware and setup etc... Seeing as how the bounce sketch works ok I tend to agree that it's possibly in your code.

The process of elimination goes on...  good luck with it.


On Wednesday, 20 February 2019 11:44:21 UTC+11, Robert Cox wrote:
Reply all
Reply to author
Forward
0 new messages