Two steppers with encoder - Arduino mega

Hey Everyone. I've got the following things: -Arduino mega 2560 -2x NEMA 11 stepper motor (200 steps / rot (1.8deg)) with an 500ppr incremental Encoder. Each motor is wired to L298p driver IC circuit. -L298p's are connected to arduino like: Left motor: A - arduino 3 A' - arduino 4 B - arduino 5 B' - arduino 6 Enable - arduino 7

Right motor:

A - arduino 8 A' - arduino 9 B - arduino 10 B' - arduino 11 Enable - arduino 12

Now I'm not using the encoders, cause they're acting weird. The problem is that the arduino can't read the encoder signals fast enough. I really hope someone can help me. Both encoders are connected to arduino's +5 and gnd. Left motors encoder is connected to arduino's 21(Enc A) and 20(Enc B) Right motors encoder is connected to arduino's 19(Enc A) and 18(Enc B).

I'd like to spin both motors at the same time, achieving the perfect positioning with the help of the encoders. I'm quiet new using arduino, and I read something about interrupts, but i don't know how to use them. Can someone help me achieve the following: Rotate both motors 800steps CW, then 800steps CCW. (At high speed if it's possible)

Datasheets: NEMA 11 datasheet, Encoder L298 driver circuit Thank you all in advance, and sorry for my bad english

If you use specialized stepper drivers such as a Pololu DRV8825 the computational load on the Arduino will be much lower than with two L298s

NB. I can't be certain that the DRV8825 is suitable as you have not provided a link to the datasheet for your motor

However, more significantly, if you are using a stepper motor why do you need an encoder? If the mtor is missing steps you are either not driving it correctly or you have an inappropriate motor for the application.

...R Stepper Motor Basics Simple Stepper Code

There is a datasheet of the motor above Nema11 Datasheet, Encoder, and if you open the link you can see the motor details under the NEMA11 one.

I’ve tried the arduino example for stepper motors. The one with one rotation cw, then one ccw. My motor turned around 2-3 times correctly, then started moving randomly 1/2 rot cw, 1/4 rot ccw, etc.

Then I wrote a simple logic code (I know it isn’t nice, but works better) like:

digitalWrite(LeftmotorEn, HIGH);
delay(50);
digitalWrite(RightmotorEn, HIGH);
delay(50);

digitalWrite(LeftmotorD,LOW);
digitalWrite(LeftmotorA,HIGH);
digitalWrite(RightmotorD,LOW);
digitalWrite(RightmotorA,HIGH);
delay(34);
digitalWrite(LeftmotorA,LOW);
digitalWrite(LeftlmotorB,HIGH);
digitalWrite(RightmotorA,LOW);
digitalWrite(RightmotorB,HIGH);
delay(33);
digitalWrite(LeftmotorB,LOW);
digitalWrite(LeftmotorC,HIGH);
digitalWrite(RightmotorB,LOW);
digitalWrite(RightmotorC,HIGH);
delay(32);
digitalWrite(LeftmotorC,LOW);
digitalWrite(LeftmotorD,HIGH);
digitalWrite(RightmotorC,LOW);
digitalWrite(RightmotorD,HIGH);
delay(31);

etc,

By decreasing the delay until it reaches 1ms. Then put the same code part in a for(int b=0; b<204; b++){
}

But I don’t know how to achieve correct accelrating. It would be better to use interrupts, because that’s the only way the Arduino could sense all the encoder signals.
Is it possible to declare counterA and CounterB, and increment both when the interrupt happens? But I read something that this method uses the timer0, so the delay function won’t work.

What I need is: a code, that makes two motors rotate CW twice, then CCW twice, but not 2002 steps, rather 2500 encoder pulses. Is it possible?
Thank you in advance!

Take a look at the AccelStepper library - its polls so you have to call run() in loop all the time,
but it handles all the acceleration logic for you.

A 1A 2.3ohm bipolar stepper is not suited to L298 drive at all - DRV8825 or A4988 is the way to go.

Your motor is skipping because it can’t do 1000 steps a second voltage-driven. Drive it from a DRV8825
and 24V supply and you’ll probably get to the limit of the AccelStepper library…

Your encoder sensing needs to be interrupt-driven, I think from the DS it is 2000 counts/revolution,
so at 1000rpm it’ll be generating transitions every 33us. 500 “impulses” per revolution I think
means pulses per revolution (4 transitions per pulse). You can arrange to trigger interrupts from
only one of the encoder outputs, which halves your resolution and halves the rate interrupts come in too.

vajdacsab: There is a datasheet of the motor above Nema11 Datasheet, Encoder, and if you open the link

Sorry. I missed that.

But you have not explained why there is any need to use an encoder?

...R

Robin2:
Sorry. I missed that.

But you have not explained why there is any need to use an encoder?

…R

So… The two motors are moving two linear actuators. There is a product at the end position that needs to be modified. (There is a simple paint drawing attached). There could be different products. Sometimes the product’s wall is a little strong, and a bit pushes back the shaft. That means that the motors won’t turn back to start position. So at the next time the shaft isn’t able to reach the product.

I hope it’s clear now.

img.jpg

Image from Reply #5 so we don’t have to download it. See Image Guide

f3c5c01e403fb73b8a9486c84d1930e61fccab94.jpg

…R

vajdacsab: Sometimes the product's wall is a little strong, and a bit pushes back the shaft. That means that the motors won't turn back to start position.

That sounds like the load on the motors is too big causing them to miss steps. Maybe a more powerful motor is needed?

If you do want the motor to miss steps (perhaps to avoid damaging the product) a simple way to re-estalish position would be a limit switch at the fully-retracted position. Just keep the stepper moving back until it triggers the limit switch.

...R

Robin2: That sounds like the load on the motors is too big causing them to miss steps. Maybe a more powerful motor is needed?

No, I think its that they are not being driven properly and can't go very fast as a result. Also the lack of microstepping may be causing severe mid-band resonance.

Robin2: Image from Reply #5 so we don't have to download it. See Image Guide

...R

Thank you, I didn't know it.

MarkT: No, I think its that they are not being driven properly and can't go very fast as a result. Also the lack of microstepping may be causing severe mid-band resonance.

So instead of: A (delay) B (delay) A/ (delay) B/ (delay) I should drive AB (delay) BA/ (delay) A/B/ (delay) B/A (delay) ?

vajdacsab:
Thank you, I didn’t know it.

So instead of: A (delay) B (delay) A/ (delay) B/ (delay)
I should drive AB (delay) BA/ (delay) A/B/ (delay) B/A (delay) ?

No, use a DRV8825 and 24V supply, then the motor is properly driven for operation at speed.

I repeat what I said earlier:

A 1A 2.3ohm bipolar stepper is not suited to L298 drive at all - DRV8825 or A4988 is the way to go.

Your motor is skipping because it can’t do 1000 steps a second voltage-driven. Drive it from a DRV8825
and 24V supply and you’ll probably get to the limit of the AccelStepper library…

I agree with @MarkT that they may be missing steps because they are being incorrectly driven.

However you have not said whether you do or don't want them to miss steps.

...R

Thank you for all answers!

Robin2: I agree with @MarkT that they may be missing steps because they are being incorrectly driven.

However you have not said whether you do or don't want them to miss steps.

...R

I don't want them to miss steps. I'm also using another stepper, a NEMA17, which is driven with the same parameters. Currently I'm not able to replace the drivers, I have to find a solution. The NEMA 17 works fine, it doesn't miss steps, and runs very smoothly. By NEMA11's I hear there is something with the accelrating. When it reaches the the desired speed then it runs smoothly too. So I know it's possible, I just don't know how to determine the delay values. At the very start they've missed really much steps, so I've changed the delays few times, now they're only missing 1-2 steps. During the accelration, the delays between each steps are (millisec) 34, 33, 32, 31, 30, 29, 28, 27, 26, 24, 22, 20, 17, 14, 11, 8, 5, 2, 1.

vajdacsab: I don't want them to miss steps.

At the very start they've missed really much steps,

Why didn't you tell us that at the start.

Have you tried using the AccelStepper library as recommended by @MarkT in Reply #3

...R

I’ll try it.
Thank you all for your help.

About the encoders, and interrupts:
Can someone please send me a short sketch that contains the interrupt handling?
I mean where is a counter variable for each encoder that increments or decrements, and an empty loop(). I’ll put my current code in it, and make the motors run until the counters reach the signal.

Something like (It’s my old basic code, but I’ll modify it to arduino-friendly )

Do While (Count1 < A_pos and Count0 < B_pos

IF (Count1 < A_pos and Count < B_pos) Then
IF(StepN == 1 and Count1 < A_pos and Count < B_pos)
Then
Byteout 5, Step_A_B_1
End IF
IF(StepN == 2 and Count1 < A_pos and Count < B_pos)
Then
Byteout 5, Step_A_B_2
End IF
IF(StepN == 3 and Count1 < A_pos and Count < B_pos)
Then
Byteout 5, Step_A_B_3
End IF
IF(StepN == 4 Count1 < A_pos and Count < B_pos)
Then
Byteout 5, Step_A_B_4
End IF
End IF

IF (Count1 == A_pos and Count < B_pos) Then
IF(StepN == 1 and Count1 ==A_pos and Count < B_pos)
Then
Byteout 5, Step_B_1
End IF
IF(StepN == 2 and Count1 == A_pos and Count < B_pos)
Then
Byteout 5, Step_B_2
End IF
IF(StepN == 3 and Counts == A_pos and Count < B_pos)
Then
Byteout 5, Step_B_3
End IF
IF(StepN == 4 Count1 == A_pos and Count < B_pos)
Then
Byteout 5, Step_B_4
End IF
End IF

IF (Count1 < A_pos and Count == B_pos) Then
IF(StepN == 1 and Count1 < A_pos and Count == B_pos)
Then
Byteout 5, Step_A_1
End IF
IF(StepN == 2 and Count1 < A_pos and Count == B_pos)
Then
Byteout 5, Step_A_1
End IF
IF(StepN == 3 and Count1 < A_pos and Count == B_pos)
Then
Byteout 5, Step_A_3
End IF
IF(StepN == 4 Count1 < A_pos and Count == B_pos)
Then
Byteout 5, Step_A_4
End IF
End IF

StepN++
Delay 2
Loop

Search and you will find several examples on these forums.