element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs Arduino controlling Stepper Motor 28BYJ-48 with Accelstepper Library
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: neilk
  • Date Created: 24 Jan 2015 6:16 PM Date Created
  • Views 15587 views
  • Likes 5 likes
  • Comments 3 comments
  • 28byj-48
  • accelstepper
  • stepper-motor
  • arduino
Related
Recommended

Arduino controlling Stepper Motor 28BYJ-48 with Accelstepper Library

neilk
neilk
24 Jan 2015

The 28BYJ-48 stepper motor with a ULN2003 driver board is readily and cheaply available on eBay. With nothing special in mind, other than curiosity,  I bought one and had a quick play with it, using the standard Arduino Stepper library.

image


I managed to do little more than demonstrate that I could make the motor rotate, before I had to put it on one side because of other demands on my time..


Moving on several months to Christmas, my wife bought me a Lego Technic kit  as a joke present - she may come to regret that joke ! As an ex-Meccano builder I was fascinated by the different approach needed for Lego Technic and soon became interested in motorising the kit. and possibly having Arduino control. Lego motors seemed a bit pricey so I thought about using the 28BYJ-48. A quick Google search brought up a number of postings showing how various motors had been "legoised", so I "legoised" my 28BYJ-48 - it's quite easy!


When I started to play with some of the example sketches from the standard Stepper library, I soon realised that they didn't drive the 28BYJ-48 properly, so I Googled again and came up with several links, including this excellent posting:


    http://42bots.com/tutorials/28byj-48-stepper-motor-with-uln2003-driver-and-arduino-uno/


The author - Stan - has written a brilliant article, which explains everything perfectly and also provides the link to the AccelStepper library, developed and supported by Mike McCauley :


      http://www.airspayce.com/mikem/arduino/AccelStepper/


The crucial things I learned from Stan's posting were:


  1. The 28BYJ-48 needs to be operated in half-step mode, ie an 8 pulse sequence - the standard Arduino Stepper library can't do this.
  2. The 28BYJ-48 coils need to be energised in the pin sequence 1,3,2,4, rather than the standard 1,2,3,4 sequence


Below is a simple sketch, based on one of the examples provided by Mike Mcauley and adapted by Stan and then by me, for the 28BYJ-48, to demonstrate some of the features of the AccelStepper library.

 

During Setup, the initial parameters of the motor (maximum speed, acceleration and (final)speed) are set up, together with an instruction to move the number of steps defined by integer variable endpoint ie 1024 or approximately 1/4 revolution, in a clockwise direction, because endpoint is positive.


(see Stan's article for further information about the 28BYJ-48 step angle and gearing, which results in 4076 steps for a full revolution, instead of the expected 4096 steps.)


The motor does not move until the command stepper1.run(); is executed  in the main loop. At this point, dependent on the initial parameters of the motor and the time elapsed since the command was last executed, a number of steps are initiated. Each time through the main loop, the perceived position of the motor is tested and the motor is moved, until it reaches endpoint.


When the motor has reached endpoint, determined by if(stepper1.distanceToGo() ==0)


  1. the current motor position is output for confirmation that endpoint has been reached
  2. the current motor position is reset to 0
  3. the value of endpoint is negated (so the motor will move in the reverse direction approximately 1/4 revolution)
  4. the motor is instructed to move to endpoint
  5. The current motor position is output, to confirm that the motor has not yet moved and the current position is still 0.



#include <AccelStepper.h>

#define HALFSTEP 8

#define motorPin1  8     // IN1 on ULN2003 ==> Blue   on 28BYJ-48
#define motorPin2  9     // IN2 on ULN2004 ==> Pink   on 28BYJ-48
#define motorPin3  10    // IN3 on ULN2003 ==> Yellow on 28BYJ-48
#define motorPin4  11    // IN4 on ULN2003 ==> Orange on 28BYJ-48

int endPoint = 1024;        // Move this many steps - 1024 = approx 1/4 turn

// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup()
{
  Serial.begin(9600);
  Serial.println(stepper1.currentPosition());
  delay(5000);
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(100.0);
  stepper1.setSpeed(200);
  stepper1.moveTo(endPoint);
}

void loop()
{
  //Change direction at the limits
    if (stepper1.distanceToGo() == 0)
   {
     Serial.println(stepper1.currentPosition());
     stepper1.setCurrentPosition(0);
     endPoint = -endPoint;
     stepper1.moveTo(endPoint);
     Serial.println(stepper1.currentPosition());
   }
    stepper1.run();
}

 


A very common application for stepper motors is that of accurate and repeatable linear positioning. My Lego Technic kit included a short length of toothed rack and a mating pinion gear. Using my new found Lego Technic skills, I created a simple demonstration system which had the Arduino driving the stepper motor to move the rack backwards and forwards.

 

I soon realised that this was too simplistic - at power on, at the start of the sketch, the stepper motor is assumed to be at position 0, when,  in reality, it could be anywhere, so the sketch needs to "home" the motor to a known physical position and then set that as position 0 in software. This can be achieved with a limit switch at one end of the rack travel, with the switch applying an interrupt to pin 2 (int0) of the Arduino.


Here is a simple sketch to demonstrate the principle of the "homing" process.

 

#include <AccelStepper.h>

#define HALFSTEP 8

#define motorPin1  8     // IN1 on ULN2003 ==> Blue   on 28BYJ-48
#define motorPin2  9     // IN2 on ULN2004 ==> Pink   on 28BYJ-48
#define motorPin3  10    // IN3 on ULN2003 ==> Yellow on 28BYJ-48
#define motorPin4  11    // IN4 on ULN2003 ==> Orange on 28BYJ-48

int endPoint = 1024;     // Move this many steps; 1024 = approx 1/4 turn

// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

volatile boolean stopNow = false;    // flag for Interrupt Service routine
int cycleCount = 0;                  // counter for endpoint cycles
boolean homed = false;               // flag to indicate when motor is homed

void setup()
{
  attachInterrupt(0, intService, LOW);           // Enable interrupt 0, switch pulled low
  Serial.begin(9600);
  Serial.println(stepper1.currentPosition());
  delay(5000);

  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(100.0);
  stepper1.setSpeed(200);
  stepper1.moveTo(endPoint);
}

void loop()
{
  if (stopNow)
   {
     detachInterrupt(0);                          // Interrupt not needed again (for the moment)
     Serial.print("Interrupted at ");
     Serial.println(stepper1.currentPosition());
     stepper1.stop();
     stepper1.setCurrentPosition(0);
     Serial.print("position established at home...");
     Serial.println(stepper1.currentPosition());
     stopNow = false;                            // Prevents repeated execution of the above code
     homed = true;
   }
  else
  if (stepper1.distanceToGo() == 0 && !homed)     //executed repeatedly until we hit the limitswitch
   {
     Serial.println(stepper1.currentPosition());
     stepper1.setCurrentPosition(0);
     stepper1.moveTo(endPoint);
     cycleCount ++;
     Serial.print("cycle count = ");
     Serial.println(cycleCount);
     Serial.println(stepper1.currentPosition());
   }
  stepper1.run();
  if(homed)
   {
     Serial.println("I am homed");   //Prints continually once homed
   }
}
//
// Interrupt service routine for INT 0
//
void intService()
{
  stopNow = true; // Set flag to show Interrupt recognised and then stop the motor
}

 

As the ideas for the application develops, it might be necessary to have a limitswitch at each end of the rack travel, with each switch activating a different interrupt, to prevent accidental overrun. Etc Etc


My next experiment might be to control the stepper motor - stop, start, forwards, backwards etc, via Bluetooth, from an Android App, developed with MIT App Inventor.

  • Sign in to reply

Top Comments

  • DAB
    DAB over 10 years ago +2
    Nice post. Not all stepper motors work the same way, but you were lucky to find a good starting point and adapt the software to make it work. DAB
  • nino65
    nino65 over 4 years ago

    Ottimo,

    Volevo controllare la rotazione di questo stepper eregistrare la sua posizione in mancanza di tensione.

    Questo sketch quando si interrompe la tensione non riparte dalla posizione dove si era spento, c'è un modo per ritornare a zero gradi e rifare il ciclo?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • realpastaman
    realpastaman over 5 years ago

    Hi Thanks so much for the homing code using 28byj-48-with-accelstepper-library. I have it  working really well using a Hall effect switch.

     

    After homing the stepper motor I wanted to do an analogue read from the arduino analogue input pin (using a potentiometer) and move the stepper motor proportionally in a continous loop. I have searched the internet for this but cannot see how to do this,

     

    I wonder if you would be so kind as to show me how your code could be modified to do this.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 10 years ago

    Nice post.

     

    Not all stepper motors work the same way, but you were lucky to find a good starting point and adapt the software to make it work.

     

    DAB

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube