Change speed of stepper with potentiometer

Is there a way to change the speed of a stepper motor with a potentiometer with one end of the potentiometer being the stepper motor being off and the other end being its max speed? Thanks.

All you have to do is vary the time between two step pulses.

Do you already have a working setup?
Leo..

Yes. Do you want the code?

What I have is the stepper motor is off unless the potentiometer is turned at least half the way and then the motor is all the way on. I don't know how to change the speed.

#include <Stepper.h>

const int rolePerMinute = 15;

Stepper myStepper(2048, 8, 10, 9, 11);

int potVal = 0;

void setup() {

  Serial.begin(9600);
  myStepper.setSpeed(rolePerMinute);
}

void loop() {

potVal = map(analogRead(A0),0,1024,0,500);

if (potVal < 250) {

  myStepper.setSpeed(rolePerMinute);
  myStepper.step(-30);
}

else {
  
    for (int i = 8; i < 13; i++)
    {
      digitalWrite(i, LOW);
    }
}
Serial.println(potVal); //for debugging

}

Could you put that in the code and post it, please? I'm not that experienced and don't want to mess it up.

const int rolePerMinute = 15; // the variable is constant, and can't be changed

int rolePerMinute = 15; // varable can be changed

Leo..

Okay, so now how do I make it so the potentiometer changes that variable? It would be helpful if you could make the change and post the complete code with the changes. Thank you, guys, for all your help.

how do I make it so the potentiometer changes that variable?

Reread post #6.

What does rolePerMinute do?

Didn't you write the code?
Then you should have included a link to where you got it.
The writer probably meant RPM (revolutions per minute).
It's just a variable that holds some speed number.
Change the word if you don't like it.

We don't usually write code here. We just help you on your way.
Look at this snippet.

void loop() {
  int rawValue = analogRead(A0);
  int RPM = map(rawValue, 0, 1024, 0, 501); // speed 1-500
  if (RPM > 0) myStepper.setSpeed(RPM);
  else {
    // do all the things here to stop the motor
  }
}

Leo..

I took some code and changed it for my purposes. I didn't know I needed to give credit still. Sorry. Thanks for the snippet. I used up all my uploads for today so I'll try it sometime next week.

Okay, sorry, and thanks for all your help so far.

As with gazillions of these projects, someone has already done it and it's simply a matter of a few quick Google searches.
Such as.............. (first one up)
https://www.youtube.com/watch?v=nLkT_SrFung

Thanks, but I'm trying to control the speed with the potentiometer, not make the motor move by turning it. That's what Google came up with for me, too, but it's not what I'm looking for.

Here's what I came up with using that, but the stepper motor doesn't do anything anymore. Don't know what the problem is.

#include <Stepper.h>

int rotationsPerMinute = 0;

Stepper myStepper(2048, 8, 10, 9, 11);

int potVal = 0;

void setup() {

  Serial.begin(9600);
}

void loop() {
  rotationsPerMinute = map(analogRead(A0), 0, 1024, 0, 500); // speed 1-499
  if (rotationsPerMinute > 0) {
    myStepper.setSpeed(rotationsPerMinute);
    
  }
  else {
    for (int i = 8; i < 13; i++)
    {
      digitalWrite(i, LOW);
    }
  }
  Serial.println(rotationsPerMinute);
}

I'm not familiar with this library, but all you seem to have done is set a potential speed.

Look more carefully at the stepper.h library example in the ide. In particular
stepper_speedControl.ino is in line with your example.
You need to add a .step( ) with a number of steps to move after you set the speed.

Okay, so I took the lazy way out and asked ChatGPT. :sweat_smile: Here's what it came up with and it works! Thank you guys for all your help!

#include <Stepper.h>

// Define the number of steps per revolution for your stepper motor
const int stepsPerRevolution = 200;

// Define the connections to the ULN2003 driver board
const int in1Pin = 8;
const int in2Pin = 9;
const int in3Pin = 10;
const int in4Pin = 11;

// Define the analog input pin for the potentiometer
const int potentiometerPin = A0;

// Create an instance of the Stepper class
Stepper motor(stepsPerRevolution, in1Pin, in3Pin, in2Pin, in4Pin);

// Variable to store the potentiometer value
int potValue;

void setup() {
  // Set the pin modes for ULN2003 inputs
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
}

void loop() {
  // Read the potentiometer value
  potValue = analogRead(potentiometerPin);

  // Map the potentiometer value to the motor speed range
  int motorSpeed = map(potValue, 0, 1023, 0, 200);

  // Set the motor speed
  motor.setSpeed(motorSpeed);

  // Rotate the motor one step
  motor.step(1);
}

Oh, gosh. I see. Thanks for telling me that.