Arduino with nema 17 stepper motor and potentiometer


I’m using an Arduino Uno Rev.3. with a nema 17 Stepper motor with this motor shield
L293D Motor Drive Shield Expansion Board For Arduino

I want to control the direction and the speed with a potentiometer.

Here’s the code that I’m using, the problem is that this doesn’t set the speed only the position. Can anyone help me with this? I am happy to pay for your time with paypal - thanks

#include <AFMotor.h>
AF_Stepper motor1(200, 2);
int analogPin = 0;     // potentiometer wiper (middle terminal) 
//connected to analog pin 3 
// outside leads to ground and +5V 
int val = 0;           // variable to store the value read 
int prepos = 0; 
int pos = 0; //0 to 224 .
void setup() 
  motor1.setSpeed(60);  // 30 rpm   

void loop() { 
  prepos = pos; 
  val = analogRead(analogPin);    // read the input pin 
  pos = map (val, 0, 4096, 0, 200); 
  if (pos != prepos){ 
    int diststep = pos - prepos; 
    if (diststep < 0) { 
    motor1.step(-diststep, BACKWARD, INTERLEAVE); 
    if (diststep > 0) { 
    motor1.step(diststep, FORWARD, INTERLEAVE); 

If it is the adafruit shield you’re using, you have to include WIRE in the beginning of your code, for it runs in I2C.

#include <Wire.h>

Did you try the examples and did they work?

The Thread stepper motor basics may be of interest.


  motor1.setSpeed(60);  // 30 rpm

Say what?

By the way, do you have any idea what NEMA 17 defines? Why do we care where the mounting holes are?