Driving a 28byj-48 + ULN2003a board with the AccelStepper library

Hi there,
I'm currently try to set up the Sardauscan 3D scanner (Sardauscan) and I'm having some problems with the AccelStepper library, I think.
To be sure the motor was working I uploaded a simple code using the standard Stepper library, and it moves smoothly. But when I upload the Sardauscan code it moves really "glitchy".. I tried manipulating maxSpeed, acceleration and speed values with no luck. I've also tried some AccelStepper examples, but none worked..

Below, the code:

1)Main Sardauscan program

#include "SerialCommand.h"
#include <AccelStepper.h>
#include "configuration.h"
/*
Commands :
identity

sardauscan =>ask if this is a sardaucan => response "yes" //

laser :
L => ask laser configuration => response number of lasers
L x => ask status of laser index X
      => 'x' laser number
L x y  => turn laser on or off 
    'x' laser number
    'y' state: 0=off 1=on

Table :
T => table position
T S => return the number of steps for a revolution 
T C => Set Current position as 0 (for absolute positionning
T R xxx => rotate relatively xxx steps 
T A xxx => turn position absolute xxx steps 

unknow command 
=> response "Hun?"

*/

AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);


SerialCommand SCmd;

int RotationToSteps(int rotation) //
{
  return rotation*STEP_BY_MINMOVE;
}
int StepsToRotation(int rot)
{
  return rot/STEP_BY_MINMOVE;
}
void Identification()
{
 Serial.println("yes");
}
void Hun()
{
 Serial.println("Hun?");
}
void TableCommand()
{
 char *arg; 
 arg = SCmd.next(); 
 if (arg != NULL) 
 {
 if(arg[0]=='R'||arg[0]=='r') // T R
 {
                    char *arg2 = SCmd.next(); 
                    int pos=atoi(arg2);
                    stepper1.move(RotationToSteps(pos));
                    stepper1.runToPosition();
    Serial.print("RELATIVE ROTATION :"); 
    Serial.print(pos); 
    Serial.print(" => "); 
    Serial.println(StepsToRotation(stepper1.currentPosition())); 
 }
 else if(arg[0]=='A'||arg[0]=='a') // T A
 {
                    char *arg2 = SCmd.next(); 
                    int pos=atoi(arg2);
                    stepper1.moveTo(RotationToSteps(pos));
                    stepper1.runToPosition();
    Serial.print("ABSOLUTE ROTATION "); 
    Serial.print(pos); 
    Serial.print(" => "); 
    Serial.println(StepsToRotation(stepper1.currentPosition())); 
 }
 else if(arg[0]=='S'||arg[0]=='s') //T S
                  {
    Serial.print("REVOLUTION STEPS "); 
    Serial.print(" => "); 
    Serial.println(StepsToRotation(REVOLUTION_STEP)); 
                  }
 else if(arg[0]=='C' ||arg[0]=='c') //T C
                  {
                    stepper1.setCurrentPosition(0);
    Serial.print("RESET CUTTENT POSITION "); 
    Serial.print(" => "); 
    Serial.println(StepsToRotation(stepper1.currentPosition())); 
                  }
                 else
                 {
    Serial.print("Unknown Table command :"); 
    Serial.println(arg); 
                 }
 } 
  else {
     Serial.print("Position "); 
    Serial.println(StepsToRotation(stepper1.currentPosition())); 
  }
}
int getLaserPin(int laserIndex)
{
       if(laserIndex==0)
         return LASER_PIN_1; 
       else if(laserIndex==1)
         return LASER_PIN_2; 
       else if(laserIndex==2)
         return LASER_PIN_3; 
       else if(laserIndex==3)
         return LASER_PIN_4; 
       else
         return (-1); 
}

void LaserCommand()
{
  char *arg; 
  arg = SCmd.next(); 
  if (arg != NULL) 
  {
    int laserIndex=atoi(arg);
    char *arg2 = SCmd.next(); 
    if (arg2 == NULL) 
    {
       Serial.print("LASER_STATE: "); 
       Serial.print(laserIndex); 
       int pin =getLaserPin(laserIndex);
       Serial.print("("); 
       Serial.print(pin); 
       Serial.print(") = "); 
       if(pin>=0)
         Serial.println(digitalRead(pin)); 
       else
         Serial.println(-1); 
    }
    else 
    {
       int laserState = atoi(arg2);
       int pin =getLaserPin(laserIndex);
       if(pin>=0)
        digitalWrite( pin, laserState==1?HIGH:LOW);
       Serial.print("SET_LASER: "); 
       Serial.print(laserIndex); 
       Serial.print("("); 
       Serial.print(pin); 
       Serial.print(") = "); 
      Serial.println(digitalRead(pin)); 
     }
   }
   else 
   {
    Serial.print("LASER_COUNT: "); 
    Serial.println(LASER_COUNT); 
   }
}



void setup() {
  stepper1.setMaxSpeed(500.0);
  stepper1.setAcceleration(200.0);
  stepper1.setSpeed(400);
  stepper1.moveTo(0);
  stepper1.runToPosition();
  Serial.begin(SERIAL_BAUD);
 
  pinMode(LASER_PIN_1, OUTPUT);
  pinMode(LASER_PIN_2, OUTPUT);
  pinMode(LASER_PIN_3, OUTPUT);
  pinMode(LASER_PIN_4, OUTPUT);
  digitalWrite(LASER_PIN_1, LOW);
  digitalWrite(LASER_PIN_2, LOW);
  digitalWrite(LASER_PIN_3, LOW);
  digitalWrite(LASER_PIN_4, LOW);
  SCmd.addCommand("sardauscan",Identification);
  SCmd.addCommand("T",TableCommand);
  SCmd.addCommand("t",TableCommand);
  SCmd.addCommand("L",LaserCommand);
  SCmd.addCommand("l",LaserCommand);
  SCmd.addDefaultHandler(Hun);
  Serial.println(FIRMWARE_VERSION);
  Serial.flush();
}


void loop() {
 SCmd.readSerial();     // We don't do much, just process serial commands
        Serial.flush();
}

2)Sardauscan second file

#ifndef CONFIGURATION_h
#define CONFIGURATION_h

#define FIRMWARE_VERSION "Sardauscan V0.1a"

#define SERIAL_BAUD 115200
//57600

// Motor definitions
#define motorPin1  2     // IN1 on the ULN2003 driver 1
#define motorPin2  3     // IN2 on the ULN2003 driver 1
#define motorPin3  4     // IN3 on the ULN2003 driver 1
#define motorPin4  5     // IN4 on the ULN2003 driver 1

//tips (from Mark Benson)
//If anyone else is having problems with a BYJ48 stepper not doing anything, 
//change the HALFSTEP value to 4 & REVOLUTION_STEP to 2048
#define HALFSTEP 4 //8
#define REVOLUTION_STEP 2048 //4096
#define STEP_BY_MINMOVE 4  // move by STEP_BY_MINMOVE (to avoid power loss when position is between step) 

#define LASER_COUNT 4
#define LASER_PIN_1 13 //yellow
#define LASER_PIN_2 A1 //orange
#define LASER_PIN_3 A2 //green
#define LASER_PIN_4 A3 //blue

#endif

3)Example program used (using standard Stepper library)

#include   <Stepper.h>
 
Stepper myStepper(2048,2,4,3,5);
 
void setup() {
 
  myStepper.setSpeed(10);
}
 
void loop() {
 
  myStepper.step(2048);
  delay(1000);  
  myStepper.step(-2048);
  delay(1000);
}

I'm going to upload the official wiring and a photo of mine in the attachments, it could be useful..
I really don't know what to do to solve the problem, any suggestion is appreciated! :smiley:

EDIT: it seems I can't upload the photo of my wiring, because "it failed the security check".. if needed, I'll try again!
EDIT2: I uploaded it as a zip file!

sardauscan wiring.jpg

my wiring.zip (1.14 MB)

Helo, I'm also have same issues with AccelStepper.h library. Will appreciate if someone give a hint where the problem is :slight_smile:

My two scenarios (voltage is 9V in both scenarios):

  1. With code from http://www.instructables.com/id/BYJ48-Stepper-Motor/ stepper works great:
/*
   BYJ48 Stepper motor code
   Connect :
   IN1 >> D8
   IN2 >> D9
   IN3 >> D10
   IN4 >> D11
   VCC ... 5V Prefer to use external 5V Source
   Gnd
   written By :Mohannad Rawashdeh
  https://www.instructables.com/member/Mohannad+Rawashdeh/
     28/9/2013
  */

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
int Steps = 0;
boolean Direction = true;// gre
unsigned long last_time;
unsigned long currentMillis ;
int steps_left=4095;
long time;
void setup()
{
Serial.begin(115200);
pinMode(IN1, OUTPUT); 
pinMode(IN2, OUTPUT); 
pinMode(IN3, OUTPUT); 
pinMode(IN4, OUTPUT); 
// delay(1000);

}
void loop()
{
  while(steps_left>0){
  currentMillis = micros();
  if(currentMillis-last_time>=1000){
  stepper(1); 
  time=time+micros()-last_time;
  last_time=micros();
  steps_left--;
  }
  }
   Serial.println(time);
  Serial.println("Wait...!");
  delay(2000);
  Direction=!Direction;
  steps_left=4095;
}

void stepper(int xw){
  for (int x=0;x<xw;x++){
switch(Steps){
   case 0:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break; 
   case 1:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, HIGH);
   break; 
   case 2:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break; 
   case 3:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break; 
   case 4:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
   case 5:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
     case 6:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
   case 7:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break; 
   default:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
}
SetDirection();
}
} 
void SetDirection(){
if(Direction==1){ Steps++;}
if(Direction==0){ Steps--; }
if(Steps>7){Steps=0;}
if(Steps<0){Steps=7; }
}
  1. With code from 28BYJ-48 Stepper Motor with ULN2003 driver and Arduino Uno – 42 Bots stepper doesn't work. If change some parameters with speed or steps, then it starts moving with missing steps (a lot), practically it trembling
#include <AccelStepper.h>
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup() {
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(100.0);
  stepper1.setSpeed(200);
  stepper1.moveTo(20000);

}//--(end setup )---

void loop() {

  //Change direction when the stepper reaches the target position
  if (stepper1.distanceToGo() == 0) {
    stepper1.moveTo(-stepper1.currentPosition());
  }
  stepper1.run();
}

Hi,
Have you tried swapping the stepper signal pins around?
Not all steppers use the same colour sequence, and not all libraries use the same output sequence.

Please do not attach zip files, forum members will not open them.
If you image file is too large, re-scale it in graphics app before attaching.

The OPs. sardu image.
sardauscan wiring.jpg
Try swapping IN1, IN2, IN3 and IN4 around.

Tom.... :slight_smile:

Hello and thank You Tom for Your answer. I read more abou swapping stepper signal pins and I found my mistake.

First it was like:
AccelStepper stepper1(HALFSTEP, IN1, IN2, IN3, IN4);

But it should be:
AccelStepper stepper1(HALFSTEP, IN1, IN3, IN2, IN4);

Its okay now, but I have read a lot and in other blogs there was saying that recomendation for 28BYJ-48 stepper motor is to use HALFSTEP mode (8 step). Problem is that with this setting my motor is not working. It's working only if I use 4 step (FULLSTEP), but with it - motor gets realy hot, not warm, but very hot. (and there is no any load yet, motor could move freely)

I paste part of code I using to understand what I talking about:

#include <AccelStepper.h>

#define FULLSTEP 4
#define CURTAIN_CLOSED 10000
#define CURTAIN_OPEN 0
#define CHILD_ID 1

MyMessage message(CHILD_ID, S_COVER);

#define IN1  6     // IN1
#define IN2  7     // IN2
#define IN3  8     // IN3
#define IN4  9     // IN4
 
AccelStepper stepper1(FULLSTEP, IN1, IN3, IN2, IN4);

void setup()
{
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(100.0);
  stepper1.setSpeed(200);
  stepper1.moveTo(CURTAIN_OPEN);
}

If others can use 8 step normaly, but I can't, what could be cause of it? Does my motor could be broken and thats why it working only with 4 steps?

Thank You!

Hello,

I sharing my last results. HALFSTEP mode was not working becose of use AccelStepper 1.3 version library. I update library to latest version 1.57 and HALFSTEP with 8 steps is now working.