How to Control Speed and Direction of a Stepper Motor using a Potentiometer and Arduino.

Use a Potentiometer to Control Stepper Motor Speed & Direction

Want precise control over your stepper motor’s speed or direction? It’s as easy as turning a knob, or potentiometer! In this tutorial, we’re pairing it with an Arduino to give you hands-on, real-time stepper motor control—no complicated code, no guesswork, just smooth and effortless movement. Whether you’re building a robotic arm, a camera slider, or a motorized spooky prop (because why not?), this guide will walk you through wiring, coding, and fine-tuning your stepper so it spins exactly like you need. Grab your gear, and let’s get that motor spinning!

If this is your first time working with a stepper motor, I highly recommend checking out my comprehensive introduction to stepper motors first. It covers everything from wiring to code basics, so you’ll hit the ground running. Here, we’ll be focusing on adding a potentiometer to the circuit for fine-tuned control.

Recommended Reading:

Control Stepper Motors with Arduino: Absolute Beginner’s Guide

How to Wire a Stepper Motor and Potentiometer to an Arduino

Before we dive into the code, let’s get everything wired up! Connecting a stepper motor, driver, and potentiometer to your Arduino might sound tricky, but don’t worry—I’ll walk you through it step by step. Grab your jumper wires, and let’s bring this setup to life!

Breadboard wiring diagram showing how to wire a 28BYJ-48 stepper motor, ULN2003 motor driver and potentiometer to an Arduino Uno.

Step 1: Connect the Stepper Motor to the Motor Driver

Take the 5-pin plastic connector from the 28BYJ-48 stepper motor and plug it into the “Motor” header on the ULN2003 driver board. It’s foolproof—it only fits one way!

And just like that, your stepper motor is connected. Easy, right?

Step 2: Connect the ULN2003 to the Arduino

Now that the stepper motor is hooked up, let’s connect the ULN2003 driver to the Arduino. These connections are how the motor driver will receive speed and direction instructions from the Arduino and pass them along to the stepper motor.

ULN2003 MOTOR DRIVERARDUINO UNO
IN18
IN29
IN310
IN411

Step 3: Power the ULN2003 Motor Driver

Your 28BYJ-48 stepper motor needs power, and you’ve got two options: batteries or a wall adapter.

Unless portability is a must, a 5V 2A wall adapter is the way to go. Stepper motors draw power even when idle, so batteries drain fast.

To keep things breadboard-friendly, I use a female jack adapter with jumper wires to connect the power supply to the ULN2003 driver via the breadboard power rails.

ULN2003 MOTOR DRIVER5V POWER SUPPLY
Positive (+)Positive rail on breadboard
Negative (-)Negative rail on breadboard

Don’t Forget the Common Ground!

Run a ground wire from the jack adapter’s negative terminal to the Arduino’s GND pin. You can also use a breadboard to link all ground connections as shown on the wiring diagram. No common ground = no movement.

Step 4: Connect the Potentiometer to the Arduino

Now, let’s hook up the potentiometer to the Arduino so we can smoothly control the stepper motor’s speed and direction with a simple turn of the dial!

POTENTIOMETERARDUINO UNO
Signal (center pin)A0
VCC (any of the outer pins)5V
GND (remaining outer pin)GND

And that’s it—your wiring is complete! With the stepper motor, ULN2003 driver, and potentiometer all connected, you’re ready to get this circuit spinning. Double-check your connections, especially the common ground, and get ready to code.

Arduino Code Example 1: Control Stepper Motor Direction with a Potentiometer

Now, let’s load up an Arduino sketch that uses the potentiometer to change the stepper motor’s direction—turn the dial left or right, and watch it spin accordingly!

#include <Stepper.h>

// potentiometer pin connection
int potPin = A0;

// # steps for full 360-degree rotation
// change to match your motor specs
int stepsPerRevolution = 2048;

// set a speed for the stepper motor
int rpm = 10;

// initialize stepper library on pins 8 - 11
// pin order IN1, IN3, IN2, IN4
Stepper myStepper (stepsPerRevolution, 8, 10, 9, 11);

int previousVal = 0; // variable to store previous pot value
int currentVal; // variable to store current pot value

void setup() {
  myStepper.setSpeed(rpm);
}

void loop() {
  // get potentiometer value
  currentVal = analogRead(potPin);

  // move # of steps equal to change in pot reading
  myStepper.step(currentVal - previousVal);

  // transfer the current pot value to previous value
  previousVal = currentVal;
}

How It Works

  • Turn the potentiometer → The stepper motor moves in that direction.
  • The farther you turn the potentiometer, the more steps the motor takes.
  • The motor’s speed isn’t controlled directly, just the number of steps it moves per loop cycle.

Code Explanation

Include the Stepper Motor Library

#include <Stepper.h>

This imports the Stepper library, which makes controlling stepper motors easier by handling step sequencing.

Define the Potentiometer Pin

int potPin = A0;

The potentiometer is connected to analog pin A0 on the Arduino. This will let us read voltage changes as the knob turns.

Define the Stepper Motor Settings

int stepsPerRevolution = 2048;

This sets the total number of steps for one full 360-degree rotation of the 28BYJ-48 stepper motor. This motor has 2048 steps per revolution in full-step mode.

int rpm = 10;

This defines the speed of the stepper motor in revolutions per minute (RPM). The motor will move at 10 RPM when running.

Initialize the Stepper Motor

Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

This creates a Stepper motor object called myStepper, assigning it to Arduino pins 8, 10, 9, and 11, which are connected to the ULN2003 driver module.

Pin Order Matters! The order (IN1, IN3, IN2, IN4) follows the correct sequence for this motor.

Variables for Potentiometer Readings

int previousVal = 0;
int currentVal;

  • previousVal keeps track of the last potentiometer reading.
  • currentVal will store the latest reading from the potentiometer.

Setup Function (Runs Once at Startup)

void setup() {
  myStepper.setSpeed(rpm);
}

  • myStepper.setSpeed(rpm); → Sets the stepper motor speed to 10 RPM.

Main Loop (Runs Continuously)

void loop() {
  // get potentiometer value
  currentVal = analogRead(potPin);

  • analogRead(potPin) reads the current voltage (ADC value) from the potentiometer (0-1023) and stores it in currentVal.

myStepper.step(currentVal - previousVal);

  • This moves the stepper motor by a number of steps equal to the change in the potentiometer reading.
  • If the potentiometer is turned right, the step count increases → the motor rotates clockwise.
  • If the potentiometer is turned left, the step count decreases → the motor rotates counterclockwise.

previousVal = currentVal;

  • Updates previousVal to store the latest potentiometer reading.
  • This ensures the next loop iteration calculates movement based on the change in potentiometer value, not the absolute value.

This is a simple but powerful way to control stepper motor direction using a potentiometer!

Arduino Code Example 2: Control Stepper Motor Speed with a Potentiometer

Now, let’s load up an Arduino sketch that uses the potentiometer to control the stepper motor’s speed—turn the dial to speed it up or slow it down smoothly!

#include <Stepper.h>

// potentiometer pin connection
int potPin = A0;

int potVal; // variable to store potentiometer value

// # steps for full 360-degree rotation
// change to match your motor specs
int stepsPerRevolution = 2048;

// initialize stepper library on pins 8 - 11
// pin order IN1, IN3, IN2, IN4
Stepper myStepper (stepsPerRevolution, 8, 10, 9, 11);

void setup() {
}

void loop() {
  // get potentiometer value
  potVal = analogRead(potPin);

  // map pot value to stepper motor speed
  int stepSpeed = map(potVal, 0 , 1023, 0, 17);

  // if the step speed is greater than 0
  if (stepSpeed > 0) {
    // set the stepper motor speed according to pot position
    myStepper.setSpeed(stepSpeed);
    // step 1/100 of a revolution at a time
    myStepper.step(stepsPerRevolution / 100);
  }
}

How It Works

  • Turn the potentiometer right → The motor speeds up.
  • Turn the potentiometer left → The motor slows down.
  • If the potentiometer is at 0, the motor stops.

Code Explanation

Many of the code lines will look familiar from the previous example but I’ll include explanations for them here too in case you jumped to this example first.

Alright, from the top!

Include the Stepper Motor Library

#include <Stepper.h>

This imports the Stepper library, which simplifies controlling stepper motors.

Define the Potentiometer Pin

int potPin = A0;

The potentiometer is connected to analog pin A0 on the Arduino, allowing us to read voltage changes as the knob turns.

int potVal;

This variable will store the current reading from the potentiometer.

Define the Stepper Motor Settings

int stepsPerRevolution = 2048;

This sets the total number of steps required for the 28BYJ-48 stepper motor to complete one full 360-degree rotation in full-step mode.

Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

This creates a Stepper motor object named myStepper, assigning it to Arduino pins 8, 10, 9, and 11, which are connected to the ULN2003 motor driver.

Pin Order Matters! The order (IN1, IN3, IN2, IN4) ensures the correct step sequence.

Setup Function (Runs Once at Startup)

void setup() {
}

  • The setup() function is empty because there’s no need to initialize anything before the loop starts.
  • Stepper motor speed is set dynamically in loop(), based on the potentiometer’s position.

Main Loop (Runs Continuously)

void loop() {
  potVal = analogRead(potPin);

  • Reads the current voltage from the potentiometer (0–1023) and stores it in potVal.

int stepSpeed = map(potVal, 0 , 1023, 0, 17);

  • The map() function scales the potentiometer reading (0 to 1023) to a stepper motor speed range of 0 to 17 RPM.
  • This means turning the potentiometer fully left = 0 RPM, fully right = 17 RPM.

if (stepSpeed > 0) {

  • Prevents the motor from moving if the speed is 0 (potentiometer turned all the way down).
  • In order to move, the potentiometer value must be greater than 0.

myStepper.setSpeed(stepSpeed);

  • Updates the stepper motor speed based on the potentiometer’s position.
  • The motor will speed up or slow down smoothly as you turn the dial.

myStepper.step(stepsPerRevolution / 100);
  }
}

  • Moves the stepper 1/100th of a full revolution at a time.
  • The smaller step size ensures smoother motion instead of big jumps.
  • If the potentiometer is at 0 RPM, the motor doesn’t move.

Dial up the speed! This is another clever and practical way to control your stepper motor using a potentiometer.

What’s Next? Spin It Your Way

And there you have it—smooth, dial-controlled stepper motor action! With just a potentiometer and a few lines of code, you’ve unlocked real-time speed and direction control. Whether you’re fine-tuning a camera slider, building a spooky animatronic, or just making a motor spin for fun (no judgment here), this setup has you covered. Now go forth and make something awesome—just don’t get too dizzy watching that motor go!