How to control a stepper motor in closed loop (PID) in arduino?

In summary, you control a stepper motor by driving in steps and counting the steps. The only other thing you need is some absolute "stop" detection, so you can know when the stepper motor is at position "zero" or similar. You don't need feedback to control a stepper motor.
  • #1
Hello,

What I have
I have a NEMA17 stepper motor driven by a DRV8825 driver module, an arduino uno, a multiturn potentiometer.

What I want to do
I want to control the stepper motors in a PID closed loop. The motor shaft will be connected to the potentiometer and I will be sure that the motor goes to the exact position.

What I have done
All necessary physical connections (arduino,driver,pot,motor etc.) done correctly. I can spin the motor at different speeds, directions and send it to different step positions without using a library. So I am sure that there is no physical problem remaining. However, I dont exactly know how I can control it in a closed loop feedback. It is a bit different then driving a DC motor with PWM in a closed loop PID sketch therefore I am confused.

I am not constraining you,

You can explain how it's done without using a library
You can explain how it's done with using a library (i.e. accelstepper.h)
You can link some pages or videos showing an example sketch

I just want to learn how to control the position of stepper motor in closed loop as it is implemented in robotic arms.
 
Physics news on Phys.org
  • #2
mastermechanic said:
I want to control the stepper motors in a PID closed loop. The motor shaft will be connected to the potentiometer and I will be sure that the motor goes to the exact position.
You control stepper motor by driving in steps and counting the steps. The only other thing you need is some absolute "stop" detection, so you can know when the stepper motor is at position "zero" or similar. You don't need feedback to control a stepper motor...
 
  • #3
berkeman said:
You control stepper motor by driving in steps and counting the steps. The only other thing you need is some absolute "stop" detection, so you can know when the stepper motor is at position "zero" or similar. You don't need feedback to control a stepper motor...

Think about a robotic arm, I sent the arm to a specific position and it went, so far there is no problem. But if I force the arm to move or intervene the arm while it is moving it will miss some steps. The controller must know it is missed and it should compansate it by sending the necessary step commands (i.e. direction and # of step). This is why I need feedback.
 
  • #4
mastermechanic said:
But if I force the arm to move or intervene the arm while it is moving it will miss some steps.
In general, that should be disallowed by the overall design. I guess if you're designing fighting robots or something, you might miss some steps. But in normal operation a stepper motor should not be missing steps. If you detect an overcurrent condition from a partially stuck rotor, you can back up to a reset position where you use the "stop" switch to recalibrate your zero point.

I suppose if you really want a lot of extra fault tolerance for a harsh setup, you can add feedback, but normally with a stepper motor assembly you do not add that extra cost and processing.
 
  • #5
A properly sized (and operated) stepper doesn't really need closed-loop control. Feedback is useful for error detection and homing, but if you need PID for your position moves, your motor may be too small for your speed/inertia. You can position a stepper more precisely with blind steps than with potentiometer feedback.

Having said that: You can operate a PID loop with Position error as PV (input) and Step rate as CV (output). Make sure to limit the step rate to something that will work for your load - step too fast and nothing will happen.
 
  • #6
Yes, if you are moving fast you can't stop immediately without missing steps. You will also need to accelerate up to high speed in control also or you will miss count. That is why open loop (dead reckoning) algorithms need for you to characterize the error free start and stop speeds for your application and then use a trapezoidal velocity profile (acceleration and deceleration). For any high performance high speed motor, you must predict where you will stop and slow down in preparation. Even purely analog controllers do this in response to a decreasing position error.

Step motors are nearly always used open loop since otherwise you would choose a BLDC motor. BLDC motors are nearly the same thing but they work better for analog control. Steps are great if you need steps, but they are an unnecessarily expensive difficulty if you aren't using them. The step motors are used because you can count discrete positions and/or they will park better if you want to turn the drive off. If you did want to use analog control or feedback control with a step motor, you can basically just pretend it's a BLDC motor, they are driven essentially the same way. There's lots of stuff on the web about this sort of application.
 
  • Like
Likes berkeman and Dullard

Suggested for: How to control a stepper motor in closed loop (PID) in arduino?

Replies
23
Views
2K
Replies
4
Views
3K
Replies
10
Views
1K
Replies
9
Views
5K
Replies
7
Views
1K
Replies
9
Views
2K
Replies
3
Views
2K
Replies
3
Views
1K
Back
Top