Control a Stepper motor using an Arduino and a Potentiometer

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

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.



SCHEMATIC

Control a Stepper motor with a Potentiometer and an Arduino

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

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

Leave a comment

Comments have to be approved before showing up