Go Down

Topic: A simple sketch to drive Stepper Motor 28BYJ-48 (Read 398 times) previous topic - next topic


Jun 25, 2015, 10:13 am Last Edit: Jun 25, 2015, 10:31 am by ma_hty
I wrote a simple stepper sketch (with video) for my nephew.  Got nowhere to put the source code.  By the way, would the board still be fine if I remove the power LED physically?  With power saving mode, the power consumption of the power LED became important.

The example: two buttons control the stepper to rotate different directions; no user inputs for 10 seconds, put the arduino into power saving mode; it assumes an arduino nano and a stepper 28BYJ-48 with a ULN2003AN control board.

The video: https://www.youtube.com/watch?v=JlRs_FlgRxQ

Code: [Select]

#include <avr/sleep.h>
#include <Stepper.h>

Stepper myStepper(4096, 8,10,9,11); 
// The pins ordering of 28BYJ-48 is differ from the Stepper class expecting,
// which need to be hacked to {8,10,9,11} instead of {8,9,10,11}.
// Check http://forum.arduino.cc/index.php?topic=143276.0 reply #6 for details.

const int left_pin  = 2;
const int right_pin = 3;
unsigned long time_to_sleep = 10000;  // if idle for 10 seconds, put arduino to sleep mode
unsigned long time_begin;  // timer for sleep
int interval = 96;  // initial delay for stepper motor rotation

void setup()
  pinMode(left_pin, INPUT_PULLUP);
  pinMode(right_pin, INPUT_PULLUP);
  time_begin = millis();

  myStepper.setSpeed( 0xEFFFFFFFL );  // Just to set the step_delay value to 0, to bypass the delay routine in the Stepper class.

void sleepNow()
    // myStepper.off();
    digitalWrite( 8, LOW);
    digitalWrite( 9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);

    set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
    attachInterrupt(0, pinInterrupt, LOW);
    attachInterrupt(1, pinInterrupt, LOW);
    sleep_mode();   // here the device is actually put to sleep!!
    sleep_disable();  // disable sleep after waking from sleep, sketch contines from here.
    time_begin = millis();

void pinInterrupt()

void loop()
  int pos = 0;
  if( digitalRead(left_pin)==LOW )
  if( digitalRead(right_pin)==LOW )
  if( pos )
    interval /= 1.5;
    if( interval<2 )
    interval = 2;
    time_begin = millis();
    interval = 96;
    if( millis()-time_begin > time_to_sleep )


Hi there,

I'm aware it's been a while since this thread was active but I find this really impressive. I've used it to help me create a remote controlled robot arm.

Well done, this is beautifully creative.



Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131