Controlling stepper motors speed Help

I would like to control speed with a potentiometer but when i configure it with the diagram and script below make a noise and does not really work any help would be great

Thanks

Steven

const int dirPin = 2;
const int stepPin = 3;
int led2 = 7;
int led = 4;
int sw = 5;
int sw2 = 6;
int stepdelay;


// const int stepsPerRevolution = 800;
 
void setup()
{
  // Declare pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(sw, INPUT);
  pinMode(sw2, INPUT);
}
void loop()
{
int val=analogRead(A0);
stepdelay = map(val,0,1023,-10,100);

  
  // Set motor direction clockwise High = counter clockwise
  //digitalWrite(dirPin, LOW); // moves to the counter closewise (Light Goes On)
  //digitalWrite(dirPin, HIGH);// moves counter clockwise (Light Goes Off)
  //digitalWrite(sw, HIGH);

  
  //digitalWrite(3, LOW);
  //delay(0);
  //digitalWrite(3, LOW);
  //delay(0);


 if (digitalRead(sw2) == LOW)  //direction and speed
  digitalWrite(stepPin, HIGH);
  digitalWrite(dirPin, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, HIGH);
delayMicroseconds(stepdelay);

if (digitalRead(sw2) == LOW) //led
  digitalWrite(led2, HIGH);
if (digitalRead(sw2) == HIGH)
  digitalWrite(led2, LOW);

  if (digitalRead(sw) == LOW)   //direction and speed
  digitalWrite(stepPin, HIGH);
  digitalWrite(dirPin, LOW);
delayMicroseconds(stepdelay);
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, LOW);
delayMicroseconds(stepdelay);

if (digitalRead(sw) == LOW) //led
   digitalWrite(led, HIGH);
if (digitalRead(sw) == HIGH)
   digitalWrite(led, LOW);







  

 
}

Preformatted text

Are you trying to use a 9 volt smoke alarm battery?

embed your code

Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

 if (digitalRead(sw2) == LOW) //direction and speed
      digitalWrite(stepPin, HIGH);  // only this like executes conditionally
  digitalWrite(dirPin, HIGH);  // the rest of these lines execute every
  delayMicroseconds(stepdelay);  // time through loop()
  digitalWrite(stepPin, LOW);
  digitalWrite(dirPin, HIGH);
  delayMicroseconds(stepdelay);

If you use the IDE autoformat tool to indent the code you will see that there may be curly brackets ({}) missing from some if blocks, like the one above. Only the very first line of code is executed conditionally after the if, not the whole block. Is that what you intended?

What do you expect a delay of -10 to do?

You are trying to step the motor way too fast. Try longer delays. No stepper will go to 5K steps per second from a dead stop. You will need acceleration to achieve high speeds. Like offered by the AccelStepper or MobaTools stepper libraries.

Did you set the coil current setting on the stepper driver?

Use Robin2's simple stepper program tutorial to get your stepper to work, then add on buttons and pot.

Hi,
What are the switches circled for?

Please hand draw your circuit and include component names, pin labels and all power supplies.
Your Fritzy unfortunately gives us little or none of this information.

Have you got the wires from the stepper connected the proper way to the driver PCB?

Do you have a DMM?

Can you please post a link to specs of your stepper motor?

Try longer pulse steps, milli-seconds not micro-seconds. Your step pulses may be too short to actually transfer any energy to the stepper windings.
What speed do you want from your stepper?

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

Are you missing some of these { } ?

It looks like you want to turn in one direction with the 'led' pin HIGH if the 'sw' pin reads LOW and turn the other directionwith the 'led2' pin HIGH if the 'sw2' pin reads LOW.

That would be something like:

void loop()
{
  int val = analogRead(A0);

  // Map to 1000 to 5 steps per second
  stepdelay = map(val, 0, 1023, 500, 100000);

  if (digitalRead(sw) == LOW) //direction and speed
  {
    digitalWrite(dirPin, LOW);
    digitalWrite(led, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(stepdelay);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(stepdelay);
  }
  else
    digitalWrite(led, LOW);

  if (digitalRead(sw2) == LOW) //direction and speed
  {
    digitalWrite(dirPin, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(stepdelay);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(stepdelay);
  }
  else
    digitalWrite(led2, LOW);
}

will need to set up limited switches on a rail for a camera mount with a belt

powered with a 5 v power supply

ok thank you, i want to go from very slow to about medium speed, not fast

stepper motor is Nema 17 Bipolar

everything works but the speed control works ok at full speed but when slowed down motor chatters, noisy, jumps, not smooth

john going to try thanks

ok done ......

thanks

I want to set pre determined speed with the potentiometer switch,
Then press one button for one direction or the other for the other direction.
I add the LED's to show the direction.
The switches are there to simulate limit switches when the carriage reaches the end it stops until then you can push the other button in the other direction and so on

good. i take a look.

ok will do thanks

const byte dirPin = 2;
const byte stepPin = 3;
const byte led2 = 7;
const byte led = 4;
const byte sw = 5;
const byte sw2 = 6;
int stepdelay = 0;

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(sw, INPUT);
  pinMode(sw2, INPUT);
}

void loop() {
  stepdelay = 2000 + analogRead(A0);

  if (digitalRead(sw2) == LOW) {//direction
    digitalWrite(dirPin, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led, LOW);
  }

  if (digitalRead(sw) == LOW)  //direction
  {
    digitalWrite(dirPin, LOW);
    digitalWrite(led, HIGH);
    digitalWrite(led2, LOW);
  }

  digitalWrite(stepPin, HIGH);
  delayMicroseconds(stepdelay);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(stepdelay);
}

works in my wokwi arduino simulator will try on the devices later
here is my project goal

I want to set pre determined speed with the potentiometer switch,
Then press one button for one direction or the other for the other direction.
I add the LED's to show the direction.
The switches are there to simulate limit switches when the carriage reaches the end it stops until then you can push the other button in the other direction and so on

thanks again

Steven

Hi,

Possibly because your pulses are too short.

Did you try the longer pulses?

Have you set the current limit on the A4988?

That only tells me how physically big it is, not its electrical characteristics.

Tom.... :smiley: :+1: :coffee: :australia:

A 5volt supply is not enough for most stepper drivers.
The A4988 needs a minimum of 8volt.
12volt is a practical value, and 24volt is better if you want torque at higher speed.
Post the model number (voltage/current) of your motor (not it's size) to make sure the driver and supply are right for that motor.
You should use a stepper driver library, so micro-stepping acceleration and deceleration are properly executed.
Leo..

Hi,
Can we please have a circuit diagram?
An image of a hand draw schematic will be fine, include ALL power supplies, component names and pin labels.

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