Stepper follows a knob (pins 2 to 5) on an Arduino Uno
A 28BYJ-48 on its ULN2003 board with IN1 to IN4 on pins 2 to 5 follows a potentiometer on A0: turn the knob and the shaft turns to match. The whole build, an Arduino Uno and 2 more parts, 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.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Positioning a stepper by counting steps
- Turning a knob reading into a target position
- Ignoring tiny changes to stop jitter
- Using the Stepper library
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
A knob on A0 sets where a stepper's shaft should point. The 28BYJ-48 is driven through its ULN2003 board from pins 2 to 5. One full turn of the shaft is spread across the knob's travel.
The sketch always remembers where the shaft is, in a variable called at. It reads the knob, works out the target step, and steps the difference.
Small changes of 8 steps or less are ignored, so the motor does not jitter. When it does move, it prints the new shaft position.
Parts list
4 parts, plus the jumper wires. They retail for about $34; 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.
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit, MicroPython Pico Kit.
Kits that can build it
These 7 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 5 |
| Stepper motor, 28BYJ-48 with ULN2003 pin IN3 | Arduino Uno R3 pin 4 |
| Stepper motor, 28BYJ-48 with ULN2003 pin IN2 | Arduino Uno R3 pin 3 |
| Stepper motor, 28BYJ-48 with ULN2003 pin IN1 | Arduino Uno R3 pin 2 |
| Stepper motor, 28BYJ-48 with ULN2003 pin VCC | Arduino Uno R3 pin 5V |
| Potentiometer pin 1 | Arduino Uno R3 pin 5V |
| Stepper motor, 28BYJ-48 with ULN2003 pin GND | Arduino Uno R3 pin GND |
| Potentiometer pin 3 | Arduino Uno R3 pin GND |
| Potentiometer pin 2 | Arduino Uno R3 pin A0 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A stepper that follows a knob: the 28BYJ-48 on pins 2 to 5, the knob on A0.
//
// ULN2003 IN1 -> pin 2 IN2 -> pin 3 IN3 -> pin 4 IN4 -> pin 5
// ULN2003 + -> 5V - -> GND
// potentiometer: ends to 5V and GND, middle -> A0
//
// A stepper turns one step each time its coils are switched in order, so the
// sketch always knows where the shaft is. It reads the knob, works out where
// the shaft should be (one full turn across the knob's travel), and steps the
// difference. The Stepper library takes the pins as IN1, IN3, IN2, IN4.
#include <Stepper.h>
const int STEPS_PER_TURN = 2048;
const int KNOB = A0;
Stepper motor(STEPS_PER_TURN, 2, 4, 3, 5);
long at = 0;
void setup() {
motor.setSpeed(12);
Serial.begin(9600);
Serial.println("Mokxi Uno: stepper on pins 2 to 5 follows the knob on A0");
}
void loop() {
long target = (long)analogRead(KNOB) * STEPS_PER_TURN / 1024;
long move = target - at;
if (move > 8 || move < -8) {
motor.step(move);
at = target;
Serial.print("shaft at step ");
Serial.println(at);
}
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
It sets the stepper speed to 12 revolutions per minute with motor.setSpeed. It also starts the serial port at 9600 and prints a startup line.
void setup() {
motor.setSpeed(12);
Serial.begin(9600);
Serial.println("Mokxi Uno: stepper on pins 2 to 5 follows the knob on A0");
}loop sketch.ino
It reads the knob and scales it to a target step from 0 to 2048. It subtracts the current position at to get the move. If the move is bigger than 8 steps either way, it steps the motor, updates at and prints the position. Then it waits 20 ms.
void loop() {
long target = (long)analogRead(KNOB) * STEPS_PER_TURN / 1024;
long move = target - at;
if (move > 8 || move < -8) {
motor.step(move);
at = target;
Serial.print("shaft at step ");
Serial.println(at);
}
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The motor hums but does not turn. This project
The library wants the pins as IN1, IN3, IN2, IN4. That is why the constructor says 2, 4, 3, 5. - The shaft does not follow the knob. This project
Check the knob's middle pin goes to A0 and its outer pins go to 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. - The reading jumps around or only covers part of the range. Potentiometer
Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range. - Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise. - 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.