Stepper motor, encoder and zero angle

Hi all,

I have a problem when an encoder is connected to the Mega board together with a stepper motor. the motor doesn't rotate fluently but brakes and accelerates if the encoder is rotated.
In brief: the goal of my project is :
First I set the zero angle of a rotary encoder, and then I rotate the motor connected to the encoder and I acquire the angle and print the rpm.
My components are: stepper motor NEMA 17, controller A4988, rotary encoder with 600 pulses/rev, arduino mega.

At the beginning of the code the motor doesn't rotate. Then I can position the motor and the encoder connected to it to a certain position, click the button to set the zero angle of the encoder and then the motor starts rotating. I adjust the speed with a potentiometer and print the speed in rpm.
Everything works, except that the motor doesn't rotate smoothly when also the encoder is rotating. For example, if I separate the encoder from the motor and rotate it quickly, the motor almost stops for a while, and then runs again.

do you have any idea for how to solve this problem?
Thank you!!

-Code-



#include <Rotary.h>

Rotary rotary = Rotary(2, 3);

int EncoderPin=2;
int EncoderPin_1=3;
int pushButton = 6;
int buttonState, speedVar, counter, startTime, endTime, x, delayInMillis;
int stepPin = 5;
int dirPin = 4; 
int speedPin = A0;
int enablePin = 7; // Za demagnetizacijo


float razlika;


void setup() {
  // put your setup code here, to run once:
  pinMode(pushButton, INPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT); 
  pinMode(speedPin, INPUT); 
  pinMode(enablePin, OUTPUT); 
  
  
  digitalWrite(enablePin, HIGH); 
  digitalWrite(dirPin,HIGH);
  
  attachInterrupt(digitalPinToInterrupt(EncoderPin,rotate, CHANGE);
  attachInterrupt(digitalPinToInterrupt(EncoderPin_1, rotate, CHANGE);
  
  
  
  
  Serial.begin(2000000);
}

void loop() {

  if(digitalRead(pushButton) == 1){
    
    counter = 0;
    buttonState = 1; 
    digitalWrite(enablePin, LOW); 
    
  }

  while (buttonState == 1){
    
    
    for(x = 0; x < 200; x++) {
      
      speedVar = map(analogRead(speedPin), 0, 1023, 700, 1600);
      
      if (x == 0) {
        
        startTime = millis(); 
      }
      digitalWrite(stepPin,HIGH);
      delayMicroseconds(speedVar); 
      digitalWrite(stepPin,LOW);
      delayMicroseconds(speedVar);
      
      if (x == 199){

        endTime = millis(); 
        razlika = endTime - startTime; 
        float seconds = razlika / 1000;
        float minutes = seconds / 60;
        long int rpm = 1/minutes;
        Serial.println(rpm); 
      }
    }
  }
}

void rotate() {
  
  unsigned char result = rotary.process();
  
  if (result == DIR_CW) {
    counter++;
    //Serial.println(counter);
  } else if (result == DIR_CCW) {
    counter--;
    //Serial.println(counter);
  }
  
  

 if (counter == 600){
    
    counter = 0;
  
 }
}

Hi @mioamr79 .

When posting sketch or printous use tags (</>) .
I suggest redoing your topic and putting your sketch between tags.

Post a drawing with the electro/electronic scheme of your project.
Please use standard schematic.

Fritzing designs are not very effective.

RV mineirin

Thanks,

I tried to put the sketch between < > but the outcome was the same.
anyway, if you have time to take a look at the sketch, I think it is possible to understand it even in this way.

thanks,
Stefano

You should not put the code between '<' and '>' , you should use the button '</>' at the top of your edit window.
Edit your first post, mark the code and klick on '</>'

thanks! I will do it.

I put the code as it should be... and here there is the wiring scheme.
thanks!

Why are you including two different rotary libraries? I don't know both of them, but at first glance it seems that you are mixing them.

you are right.
But that's an error when I changed pc. The only library included now is the Rotary.h, but same problem.

I corrected the original file

Hi, @mioamr79
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Can you post a link specs/data on your encoder please?
Can you post a link specs/data on your motor please?
NEMA17 tells me its physical size, but nothing about its electrical characteristics.

Have you written code that just reads the encoder to show you are getting valid data?
Have you written code that just runs the stepper to show that you have full control of the motor?

You should be able to get an EXPORT jpg image from your CAD, that will be clearer.

What is your motor power supply?

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

Hi @mioamr79 .
There are errors on lines 30 and 31.
One more ")" close parenthesis is missing in both lines.

RV mineirin

Hi all,

I have a problem when an encoder is connected to the Mega board together with a stepper motor. the motor doesn't rotate fluently but brakes and accelerates if the encoder is rotated.
In brief: the goal of my project is :
First I set the zero angle of a rotary encoder, and then I rotate the motor connected to the encoder and I acquire the angle and print the rpm.
My components are: stepper motor NEMA 17, controller A4988, rotary encoder with 600 pulses/rev, arduino mega.

At the beginning of the code the motor doesn't rotate. Then I can position the motor and the encoder connected to it to a certain position, click the button to set the zero angle of the encoder and then the motor starts rotating. I adjust the speed with a potentiometer and print the speed in rpm.
Everything works, except that the motor doesn't rotate smoothly when also the encoder is rotating. For example, if I separate the encoder from the motor and rotate it quickly, the motor almost stops for a while, and then runs again.

do you have any idea for how to solve this problem?
Thank you!!

CODE:


#include <Rotary.h>

Rotary rotary = Rotary(2, 3);

int EncoderPin=2;
int EncoderPin_1=3;
int pushButton = 6;
int buttonState, speedVar, counter, startTime, endTime, x, delayInMillis;
int stepPin = 5;
int dirPin = 4; 
int speedPin = A0;
int enablePin = 7; // Za demagnetizacijo


float razlika;


void setup() {
  // put your setup code here, to run once:
  pinMode(pushButton, INPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT); 
  pinMode(speedPin, INPUT); 
  pinMode(enablePin, OUTPUT); 
  
  
  digitalWrite(enablePin, HIGH); 
  digitalWrite(dirPin,HIGH);
  
  attachInterrupt(digitalPinToInterrupt(EncoderPin),rotate, CHANGE);
  attachInterrupt(digitalPinToInterrupt(EncoderPin_1), rotate, CHANGE);
  
  
  
  
  Serial.begin(57600);
}

void loop() {

  if(digitalRead(pushButton) == 1){
    
    counter = 0;
    buttonState = 1; 
    digitalWrite(enablePin, LOW); 
    
  }

  while (buttonState == 1){
    
    
    for(x = 0; x < 200; x++) {
      
      speedVar = map(analogRead(speedPin), 0, 1023, 700, 1600);
      
      if (x == 0) {
        
        startTime = millis(); 
      }
      digitalWrite(stepPin,HIGH);
      delayMicroseconds(speedVar); 
      digitalWrite(stepPin,LOW);
      delayMicroseconds(speedVar);
      
      if (x == 199){

        endTime = millis(); 
        razlika = endTime - startTime; 
        float seconds = razlika / 1000;
        float minutes = seconds / 60;
        long int rpm = 1/minutes;
        Serial.println(rpm); 
      }
    }
  }
}

void rotate() {
  
  unsigned char result = rotary.process();
  
  if (result == DIR_CW) {
    counter++;
    //Serial.println(counter);
  } else if (result == DIR_CCW) {
    counter--;
    //Serial.println(counter);
  }
  
  

 if (counter == 600){
    
    counter = 0;
  
 }
}

@mioamr79

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

Running the troubeling code, does it really help to just disconnect the encoder wires?
A wiring diagram, not any Fritzing, could be helpful.

Where does the 5 volt come from?
Please, a link to the stepper. NEMA tells the mechanical dimensions, not any electrical info in that.
Please post a link to the rotary encoder. I fear that it outputs 12 volt and endangers Your controller.

@mioamr79
I have merged your other topic on the same subject in to this one. Posting the same question is against the rules that have already been linked to by others. Please read those rules and don't waste the time of the volunteers trying to help you by asking the same question multiple times.

Thank you.

You use the variable 'counter' in an interrupt routine but you failed to declare it as volatile - this is a requirement for global variables used in interrupt routines.

buttonState is never cleared. Why are you using a while loop with it?

thanks!

I already corrected that but it seems not to be the problem. I still have a non smooth rotation of the motor when the encoder rotates

Thanks PerryBebbington for merging the two topics.

By the way, I opened another topic because someone suggested to do so putting the code between <>, and then, if you believe or not, I tried to delete my old post, but I couldn't find how.
Please, next time, before being thinking that I wanted to be arrogant by posting twice, maybe ask the reason. I respect a lot the time of the volunteers.