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
mastermechanic
108
15
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

1. How do I connect a stepper motor to an Arduino?

To connect a stepper motor to an Arduino, you will need a motor driver that can handle the current and voltage requirements of your motor. Connect the motor driver to the Arduino's digital pins and power supply, and then connect the motor to the motor driver's output pins.

2. What is a closed loop control system?

A closed loop control system uses feedback from a sensor to continuously adjust and maintain a desired output. In the case of controlling a stepper motor, a closed loop control system uses a sensor to monitor the motor's position and make adjustments to ensure it reaches and maintains the desired position.

3. What is PID control?

PID (Proportional-Integral-Derivative) control is a control algorithm that uses feedback from a sensor to continuously adjust the output based on the error between the desired value and the actual value. It takes into account the current error, the cumulative error, and the rate of change of the error to make precise adjustments to the output.

4. How do I implement PID control for a stepper motor in Arduino?

To implement PID control for a stepper motor in Arduino, you will need to use a library or write your own code that calculates the PID values and sends the appropriate signals to the motor driver. You will also need to calibrate the PID values for your specific motor and system.

5. What are the benefits of using closed loop control and PID for controlling a stepper motor?

Closed loop control and PID offer several benefits for controlling a stepper motor, including increased precision and accuracy, reduced power consumption, and improved stability. It also allows for more complex movements and the ability to compensate for external factors that may affect the motor's performance.

Similar threads

Replies
6
Views
977
  • Mechanical Engineering
Replies
2
Views
3K
Replies
8
Views
2K
Replies
9
Views
2K
Replies
6
Views
930
  • Electrical Engineering
Replies
11
Views
963
Replies
1
Views
2K
Replies
5
Views
1K
Replies
31
Views
6K
Replies
1
Views
1K
Back
Top