Hi guys, I am trying to build a final project for school. Trying to control a stepper motor speed with a rotary encoder, but so far no luck

I am using a NEMA 17 stepper with a A4988 driver, and the standard rotary encoder. I'm trying to get a base layout for my project, and would appreciate any tips to map the rotary encoder to control the speed of the motor. What I currently have is not working for the speed adjustment.

int startPinState;
int lastStartPinState;
int EMOstate;

const int CLK = 11;
const int DT = 10;
const int SWpin = 9;

const int dirPin = 2;
const int stepPin = 3;

const int startPin = 4;
const int GLED = 5;
const int EMOButton = 13;
const int RLED = 12;

int lastCLKState;
int currentCLKState;
int speedControl;
const int maxSpeed = 500;
const int minSpeed = 0;

void setup() {
  pinMode(startPin, INPUT);
  pinMode(EMOButton, INPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  pinMode(SWpin, INPUT_PULLUP);
  pinMode(RLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  Serial.begin(9600);

  lastCLKState = digitalRead(CLK);
}

void loop() {
  currentCLKState = digitalRead(CLK);

  if (currentCLKState != lastCLKState) {
    if (digitalRead(DT) != currentCLKState) {
      speedControl++;
    } else {
      speedControl--;
    }
    speedControl = constrain(speedControl, minSpeed, maxSpeed);
  }
  lastCLKState = currentCLKState;

  Serial.println(speedControl);


  startPinState = digitalRead(startPin);

  if (startPinState == HIGH) {
    digitalWrite(dirPin, LOW);
    digitalWrite(RLED, LOW);
    digitalWrite(GLED, HIGH);

    int speedTime = map(speedControl, minSpeed, maxSpeed, 1000, 500);

    for (int i = 0; i < 100000; i++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(speedTime);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(speedTime);
      if (digitalRead(EMOButton) == HIGH) {
        digitalWrite(GLED, LOW);
        digitalWrite(RLED, HIGH);
        break;
      }
    }
  }
}```

In what way is it not working , what actually happens? wiring diagram ? Power supply ?

Please read the guidelines on how to post .

HI, @samsand0

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

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

Can you use libraries ?

The encoder library would help. And of course any stepper library

Don’t lock your loop in a tighter loop -
you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

Hi,

This might help you get the basics working before adding the control etc, to establish you have control of the stepper.

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

And …. It’s worth getting the parts working on their own .
Get the encoder working and displaying numbers on the screen . Map
It’s values and check that works .
Get the stepper running on its own and try different speeds ( delays between steps ) .
De bugging by printing out your variables to the monitor helps you follow what is happening .

1 Like

Appreciate your help. Breaking the code down helped a lot. I was able to figure out how to map the rotary encoder values to my motor. Thanks!

Appreciate your help. Adding a stepper library was much easier than using none. Thanks.

Sorry, I will be sure to add everything next time.

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