Getting 720deg rotations with bounce example (AccelStepper.h library)

Hi,

Having worked my way through the 33 tutorials of an ultimate starter kit, I’ve bought myself a stepper driver and stepper motor :-

Elegoo Mega 2560 R3 board
Geekcreit A4988 stepper driver + heat sink. Current limit - adjusted Vref to give 0.6V (1.5A).
Nema 17, bipolar stepper motor, 4 lead, 26Ncm, 12V 1.5A, 1.8° steps, 30 Ohm coils.

I’ve been following this website, which has a lot of helpful information.

I can successfully drive the stepper motor (200 steps) 360° clockwise and anticlockwise using the standard Arduino library :-

// Define pin connections & motor's steps per revolution
const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 200;

void setup()
{
  // Declare pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}
void loop()
{
  // Set motor direction clockwise
  digitalWrite(dirPin, HIGH);

  // Spin motor slowly
  for(int x = 0; x < stepsPerRevolution; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }
  delay(1000); // Wait a second
  
  // Set motor direction counterclockwise
  digitalWrite(dirPin, LOW);

  // Spin motor quickly
  for(int x = 0; x < stepsPerRevolution; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
  delay(1000); // Wait a second
}

However, when I attempt to do the same using the “accelstepper.h” library, with the bounce example, I don’t get a 360° clockwise rotation with equal and opposite rotation back to the start point. Instead it is doing a 720° rotation clockwise followed by the same rotation anticlockwise.

Here is the code (“AccelStepper stepper;” line modified to use the driver board) :-

// Bounce.pde
// -*- mode: C++ -*-
//
// Make a single stepper bounce from one limit to another
//
// Copyright (C) 2012 Mike McCauley
// $Id: Random.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 3, 2); // changed from "AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5"

void setup()
{  
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(100);
  stepper.setAcceleration(20);
  stepper.moveTo(200);
}

void loop()
{
    // If at the end of travel go to the other end
    if (stepper.distanceToGo() == 0)
      stepper.moveTo(-stepper.currentPosition());

    stepper.run();
}

Am I wrong to expect a 360° rotation in both directions? It is almost like it is in a double stepping mode!

I have even tried grounding the MS1/2/3 pins (though this should not be necessary as they are pulled low by default) to enforce full stepping control, but this made no difference. I’ve also tried a second version of the same driver board with the same outcome.

I would be grateful for any help as to why I’m getting 720° rotations and not the expected 360° ones.

Thanks,

Andrew

Your second example is not the same as the one they use in the link you provided.

// Define motor interface type
#define motorInterfaceType 1

// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
	// set the maximum speed, acceleration factor,
	// initial speed and the target position
	myStepper.setMaxSpeed(1000);
	myStepper.setAcceleration(50);
	myStepper.setSpeed(200);
	myStepper.moveTo(200);
}

Double check to be sure.

Thanks for the suggestion. I had already tried that but reverted to the bounce example as this code was also giving 720° rotations, but at a faster speed. I’ve retried it just now and can confirm this is still the case.

// Include the AccelStepper Library
#include <AccelStepper.h>

// Define pin connections
const int dirPin = 2;
const int stepPin = 3;

// Define motor interface type
#define motorInterfaceType 1

// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  // set the maximum speed, acceleration factor,
  // initial speed and the target position
  myStepper.setMaxSpeed(1000);
  myStepper.setAcceleration(50);
  myStepper.setSpeed(200);
  myStepper.moveTo(200);
}

void loop() {
  // Change direction once the motor reaches target position
  if (myStepper.distanceToGo() == 0) 
    myStepper.moveTo(-myStepper.currentPosition());

  // Move the motor one step
  myStepper.run();
}

Answering my own question :-

Am I wrong to expect a 360° rotation in both directions?

Yes. The accelstepper.h examples above are indeed causing the stepper motor to move 720degrees or 400steps.

Void setup first prepares the stepper to moveTo +200, an absolute position, which when triggered by the loop results in a 360degree clockwise rotation. Once it has done this the currentPosition function (absolute position) returns +200. Likewise the distanceToGo function returns 0 (see snippet below)) and the moveTo function then moves to the -200 steps (-currentPosition) absolute position, which is a swing of 400steps or 720° anticlockwise.

if (stepper.distanceToGo() == 0)
stepper.moveTo(-stepper.currentPosition());

At -200 absolute currentPosition distanceToGo is again 0. The stepper moveTo function is then set back to +200 absolute position, resulting in a 720° clockwise rotation. The process repeats.

Code to get my desired 360° clockwise then anticlockwise rotations:-

#include <AccelStepper.h>

// Define pin connections
const int dirPin = 2;
const int stepPin = 3;
const long desiredSteps = 200; // 200 steps = 1 rotation
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);   //Use the Driver board with step and direction control.

void setup()
{  
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(100);
  stepper.setAcceleration(20);
  stepper.moveTo(desiredSteps);  //stepper motion setup to be to the absolute position = +desiredSteps (+200), a clockwise rotation.

}

void loop()
{
    // Switch to anticlockwise motion, then clockwise motion
    if (stepper.currentPosition() == desiredSteps)  //Now at +200 (+desiredSteps) position
     {
      stepper.moveTo(0);  //anticlockwise move to position 0 (from +desiredSteps position)
     }
    else if (stepper.currentPosition() == 0)  // Now at 0 position
    {
      stepper.moveTo(desiredSteps);  //clockwise move to position +200 (from 0 position)
    }



    stepper.run();
}

Thanks for putting your answer up.
I was away on family business.