Control a Stepper motor using an Arduino and Potentiometer

//Control a Stepper motor using an Arduino and Potentiometer

Use a Potentiometer to control a Stepper Motor


Sometimes keeping things simple is best!

In this tutorial we will see how to move a stepper using only a regular Potentiometer.

We will use a NANO and the Easy driver to control the NEMA 17 stepper motor.


Connections for this tutorial are as follow:

Pin 2 and 3 of the NANO are connected to the STEP and DIR pin of the Easy Driver

We also connect a Ground pin of the NANO to the Easy Driver GND pin

Pin A4 of the NANO is used to read the Analog value of the Potentiometer Center pin

5V and GND of the NANO are connected to the outer pins of the Potentiometer

We then power the Easy Driver, thus powering the Stepper Motor, using an external 12v power supply.


We will be using the AccelStepper library to control the speed and acceleration of the stepper motor, we want the motor to move in relation to the rotation of the Potentiometer.

Since a Potentiometer can change value even when we are not touching it, we are moving the motor only if the Potentiometer value has changed +6 or -6 from the previous read value, that way we avoid jitter of the Stepper motor.

As always please have a look at the tutorial video for more information.

/* Arduino Stepper Control with Potentiometer

Created by Yvan /
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!


#include "AccelStepper.h" 
// Library created by Mike McCauley at

// AccelStepper Setup
AccelStepper stepper(1, 2, 3);  // 1 = Easy Driver interface
                                  // Nano Pin 2 connected to STEP pin of Easy Driver
                                  // Nano Pin 3 connected to DIR pin of Easy Driver

// Variables to store current, previous and move position
int val = 0;
int previous = 0;
int long newval = 0;    

void setup() {
  stepper.setMaxSpeed(4800);  // Set speed fast enough to follow pot rotation
  stepper.setAcceleration(4800);  //  High Acceleration to follow pot rotation  

void loop() {
  val = analogRead(A4);  //  Read Potentiometer current value
  if ((val > previous+6) || (val < previous-6)) {  // Check that a move of the pot is at least > or < than 6
    newval = map(val, 0, 1023, 0, 1600);  // Map value (1600 = 1 stepper shaft rotation)
    stepper.runToNewPosition(newval);  // Move stepper to new position
    previous = val;  // save current value into variable previous

Copy the above Sketch code in your Arduino IDE software to program your Arduino.

(AccelStepper Library created by Mike McCauley at

Once downloaded, just extract the content of the zip files inside your “arduino libraries” folder.

By | 2018-02-23T17:05:14-05:00 March 11th, 2016|Tutorials|


  1. shashank kulkarni January 2, 2020 at 12:22 am - Reply

    where can I get all apparatus to make this project? please direct share link?

  2. Dogan July 22, 2019 at 6:25 am - Reply

    how can you use simple a4988 motor driver with same library? There is no variable inside that library for ENABLE pin to change

  3. Carlos v B May 16, 2019 at 4:39 pm - Reply

    gracias por tu proyecto me ayudo mucho

  4. roni farias April 22, 2019 at 12:01 pm - Reply

    I copied and pasted the code in the Arduino plus gave error. I researched some forum and saw that I need to rewrite all code. the question is. Has anyone had the same problem? Did someone rewrite the code if they could send me? thankful. [email protected]

  5. maddecha miaseph October 29, 2018 at 6:37 am - Reply

    i want to 6 stepper and recode-playback

  6. Tomas August 25, 2018 at 7:44 am - Reply

    How could I add a ramp to accelerate or decelerate to this sketch. Thank you

  7. Charles May 14, 2018 at 1:57 pm - Reply

    How can I control the motor in speed instead of position ? «i would like to change the speed of the motor with the potentiometer.

    • zxx July 10, 2018 at 3:31 am - Reply

      please i need your help want to make a record and play robotic arm using nema isaw this video just what i need but missing play and record accion how can i do it

  8. joe April 27, 2018 at 8:00 pm - Reply

    Thanks for sharing-it’s a very nice video.
    Would you be willing to write code to make the pot control only speed? Not change direction and not stop.

  9. walid April 1, 2018 at 5:56 pm - Reply

    can i use UNO instead of NANO

    • brainy-bits April 8, 2018 at 10:58 pm - Reply

      You can use the UNO instead if you want with no modification to the connections or the code.

  10. Rohan kumara March 9, 2018 at 4:22 am - Reply

    I ‘m rohan i’m from srilanka….good your tutorial..i like..thank’s…
    I wont to .this tutorial arduino code and cerkit diagrm..than you…

Leave A Comment