Controlling stepper with slide potentiometer

Hi, trying to set the speed of a stepper motor using a 50K slide potentiometer. I want it to be stopped completely when slide is in one end, end full speed at the other end.
Hardware:
Arduino UNO
Arduino motor shield
Slider 50K (pins 1, 2, 2, 3)
Bipolar stepper 3V, 0,9°, 0,41Nm

Pot is connected:
Pin 1 to 5V
Pin 2 to analog in A0
Pin 3 ground

Motor is connected to the +/- +/-
External power 3V and the Vin connect is cut

First tried with this code to get the motor running, works fine:

Then I wanted to add the speed control so I changed some of the code using this tutorial:

My code now looks like this and is not working, the motor runs only when slider is in one direction otherwise it just skips around:

// Include the Stepper library:
#include <Stepper.h>
// Define number of steps per revolution:
const int stepsPerRevolution = 400;
// Give the motor control pins names:
#define pwmA 3
#define pwmB 11
#define brakeA 9
#define brakeB 8
#define dirA 12
#define dirB 13
// Initialize the stepper library on the motor shield:
Stepper myStepper = Stepper(stepsPerRevolution, dirA, dirB);
void setup() {
  // Set the PWM and brake pins so that the direction pins can be used to control the motor:
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
}

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }
}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
https://forum.arduino.cc/index.php?topic=712198.0
Then look down to "code problems" about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Good to see you have got the stepper working with some code.

Thanks..Tom.. :grinning: :+1: :coffee: :australia:

Thanks Tom :grinning: done!
(That was a nice feature btw, "copy for forum")

could the motor speed be too fast for the motor?

Maybe so? I din't think I set the actual speed somewhere in the code, just that it should vary between max and min. But now when I think about It the Arduino doesn't know the maximum speed of the motor. It's not clear when I look in the spec.sheet of the motor either. It's a "Wantmotor" product: 42BYGHM809

As it is now, it spins a little at one speed when slider is on one side. But if I slide it to the other side it's just going "ghrmmm" in one place vibrating the same no matter where the slider is.

which side of the pot is closer to the max analog input value of 1023?

Hmm don't really know how to determine that. It's the side closest to pin 1 and 2 when it moves.

is that the side connect to power or ground?

It's the side connected to 5V. What does that mean?

Hi,
Can you try this edit of your code please.
When you have uploaded it, open the IDE "monitor", set the baud to 115200, see what it shows as you adjust the slider.
Do you have a DMM?

// Include the Stepper library:
#include <Stepper.h>
// Define number of steps per revolution:
const int stepsPerRevolution = 400;
// Give the motor control pins names:
#define pwmA 3
#define pwmB 11
#define brakeA 9
#define brakeB 8
#define dirA 12
#define dirB 13
// Initialize the stepper library on the motor shield:
Stepper myStepper = Stepper(stepsPerRevolution, dirA, dirB);
void setup() {
  Serial.begin(115200);
  // Set the PWM and brake pins so that the direction pins can be used to control the motor:
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
}

void loop() {
  delay(100);
  // read the sensor value:
  int sensorReading = analogRead(A0);
  Serial.print("Pot Reading = ");
  Serial.print(sensorReading);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  Serial.print("  MotorSpeed = ");
  Serial.println(motorSpeed);
  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }
}

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

the analogRead () will return a value closer to 1023. motorSpeed will be higher. i found that i couldn't step a motor more quickly than 1 ms.

if the motor has 400 step/rev and a max step rate of 1ms/step, then the max speed is 2.5 rev/sec or 150 rev/min.

since the code scales (maps) the sensorReading from 0-1023 to 0-100 -- max motorspeed is 100 -- i don't see a problem.

but maybe 1 ms/step is too fast for this motor

suggest to you do what @TomGeorge suggested and print the values on the serial monitor

Hi Tom, now the motor at least turns around all the time. When the slider is closest to 5V it moves a little faster, and for all the rest it moves at a constant speed. Just like the IDE monitor shows. It seems that the signal from pot doesn't change when sliding, until I hit the end.
Looks like this in the IDE:
Pot Reading = 72 MotorSpeed = 7
Pot Reading = 76 MotorSpeed = 7
Pot Reading = 80 MotorSpeed = 7
Pot Reading = 81 MotorSpeed = 7
Pot Reading = 75 MotorSpeed = 7
Pot Reading = 76 MotorSpeed = 7
Pot Reading = 82 MotorSpeed = 8
Pot Reading = 76 MotorSpeed = 7
Pot Reading = 74 MotorSpeed = 7
Pot Reading = 79 MotorSpeed = 7
Pot Reading = 73 MotorSpeed = 7
Pot Reading = 705 MotorSpeed = 68
Pot Reading = 986 MotorSpeed = 96
Pot Reading = 988 MotorSpeed = 96
Pot Reading = 990 MotorSpeed = 96
Pot Reading = 991 MotorSpeed = 96
Pot Reading = 992 MotorSpeed = 96
Pot Reading = 992 MotorSpeed = 96
Pot Reading = 992 MotorSpeed = 96
Pot Reading = 993 MotorSpeed = 97
Pot Reading = 993 MotorSpeed = 97
Pot Reading = 993 MotorSpeed = 97

My DMM is not sensitive enough for this I think.

There are 4 pins on the pot, 2 of them are both marked with "2" but I just connected one of them. Figured they were the same? :thinking:

Hi,

Can you post a picture of your pot please.
Your DMM in DC Volts mode, should be able to measure the volts on the input pin connected to the pot with respect to gnd.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

might be an audio taper (logarithmic) pot

Here it is, it's marked "Alpha" and "B50K"

Hi,
Thanks for the images.
Looking at your code, you cannot use A0 or A1 as they are used by the shield.


Change to A2 and see what happens.

Where do each of the coloured wires go?
You only need three connections.
I think
RED should go to 5v of your controller.
WHITE should go to gnd of your controller.
GREEN to your analog input of your controller.
The grey is not needed.

Can you post and image showing your pot, controller and wire connections?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

That did it, now it works perfect :grinning_face_with_smiling_eyes:

What I did:
Changed the connections as you suggested, changed input to A2 and also in the code.

Now I just need a faster stepper for my application I noticed :thinking: :grinning: Only way to speed this up is by increasing the voltage I guess

Thanks both @TomGeorge and @gcjr for helping me out here, very nice first impression of this forum!

Edit, final code:

/* Example sketch to control a stepper motor with Arduino Motor Shield Rev3, Arduino UNO and Stepper.h library. More info: https://www.makerguides.com */
// Include the Stepper library:
#include <Stepper.h>
// Define number of steps per revolution:
const int stepsPerRevolution = 400;
// Give the motor control pins names:
#define pwmA 3
#define pwmB 11
#define brakeA 9
#define brakeB 8
#define dirA 12
#define dirB 13
// Initialize the stepper library on the motor shield:
Stepper myStepper = Stepper(stepsPerRevolution, dirA, dirB);
void setup() {
  // Set the PWM and brake pins so that the direction pins can be used to control the motor:
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);
}

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(A2);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }
}

Any reason you’re using a stepper in particular?
Steppers have uses, but it’s unlikely to be a continuous variable speed drive motor.

There are cases, it a DC motor (geared optional) driven with PWM may be a better choice.

@lastchancename No not really for this application, but I had one laying and was interested in learning about steppers for another project. So yes, I was thinking about a DC motor would be better and also for speed.