Controlling speed of a stepper with a pot

Hi everybody :slight_smile:

I’ve been trying by myself a lot to get this working, but i need some help.

I want to control the speed of a stepper motor (200 steps/rev) with a potentiometer, and i’m using an Adafruit Motorshield V2. I tried first with the adafruit motorshield library. This code :

This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM

For use with the Adafruit Motor Shield v2 

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 1);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  myMotor->setSpeed(10);  // 10 rpm (peut aller jusqu a 80?)  

void loop() {
  // lis la valeur en A0 et l enregistre dans sensorValue
  int sensorValue = analogRead(A0);
  // // div. par 1023 pour avoir une valeur entre 0 et 1 et mult. par 50 pour avoir
  //  entre 0 et 50 rpm. Enregistre dans vitesse.
  float vitesse = sensorValue * (199.0 / 1023.0) + 1; // 199 being the speed in rpm - 1
  myMotor->step(1, FORWARD, SINGLE); 

. But i couldn’t get it to work properly, the rpm speed in reality isn’t the one written in the code. If i write 49 or 199 rpm, it is the same.

I’ve read about accelstepper which is more isf you want the steppers to rotate in the background, and it looks really harder to use it with the motorshield, as there is only 2 examples from adafruit that quickly explain show what to do.

I’ve found this code ( and i just want the part with one stepper and the speed control.

To help, one of the things i want to do with this setup is to replace the motor on a turntable with a belt by a stepper, to be able to slow it down to 0 rpm and accelerate to let’s say 100rpm.

Here is the adaptation i tried to do and it’s not even compiling.

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60);
Adafruit_StepperMotor *motor = AFMS.getStepper(200, 1);
/*AccelStepper motor(1,6,5); */    //stepper 1

#define ANALOG_IN A0           //slide pot for speed

int sped = analogRead(ANALOG_IN);
const int numReadings = 100;    //100 samples for average
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average
int inputPin = A0;              //slide pot for position

void setup() {

void loop() {
  int sped = analogRead(ANALOG_IN);
  int val = map(sped, 0, 1023, 350, 600);

If someone can help me, i’m stuck :frowning: and my goal is so simple it’s embarassing to not be able to do that.

Thank you,



I suspect your problem is here:

 myMotor->setSpeed(10);  // 10 rpm (peut aller jusqu a 80?)

I had a similar problem with the Adafruit motor shield V1. After setting the motor speed to the maximum (255) it all worked correctly.