Stepper Motor pulse, direction and speed control

To run Nema17 stepper motor with A4988 and Arduino UNO, code below runs motor continuously but I need to control number of steps, direction and spped

*/

// Defin pins

int reverseSwitch = 5; // Push button for reverse
int driverPUL = 2; // PUL- pin
int driverDIR = 3; // DIR- pin
int spd = A0; // Potentiometer

// Variables

int pd = 100; // Pulse Delay period
boolean setdir = LOW; // Set Direction

// Interrupt Handler

void revmotor (){

setdir = !setdir;

}

void setup() {

pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);

}

void loop() {

pd = map((analogRead(spd)),0,1023,2000,50);
digitalWrite(driverDIR,setdir);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);

}

Steps are constant, inside loop()
Direction is controlled or changed in revmotor()
Speed is controlled by the potentiometer.

Topic moved. Please do not post in "Uncategorized"; see the sticky topics in https://forum.arduino.cc/c/using-arduino/uncategorized/184.

Please read the section about code tags in How to get the best out of this forum and apply code tags to the code that you posted in the opening post. Code tags make it easier to read the code, easier to copy and the forum software will display it correctly.

Do not have potentio meter, speed is st in IDE

Get one.

1 Like

Hello so speedy person

In my code

  1. Steps are constant, inside loop()
    Where to declare value inside loop()

  2. Direction is controlled or changed in revmotor()
    Direction I am not much concerned as it is always anticlockwise

3. where to declare speed

Regards

Nilesh Dhan Mistry 69Years

First you need to post your code correctly so we can read and copy it without mistakes.

In the IDE click on Edit then Copy for Forum, that will copy your code. Then come here and just do a paste.

First of all, thank you, following is the code

// MOTOR RUNNING OK ACC A4988 241214 - Nilesh Dhan Mistry
//AccST_A4988ContRunOK241214AccST_A4988CoStepper Motor Test
/*
Stepper Motor Test
stepper-test01.ino
Uses MA860H or similar Stepper Driver Unit
Has speed control & reverse switch

DroneBot Workshop 2019
[https://dronebotworkshop.com](https://dronebotworkshop.com)
*/

// Defin pins

int reverseSwitch = 5; // Push button for reverse
int driverPUL = 2; // PUL- pin
int driverDIR = 3; // DIR- pin
int spd = A0; // Potentiometer
//int moveTo();

// Variables

int pd = 100; // Pulse Delay period
boolean setdir = LOW; // Set Direction

// Interrupt Handler

void revmotor (){

setdir = !setdir;

}

void setup() {

pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
//attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);
attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, 50);

}

void loop() {

pd = map((analogRead(spd)),0,1023,2000,50);
digitalWrite(driverDIR,setdir);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);

//moveTo();

}

Your code transforms the potentiometer reading into the half-delay between steps (in microseconds) with this line:

If you have a particular speed, you need to do the math to convert it into a new pd for the delay between steps. Is the “speed” in RPM? SFPM? Rev/sec? How many steps/rev is your motor?

Its number of steps, each cycle mives 20 to 80 steps
Motor is Nema17, 200 PPR

You say you do not have a potentiometer. So how do you plan on setting the speed?

1 Like

Speed is set in code

Install the AccelStepper library

This is the code

#include <AccelStepper.h>

const int pinStep = 4; // Step pin
const int pinDir = 5; // Dir pin
float stepsPerSecond = 0;

AccelStepper stpmotor (1, pinStep, pinDir);

void setup()
{
  Serial.begin(115200);

  stpmotor.setMaxSpeed(1000); // 1000 steps/second

  //Set the speed. Choose 0 to 999 steps/second
  stepsPerSecond = 500;

  stpmotor.setSpeed(stepsPerSecond);

  Serial.print("Speed = ");
  Serial.println(stepsPerSecond);
}

void loop()
{
  stpmotor.runSpeed();
}
1 Like

Well, with that and speed you should be able to calculate pd:

 pd = ???;

pd = ?

Is p equal to ppr?
Is d number of steps?

There is no p, ppr or d in my code.
stepsPerSecond is the speed.
It is how many steps the motor will make in one second. Very simple.

No, pd in your code is...

If you know how many revolutions per minute you want to turn the motor, and the number of steps per revolution, and the number of microseconds per minute, you can convert those to the pulse delay in microseconds per half-pulse.

pd = 60000000us/min /(XXX rev/min) / (200 step/rev) /(2half-step/step) = ???? us/half-step

Ok, I will calculate, do change in sketch and revert back.
Thank you

This system with UNO ULN2003 and 28BYJ through stepper.h is OK but 28BYJ fails.
26 byj is 64 PPR and we number of pulses is from 230 to 800.
RPM 640.
Dwell(65000).
One cycle = (230 to 800 pulses) + Dwell(65000).
It was good to run 28BYJ from 5 vdc of USB, but delicate motor

This need higher power Nema17 and A4988 by Pulse and dir with accelstepper.h

Finally, a speed:

640rev/min * 200 steps/rev *1min/60 sec = 2133 steps/sec

or in half-step delay form

1000000 us/sec /(2133 steps/sec) / (2 half-step/sec) = 234 us/sec.

Jumping instantly to 640RPM (2133 steps/sec) is too fast for the actual motor to keep up. You will need to use acceleration to ramp the speed up so that the motor's rotor can keep up with the stepping. @jim-p's code in #3 would be better (with a stepsPerSecond=2133) and could possibly work with the rest of your system with proper tuning.