Knob stepper on an Arduino Uno
A rotary encoder on pins 2, 3 and 4 steers a 28BYJ-48 click for click; press the knob and the shaft goes home. 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 30 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
- Reading a rotary encoder as quarter steps with a lookup table
- Using interrupts so no turn is missed while the motor moves
- Driving a 28BYJ-48 stepper through a ULN2003 board with the Stepper library
- Turning clicks into motor steps with a ratio
- Difficulty: Intermediate
- Time: About 30 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
A KY-040 rotary encoder on pins 2, 3 and 4 steers a 28BYJ-48 stepper on pins 8 to 11. The encoder has 20 clicks a turn and the motor needs 2048 steps for a full turn. So each click moves the shaft about 102 steps, and one turn of the knob is one turn of the shaft.
Pins 2 and 3 are interrupt pins, so every change on them is read at once, even while the motor is busy stepping. Four quarter steps make one click. Pressing the knob on pin 4 sets the click count to zero and sends the shaft home.
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 × Rotary encoder, KY-040In MIDI Controller Kit.
Kits that can build it
This starter kit has 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 |
| Rotary encoder, KY-040 pin SW | Arduino Uno R3 pin 4 |
| Rotary encoder, KY-040 pin DT | Arduino Uno R3 pin 3 |
| Rotary encoder, KY-040 pin CLK | Arduino Uno R3 pin 2 |
| Stepper motor, 28BYJ-48 with ULN2003 pin VCC | Arduino Uno R3 pin 5V |
| Rotary encoder, KY-040 pin VCC | Arduino Uno R3 pin 5V |
| Stepper motor, 28BYJ-48 with ULN2003 pin GND | Arduino Uno R3 pin GND |
| Rotary encoder, KY-040 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.
// Knob stepper: a rotary encoder steers a stepper motor, click for click.
//
// encoder CLK -> pin 2, DT -> pin 3, SW -> pin 4, + -> 5V, GND -> GND
// ULN2003 IN1 -> pin 8, IN2 -> pin 9, IN3 -> pin 10, IN4 -> pin 11
// + -> 5V, - -> GND
//
// The encoder has 20 clicks a turn and the 28BYJ-48 takes 2048 steps a turn,
// so each click moves the shaft about 102 steps, 18 degrees: turn the knob
// once and the shaft turns once. Press the knob to send the shaft home.
//
// # Reading the encoder
//
// CLK and DT are two switches that open and close a quarter of a click apart.
// Every change is looked up in a table of all sixteen moves: a real quarter
// step counts +1 or -1, and a jump of two states, which is what a bouncing
// contact looks like, counts nothing. Four quarter steps make a click.
//
// Pins 2 and 3 are the Uno's two interrupt pins, so the reading happens in an
// interrupt on every change. That matters here: while the motor is stepping,
// loop() is busy for a fifth of a second a click, and a knob turned in that
// time would be missed if loop() had to notice it.
#include <Stepper.h>
const int CLK = 2;
const int DT = 3;
const int SW = 4;
const int STEPS_PER_TURN = 2048;
const int CLICKS_PER_TURN = 20;
Stepper motor(STEPS_PER_TURN, 8, 10, 9, 11);
const int8_t QUARTER[16] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
volatile uint8_t state = 3;
volatile int8_t quarters = 0;
volatile long clicks = 0;
long position = 0; // where the shaft is, in steps from home
long shownClicks = 0;
bool lastSw = HIGH;
uint8_t pair() {
return (uint8_t)((digitalRead(CLK) << 1) | digitalRead(DT));
}
void onTurn() {
uint8_t now = pair();
if (now == state) return;
quarters += QUARTER[(state << 2) | now];
state = now;
if (now == 3) {
if (quarters >= 4) clicks++;
if (quarters <= -4) clicks--;
quarters = 0;
}
}
void moveTo(long target) {
if (target != position) {
motor.step((int)(target - position));
position = target;
}
for (int pin = 8; pin <= 11; pin++) digitalWrite(pin, LOW);
}
void setup() {
pinMode(CLK, INPUT_PULLUP);
pinMode(DT, INPUT_PULLUP);
pinMode(SW, INPUT_PULLUP);
motor.setSpeed(15);
Serial.begin(9600);
Serial.println("Mokxi Uno: knob stepper");
state = pair();
attachInterrupt(digitalPinToInterrupt(CLK), onTurn, CHANGE);
attachInterrupt(digitalPinToInterrupt(DT), onTurn, CHANGE);
}
void loop() {
noInterrupts();
long now = clicks;
interrupts();
if (now != shownClicks) {
shownClicks = now;
Serial.print("click ");
Serial.println(now);
moveTo(now * STEPS_PER_TURN / CLICKS_PER_TURN);
}
bool sw = digitalRead(SW);
if (sw == LOW && lastSw == HIGH) {
noInterrupts();
clicks = 0;
interrupts();
shownClicks = 0;
Serial.println("home");
moveTo(0);
}
lastSw = sw;
}
Code walkthrough
The key functions of the sketch, and what each one does.
pair sketch.ino
Reads CLK and DT and packs them into a two-bit number from 0 to 3. That number is the encoder's current state, and the sketch compares it with the previous one to see which way it moved.
uint8_t pair() {
return (uint8_t)((digitalRead(CLK) << 1) | digitalRead(DT));
}onTurn sketch.ino
Runs on every change of CLK or DT. It looks up the old and new state in QUARTER: a real quarter step gives +1 or -1, and a two-state jump, which is what bounce looks like, gives 0. When the pins come back to rest at state 3, four steps in either direction count one click.
void onTurn() {
uint8_t now = pair();
if (now == state) return;
quarters += QUARTER[(state << 2) | now];
state = now;
if (now == 3) {
if (quarters >= 4) clicks++;
if (quarters <= -4) clicks--;
quarters = 0;
}
}moveTo sketch.ino
Takes a target in motor steps. If it differs from position, it steps the motor by the difference and stores the new position. Afterward it sets pins 8 to 11 low, so the coils stop drawing current while the motor rests.
void moveTo(long target) {
if (target != position) {
motor.step((int)(target - position));
position = target;
}
for (int pin = 8; pin <= 11; pin++) digitalWrite(pin, LOW);
}loop sketch.ino
Copies clicks with interrupts off, so a half-written value is never read. When it changes, it prints it and calls moveTo with clicks times 2048 divided by 20. A fresh press of the knob zeroes the count and moves to 0.
void loop() {
noInterrupts();
long now = clicks;
interrupts();
if (now != shownClicks) {
shownClicks = now;
Serial.print("click ");
Serial.println(now);
moveTo(now * STEPS_PER_TURN / CLICKS_PER_TURN);
}
bool sw = digitalRead(SW);
if (sw == LOW && lastSw == HIGH) {
noInterrupts();
clicks = 0;
interrupts();
shownClicks = 0;
Serial.println("home");
moveTo(0);
}
lastSw = sw;
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The shaft jiggles or turns the wrong way This project
Check the order of IN1 to IN4 on pins 8 to 11. TheStepperis set up as 8, 10, 9, 11, so keep the wires in order. - Clicks count twice or in the wrong direction This project
Check CLK is on pin 2 and DT is on pin 3. Swapping them reverses the direction. - 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. - Turning the knob counts twice, skips, or goes the wrong way. The contacts bounce and polling is too slow. Rotary encoder
Use interrupts or a proper encoder library on pins CLK and DT. Use INPUT_PULLUP, since the contacts are open and shut to ground. Swap CLK and DT to flip direction. - The KY-040 pins read wrong or noisy. The board has pull-ups to its + pin, so + must be powered, and its switch needs debounce. Rotary encoder
Connect + to 5 V or 3.3 V to match your board's logic level and GND to ground. Debounce SW for about 20 ms. One click often equals four state changes. - 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.