Stepper motor on pins 8 to 11, on an Arduino Uno
A 28BYJ-48 on its ULN2003 board with IN1 to IN4 on pins 8 to 11: the Stepper library turns the shaft one full turn one way, then back. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
What you'll learn
- How a stepper moves in exact steps
- Using the ULN2003 driver board
- Steps per turn and speed in revolutions per minute
- Using the Stepper library
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
A stepper motor does not spin when you power it. It moves one small step each time its coils are switched in the right order. The ULN2003 board switches the coils, and the Uno sends it signals on pins 8 to 11.
Through its gearbox the 28BYJ-48 needs 2048 steps for one turn of the shaft. The sketch turns one full turn clockwise, waits 500 ms, turns one full turn back, and waits 500 ms again.
After each move it switches all four coil pins off, so the motor does not stay energized and warm.
Parts list
3 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Stepper motor, 28BYJ-48 with ULN2003In Satellite Tracker Kit, Small Satellite Flight Computer Kit.
Kits that can build it
These 8 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Stepper motor, 28BYJ-48 with ULN2003 pin IN4 | Arduino Uno R3 pin 11 |
| Stepper motor, 28BYJ-48 with ULN2003 pin IN3 | Arduino Uno R3 pin 10 |
| Stepper motor, 28BYJ-48 with ULN2003 pin IN2 | Arduino Uno R3 pin 9 |
| Stepper motor, 28BYJ-48 with ULN2003 pin IN1 | Arduino Uno R3 pin 8 |
| Stepper motor, 28BYJ-48 with ULN2003 pin VCC | Arduino Uno R3 pin 5V |
| Stepper motor, 28BYJ-48 with ULN2003 pin GND | Arduino Uno R3 pin GND |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A 28BYJ-48 stepper on its ULN2003 board: one turn forward, one back.
//
// ULN2003 IN1 -> pin 8 IN2 -> pin 9 IN3 -> pin 10 IN4 -> pin 11
// ULN2003 + -> 5V (on the bench, the breadboard power module)
// ULN2003 - -> GND
//
// A stepper does not spin when you power it. It turns one small step each
// time the coils are switched in the right order, so the code decides exactly
// how far it goes. Through its gearbox the 28BYJ-48 takes 2048 steps for one
// turn of the output shaft.
//
// The Stepper library wants the pins as IN1, IN3, IN2, IN4: that is why the
// constructor says 8, 10, 9, 11. Given 8, 9, 10, 11 the motor hums and does
// not turn.
#include <Stepper.h>
const int STEPS_PER_TURN = 2048;
Stepper motor(STEPS_PER_TURN, 8, 10, 9, 11);
void release() {
for (int pin = 8; pin <= 11; pin++) digitalWrite(pin, LOW);
}
void setup() {
motor.setSpeed(15); // revolutions per minute; this motor manages about 15
Serial.begin(9600);
Serial.println("Mokxi Uno: stepper on pins 8 to 11");
}
void loop() {
Serial.println("one turn clockwise");
motor.step(STEPS_PER_TURN);
release();
delay(500);
Serial.println("one turn back");
motor.step(-STEPS_PER_TURN);
release();
delay(500);
}
Code walkthrough
The key functions of the sketch, and what each one does.
release sketch.ino
It loops through pins 8 to 11 and sets each one low. That switches off all four coils, so the motor stops drawing current while it rests.
void release() {
for (int pin = 8; pin <= 11; pin++) digitalWrite(pin, LOW);
}setup sketch.ino
It sets the speed to 15 revolutions per minute with motor.setSpeed, which is about the most this motor manages. It also starts the serial port at 9600 and prints a startup line.
void setup() {
motor.setSpeed(15); // revolutions per minute; this motor manages about 15
Serial.begin(9600);
Serial.println("Mokxi Uno: stepper on pins 8 to 11");
}loop sketch.ino
It prints a message and steps STEPS_PER_TURN forward, which is one full turn. Then it calls release and waits 500 ms. Next it steps the same amount backward with a negative number, releases and waits again.
void loop() {
Serial.println("one turn clockwise");
motor.step(STEPS_PER_TURN);
release();
delay(500);
Serial.println("one turn back");
motor.step(-STEPS_PER_TURN);
release();
delay(500);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The motor hums or twitches but does not turn. This project
The library needs the pin order IN1, IN3, IN2, IN4. That is why the constructor says 8, 10, 9, 11 and not 8, 9, 10, 11. - The motor does not move at all. This project
Check IN1 to IN4 go to pins 8 to 11, and the board has 5V and GND. - The motor buzzes or shakes and does not turn. The coil pins IN1 to IN4 are in the wrong order for the library. Stepper motor
For Arduino's Stepper library on a 28BYJ-48, create it with pins 8, 10, 9, 11 (IN1, IN3, IN2, IN4). Or use AccelStepper with HALF4WIRE and the same pin order. - It stalls or skips when spun fast or powered from the Arduino 5 V pin. It also draws more than a USB port may give. Stepper motor
Power the ULN2003 board from a separate 5 V supply and join the grounds. Run it slowly, about 10 to 15 RPM, and ramp up speed gradually. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.