Using the Feedback of Closed Loop in Arduino Program

Hey Community!

After days of research and contacting the customer support of Stepperonline way too many times, i was thinking about reaching out to this amazing community with my problem:

I want to run NEMA23 (https://www.omc-stepperonline.com/p-series-ip65-waterproof-nema-23-closed-loop-stepper-motor-2nm-283-28oz-in-with-encoder-1000cpr.html?search=water) and NEMA34 (https://www.omc-stepperonline.com/p-series-ip65-waterproof-nema-34-closed-loop-stepper-motor-4-5nm-637-38oz-in-with-encoder-1000cpr.html?search=water) stepper-motors. They have an included encoder (closed loop). The feedback is used so far by the driver (manual: https://www.omc-stepperonline.com/download/CL57T.pdf) to check if there is a lack of positioning.
My goal is to use this feedback in the arduino code. One reason is to have a value to work with (measurement), because i am controlling many steppers after each other and i want to make sure that they have arrived as planned, before the next one starts. Another reason is that with the "standard" code to drive those steppers, i donĀ“t always get the same result as planned and that is crucial to my machine:

digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);

The answer of the Stepperonline Customer support was:

"This is the way to get the encoder position. Check the position of the motor shaft by the number of pulses output from the serial port.
Note that because the encoder is a differential signal, AB should be isolated from GND. But ARDUINO shares a common ground.
It may cause signal interference.
Because A + A- needs to form a loop.
It seems that A- must be connectted to GND. If it is connected to other digital ports of ARDUINO, there will be no level change of A +."

const int stepPin = 34;
const int dirPin = stepPin + 1;
const int enPin = stepPin + 2;

const int schrittvorgabe = 200;
int outputA = 15; // Connection to A+
int outputB = 16; // Connection to B+
int counter = 0;
int aState;
int aLastState;

void setup() {

// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);

pinMode (outputA, INPUT);
pinMode (outputB, INPUT);

Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);

}
void loop() {

digitalWrite(dirPin, LOW); // Enables the motor to move in a particular direction

while (counter >= -50) {

//Motoren fahren*********
// Makes 400 pulses for making two full cycle rotation
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);

//ENCODER AUSLESEN*********
aState = digitalRead(outputA); // Reads the "current" state of the outputA
//Serial.print(outputA);
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState) {
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
Serial.print("Position: ");
Serial.println(counter);
}
aLastState = aState; // Updates the previous state of the outputA with the current state

}

I hope that somebody can help me with the code and show me how to connect the feedback to Arduino correctly. Yet i have put A- and B- on GND of Arduino but think that this could be the mistake.

Thank you in advance!

Think-Digi:
One reason is to have a value to work with (measurement), because i am controlling many steppers after each other and i want to make sure that they have arrived as planned, before the next one starts.

If the stepper motor is correctly sized for the load why wouldn't it arrive where planned? Thousands of cheap 3D printers work very reliably without any need for position feedback.

If your project is missing steps then it is the wrong stepper motor, or the wrong stepper motor driver, or the wrong power supply or the wrong program.

Having said all that I don't know what, exactly, you are asking for advice about.

Almost certainly you will need to use interrupts to detect the encoder pulses. And reliably reading encoder pulses uses up a lot of Arduino CPU cycles.

What Arduino board are you using?

...R

Thank you, Robin2!

I am using a MEGA2560.
It does take quite some time to process in the cycle. The program i have posted initially gives the stepper interrupted impulses because of that.

Would you suggest to use the encoder - signals just to be used in the CL57T?

Think-Digi:
I am using a MEGA2560.
It does take quite some time to process in the cycle. The program i have posted initially gives the stepper interrupted impulses because of that.

If you want a responsive program you should not be using delay() or delayMicroseconds().

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

Also the second example in this Simple Stepper Code illustrates how to use millis() and micros().

You may also consider using the AccelStepper library which has the non-blocking run() and runSpeed() functions

Would you suggest to use the encoder - signals just to be used in the CL57T?

I am really asking why do you need to use the encoder at all? (But I am not familiar with the driver in your link). What will you do if the motor gets to its destination correctly but the encoder count is wrong?

...R

Hey Robin2,

thank you very much for your useful content! This helps me to get a better performance in the code i have built.

After your first post, i have changed some hardware-settings and now the steppers all go to the positions as planned. With that i am saving the current position in a variable and trust that one. To have a definite position (initial position) i wrote a function to bring them all back to 0. This is truly important for me, because i cannot risk the mechanics to break.

Thank you for your time and help! Even though i left my original plan, i have gotten new experience.

Wish you a great day,
Patrick

Think-Digi:
To have a definite position (initial position) i wrote a function to bring them all back to 0.

That is a standard procedure with stepper motors.

...R