Control a Stepper motor using an Arduino and Potentiometer

//Control a Stepper motor using an Arduino and Potentiometer
TUTORIAL


Use a Potentiometer to control a Stepper Motor

OVERVIEW

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

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.

THE CODE

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 / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include "AccelStepper.h" 
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/

// 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
  }
}
TUTORIAL VIDEO
DOWNLOAD

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

(AccelStepper Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/)

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

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

5 Comments

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

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

  2. 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.

  3. 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.

  4. 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