Built-in project · Arduino Uno R3

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 Runs in your browser. Free, and no account needed.

Stepper follows a knob (pins 2 to 5) · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// 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);
}

Parts list

4 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 5; Stepper motor, 28BYJ-48 with ULN2003 pin IN4
  • Arduino Uno R3 pin 4; Stepper motor, 28BYJ-48 with ULN2003 pin IN3
  • Arduino Uno R3 pin 3; Stepper motor, 28BYJ-48 with ULN2003 pin IN2
  • Arduino Uno R3 pin 2; Stepper motor, 28BYJ-48 with ULN2003 pin IN1
  • Arduino Uno R3 pin 5V; Stepper motor, 28BYJ-48 with ULN2003 pin VCC; Potentiometer pin 1
  • Ground: Arduino Uno R3 pin GND; Stepper motor, 28BYJ-48 with ULN2003 pin GND; Potentiometer pin 3
  • Arduino Uno R3 pin A0; Potentiometer pin 2

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.