stepper 1 revolution example sketch wont work?

hey, im trying to use a 28BYJ-48 stepper motor with uln2003 driver using a arduino mega however it does not work? I need to turn the Stepper a precise angle so any suggestions to fix the example or new code? I tried other examples and it worked fine but they are really complex for my small head…

#include <Stepper.h>

const int stepsPerRevolution = 4096;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(100);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

}

How are you supplying power to the stepper? Please post a wiring diagram (schematic).

I got my 28BYJ to work with this Tutorial

...R

stepper is powered fine. another code source made it work but is not easy to use and ill have a look :slight_smile:

Try this version - it also worked for me

#include <Stepper.h>

const int stepsPerRevolution = 32;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 3, 4, 5, 6);



void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(360);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(512);

}

…R

the code you tried also didnt work, the motor seems to just humm. I tried the tutorial but it get "error compiling for board arduino or mega 2650"

never mind i forgot to install the library lol, it now works but the motor spins continuously. Is there a way to move the stepper a precise amount? its for a gauge on a flight simulator. Thanks for the help!

#include <AccelStepper.h>
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  8     // IN1 on the ULN2003 driver 1
#define motorPin2  9     // IN2 on the ULN2003 driver 1
#define motorPin3  10     // IN3 on the ULN2003 driver 1
#define motorPin4  11    // 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();
}

trying to interface it with my flight sim now… the motor doesnt move? any ideas?

/* 
    This code is in the public domain
    For use with "Link2fs_Multi"
    Jimspage.co.nz
    My thanks to the Guys that gave me snippets of code. 
    
    Here is the basic concept of my lay-outs.
    You could actually use this INO as a starting point for your own project.
    Deleting all the comments makes it really simple to follow once you understand the concept
*/
#include <AccelStepper.h>
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  8     // IN1 on the ULN2003 driver 1
#define motorPin2  9     // IN2 on the ULN2003 driver 1
#define motorPin3  10     // IN3 on the ULN2003 driver 1
#define motorPin4  11    // 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);

String spd;
int spdi;

int CodeIn;// The normal declearations go here

void setup(){
   Serial.begin(115200); 
   stepper1.setMaxSpeed(1000.0);
   stepper1.setAcceleration(100.0);
   stepper1.setSpeed(200);
       // The normal startup stuff goes here
}

void loop() {
  {OTHER();}// Check for "Other" things to do. (Non extraction stuff)
  if (Serial.available()) {  //Check if anything there
    CodeIn = getChar();      //Get a serial read if there is.
    if (CodeIn == '=') {EQUALS();} // The first identifier is "=" ,, goto void EQUALS
    if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<" ,, goto void LESSTHAN
    if (CodeIn == '?') {QUESTION();}// The first identifier is "?" ,, goto void QUESTION
    if (CodeIn == '/') {SLASH();}// The first identifier is "/"  ,, goto void SLASH (Annunciators)
  }
  stepper1.moveTo(spdi);
  stepper1.runToPosition();
}

char getChar()// Get a character from the serial buffer(Dont touch)
{
  while(Serial.available() == 0);// wait for data (Dont touch)
  return((char)Serial.read());// (Dont touch) Thanks Doug
}

void OTHER(){
/* In here you would put code that uses other data that
cant be put into an "extraction void" that references something else.
Also in here you would put code to do something that was not
relying on a current extraction.
(Remember - The identifier voids only trigger when it receives that identifier)
*/
}

void EQUALS(){      // The first identifier was "="
       CodeIn = getChar(); // Get another character
  switch(CodeIn) {// Now lets find what to do with it
    case 'A'://Found the second identifier
       //Do something
    break;
     
    case 'W':
       //Do something
    break;
     
    case 'a':
       //Do something
    break;
       //etc etc etc
       // You only need the "Case" testing for the identifiers you expect to use.
     }
}

void LESSTHAN(){    // The first identifier was "<"
      //Do something (See void EQUALS)
      switch(CodeIn) {// Now lets find what to do with it
        case 'P':
        {
           spd = "";
           spd += getChar();
           spd += getChar();
           spd += getChar();
           spdi = spd.toInt();
        }

      }
}

void QUESTION(){    // The first identifier was "?"
       //Do something (See void EQUALS)
}

void SLASH(){    // The first identifier was "/" (Annunciators)
  //Do something (See void EQUALS)
}

What sort of data are you sending to the Arduino - give some samples.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

The arduino gets something like “<P090”

so the stepper should get 090.

georgegohl888:
The arduino gets something like “<P090”

You are saying what the Arduino gets. But You have not said what is being sent to it. At this stage of diagnosis that is the important bit.

…R

Note that these small steppers don't always have integer ratio gear trains. If your motor is going to be turning in the same direction for many turns you may need to add a correction factor.

See: https://grahamwideman.wikispaces.com/Motors-+28BYJ-48+Stepper+motor+notes

i ended up using a different library and sot of figured things out thanks for the help :slight_smile: