# Stepper motor example does not work?

**URL:** <https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [January 24, 2020, 9:51am UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763 "2020-01-24T09:51:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon13464921](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@anon13464921](https://forum.arduino.cc/u/anon13464921)\
**Post date:** [January 24, 2020, 9:51am UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/1 "2020-01-24T09:51:08Z")

</div>

Since I have two small stepper motors (28BYJ-48) with ULN2003 modules and wanted to find out how they work, I loaded the example "Stepper\_OneRevolution" into an UNO, after adjusting the steps to 64, which is according to the datasheet of the motor the number of steps per revolution.  
I hooked up UNO pin 8-11 to pin 1-4 of the module, attached an external 5V power supply as well, (grounds connected) and got the motor to turn.  
However, although the example states that my motor should turn forward and then backward, it did just go forward and kept going forward. I decided to switch wires backward (UNO pin 8-11 to 4-1 of the module) but that did not change anything.  
And both motors and ULN2003 modules behaved the same.  
The stepper library is 1.1.3

Anybody who has a hint as to what I do wrong???

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [January 24, 2020, 10:08am UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/2 "2020-01-24T10:08:23Z")

</div>

post the code inside the code tags (\</\> icon)

---

<div class="post-metadata">

**Author:** ![anon13464921](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@anon13464921](https://forum.arduino.cc/u/anon13464921)\
**Post date:** [January 24, 2020, 11:01am UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/3 "2020-01-24T11:01:15Z")

</div>

Hi, it's the standard example in the IDE, here you go and thnks for helping!

```cpp
/*
 Stepper Motor Control - one revolution

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.

 The motor should revolve one revolution in one direction, then
 one revolution in the other direction.

 Created 11 Mar. 2007
 Modified 30 Nov. 2009
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 64;  // 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(60);
  // initialize the serial port:
  Serial.begin(9600);
}

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

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

```

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [January 24, 2020, 12:36pm UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/4 "2020-01-24T12:36:01Z")

</div>

The 28BYJ-48 motor comes with a gearbox so you need about 2048 steps IIRC to get the output shaft to make one revolution.

...R

---

<div class="post-metadata">

**Author:** ![anon13464921](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@anon13464921](https://forum.arduino.cc/u/anon13464921)\
**Post date:** [January 24, 2020, 1:40pm UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/5 "2020-01-24T13:40:25Z")

</div>

OK Robin,  
Thanks, will try that out, but it still is no explanation for it to only turn clockwise, right?  
Any explanation for that?

---

<div class="post-metadata">

**Author:** ![anon13464921](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@anon13464921](https://forum.arduino.cc/u/anon13464921)\
**Post date:** [January 24, 2020, 2:08pm UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/6 "2020-01-24T14:08:17Z")

</div>

Result: 2048 steps means the shaft does not move at all, or at least not visible (understandibly so)  
It seems 64 is also not correct, because the shaft moves no more than roughly 20 degrees.  
But more importantly, why does it not run counterclockwise???

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [January 24, 2020, 3:00pm UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/7 "2020-01-24T15:00:28Z")

</div>

Try a much lower speed for initial tests.

If that does not help my guess is that your connections are wrong.

If this was my problem I would have tried lots of variations in the number of steps to see what the effect is. The Arduino system is great for learning by doing.

I have a 28BYJ motor and I don't recall having any problems getting it to work.

...R

---

<div class="post-metadata">

**Author:** ![anon13464921](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@anon13464921](https://forum.arduino.cc/u/anon13464921)\
**Post date:** [January 24, 2020, 3:23pm UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/8 "2020-01-24T15:23:50Z")

</div>

2048 turns out to be correct for a complete rotation, but only works with much reduced speed : 10 instead of 60 in the example.  
Still does not turn counterclockwise, although it seems to start to run in the other direction, it hesitates, then goes on clockwise

---

<div class="post-metadata">

**Author:** ![anon13464921](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@anon13464921](https://forum.arduino.cc/u/anon13464921)\
**Post date:** [January 24, 2020, 3:48pm UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/9 "2020-01-24T15:48:41Z")

</div>

Robin,  
You're absolutely right of course and I had tried various pin positions already. One I had not tried till now was moving connections up one pin. So the connection of pin 8 goes to 11, 9 to 8, 10 to 9, 11 to 10.  
Result: shaft turns counterclockwise ! ................and does not reverse, but keeps going counterclockwise.

Some more experimenting: Finally: uln2003 pin 1 to UNO pin 8, 2 to 10, 3 to 9, 4 to 11.

BINGO!!

Thanks for helping Robin, I was just a bit too worried to damage things!

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [January 24, 2020, 5:18pm UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/10 "2020-01-24T17:18:57Z")

</div>

> JB47:  
> but only works with much reduced speed : 10 instead of 60 in the example.

Just the same as any other motor a stepper motor needs to be accelerated to higher speeds. Maybe have a look at the [AccelStepper](http://www.airspayce.com/mikem/arduino/AccelStepper/) library

...R

---

<div class="post-metadata">

**Author:** ![MarkT](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MarkT](https://forum.arduino.cc/u/MarkT)\
**Post date:** [January 26, 2020, 3:07pm UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/11 "2020-01-26T15:07:06Z")

</div>

> JB47:  
> Robin,  
> You're absolutely right of course and I had tried various pin positions already. One I had not tried till now was moving connections up one pin. So the connection of pin 8 goes to 11, 9 to 8, 10 to 9, 11 to 10.  
> Result: shaft turns counterclockwise ! ................and does not reverse, but keeps going counterclockwise.
> 
> Some more experimenting: Finally: uln2003 pin 1 to UNO pin 8, 2 to 10, 3 to 9, 4 to 11.
> 
> BINGO!!

There are 24 ways to connect 4 pins, and only 8 of them will work. 4 of those are cyclically equivalent and will turn it one way (for a given drive sequence), the other 4 turn it the other way.

With a 5-wire motor like this there's no obvious way to figure out the wiring except trial and error. 4-wire bipolars and 6 or 8 wire motors can be figured out with a multimeter.

---

<div class="post-metadata">

**Author:** ![Dingo61](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Dingo61](https://forum.arduino.cc/u/Dingo61)\
**Post date:** [August 4, 2020, 10:01am UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/12 "2020-08-04T10:01:34Z")

</div>

Just for reference, i you have bought the Jaycar (XC-4458) or Altronics (Z6330) stepper and driver kits kit, much of the documentation with them is incorrect,so while okto wire the pins in the order shown, the Stepper instance needs the pin order changing to 8, 10, 9, 11 so the motor reverses ok.  
BTW I can only get max of 15 RPM on the output shaft (not 30 as claimed by Jaycar)

see this post by Michael Schoeffler

> **[How to use Arduino as a 28BYJ-48 Stepper Motor Controller with a ULN2004...](https://www.seeedstudio.com/blog/2019/03/04/driving-a-28byj-48-stepper-motor-with-a-uln2003-driver-board-and-arduino/)**
>
> We have summarised a guide on how to use a 28BYJ-48 stepper motor with an Arduino by using a ULN2003 driver board. It's easier than you think!

1. #include \<Stepper.h\>
2. 
3. #define STEPS 2038 // the number of steps in one revolution of your motor (28BYJ-48)
4. 
5. Stepper stepper(STEPS, 8, 10, 9, 11);
6. 
7. void setup() {
8. // nothing to do
9. }
10. 
11. void loop() {
12. stepper.setSpeed(1); // 1 rpm
13. stepper.step(2038); // do 2038 steps -- corresponds to one revolution in one minute
14. delay(1000); // wait for one second
15. stepper.setSpeed(6); // 6 rpm
16. stepper.step(-2038); // do 2038 steps in the other direction with faster speed -- corresponds to one revolution in 10 seconds
17. }

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 9:52am UTC](https://forum.arduino.cc/t/stepper-motor-example-does-not-work/632763/13 "2021-05-06T09:52:16Z")

</div>

Closed on May 6, 2021, 9:52am UTC


