HALF4WIRE stepper problem with AccelStepper

310 views
Skip to first unread message

Evan Jones

unread,
Apr 1, 2018, 6:24:06 AM4/1/18
to accelstepper
I found problems with trying to run a 28BYJ-48 5VDC stepper using AccelStepper library in the HALF4WIRE mode
I pulled some code from the examples in the library which uses a pot to control the position of the stepper, where I defined
the mode and pins used by the stepper. The code did not work for HALF4WIRE mode. The code fragment of program I was using:

#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::HALF4WIRE, 8, 9, 10, 11);

// This defines the analog input pin for reading the control voltage
// Tested with a 10k linear pot between 5v and GND
#define ANALOG_IN A0                      // Analog pin pot is connected to

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  // Configure each stepper
  stepper.setMaxSpeed(1200);              // Set maximum stepper speed (steps/second)
}

void loop() {
  // put your main code here, to run repeatedly:
  int analog_in = analogRead(ANALOG_IN);  // read the desired position information
  stepper.moveTo(4*analog_in);            // Set the desired target position
  stepper.setSpeed(1200);                  // set the desired stepper speed (steps/second)
  stepper.runSpeedToPosition();           //  move motor (with accel/decel) to position

}

I had a look at the library file defined for accelstepper and found this code relating to HALF4WIRE in the accelstepper.cpp file:

// 4 pin half step function
// This is passed the current step number (0 to 7)
// Subclasses can override
void AccelStepper::step8(long step)
{
    switch (step & 0x7)
    {
    case 0:    // 1000
        setOutputPins(0b0001);
            break;
       
        case 1:    // 1010
        setOutputPins(0b0101);
            break;
       
    case 2:    // 0010
        setOutputPins(0b0100);
            break;
       
        case 3:    // 0110
        setOutputPins(0b0110);
            break;
       
    case 4:    // 0100
        setOutputPins(0b0010);
            break;
       
        case 5:    //0101
        setOutputPins(0b1010);
            break;
       
    case 6:    // 0001
        setOutputPins(0b1000);
            break;
       
        case 7:    //1001
        setOutputPins(0b1001);
            break;
    }
}


I believe the output pin values are cycling incorrectly. Perhaps should read:

// 4 pin half step function
// This is passed the current step number (0 to 7)
// Subclasses can override
void AccelStepper::step8(long step)
{
    switch (step & 0x7)
    {
    case 0:    // 0001
        setOutputPins(0b0001);
        break;
       
    case 1:    // 0011
        setOutputPins(0b0011);
        break;
       
    case 2:    // 0010
        setOutputPins(0b0010);
        break;
       
    case 3:    // 0110
        setOutputPins(0b0110);
        break;
       
    case 4:    // 0100
        setOutputPins(0b0100);
        break;
       
    case 5:    // 0110
        setOutputPins(0b1100);
        break;
       
    case 6:    // 1000
        setOutputPins(0b1000);
        break;
       
    case 7:    // 1001
        setOutputPins(0b1001);
        break;
    }
}
When I changed AccelStepper.cpp to reflect these changes the stepper behaved as I would have expected.

Mike McCauley

unread,
Apr 1, 2018, 5:21:11 PM4/1/18
to accels...@googlegroups.com
Hello,

or instead you could change your wiring to suit your motor?

Cheers.
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com 5R3MRFM2+X6
Phone +61 7 5598-7474

gregor

unread,
Apr 3, 2018, 8:08:30 AM4/3/18
to accelstepper
alternatively, you could change the sequence of pins in the AccelStepper contstructor call.
Reply all
Reply to author
Forward
0 new messages