I want variable speed of stepper motor using pulse delay like pd=250,300, 400etc so that motor moves with variable value. Please help anybody have idea?

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

// Variables

int pd = 500; // 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);

I have moved your thread to a relevant section. Installation and Troubleshooting is NOT for your projects as it says in the section’s description.

Any variable used inside and outside of an ISR must be declared as a volatile value.


yes. But I need here is variabe value of pd like 200, 300, 400 and 500. So that motor moved that speed accordingly.

So what happens when you run the code you have?
What does it do?
What would you like it to do?

Hint - you can use Serial.print to see the value of any variables you calculate.

when it execute, motor moves fast with pulse delay is 250 in this case it is fixed pd value 250. So I want pd 300 so that motor moves slowly. I want variable value of pd 300,400 likewise. From serial suppose I'm sending 300 then rotate motor.

So why don’t you change the value of pd?
By the way the bigger the value of pd is the slower it moves, the smaller it is the faster it moves.

You set it here

int pd = 500; // Pulse Delay period

If you want to make it a variable then you have to change something that changes the value of pd, like in response to the reading of a pot on an analogue input pin.

Then change the map function so you get those values.
Note that the reading of the analogue port takes 0.1 mS, which significantly adds to the 300uS you want to add.

i used in a programm this code. I'am German, so the comments are German. If you need, i can translate.
FahreLinks = move left
Verz = Verzögerung = delay
Rampe = ramp (simple version)
AnzahlSchritte = number of steps

The pd you mean is Verz in my code.
The motor moves at the beginning slowlier due to the variable Rampe. The delay from Rampe gos down with increasing the steps in the for-loop.

void FahreLinks(double mm){
Serial.println(" mm Fahrbefehl links");
Rampe = 200;
AnzahlSchritte = mm * FaktorX;
digitalWrite(XdirPin,LOW); //Drehrichtung links auswählen
// Fahrbefehl ausführen. 200 Schritte/Umdrehung
for(double x = 0; x < AnzahlSchritte; x++) {
delayMicroseconds(Verz + Rampe);
delayMicroseconds(Verz + Rampe);
Rampe = Rampe - 1;
if(Rampe < 1){
Rampe = 1;

i hope it can help.
Remember: Calling the Function must look like this:
The decimal dot is needed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.