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

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

1. What is a closed-loop control system for stepper motors?

A closed-loop control system for stepper motors involves using feedback to continuously adjust the motor's performance. This means that the system monitors the motor's position and speed and makes real-time corrections based on the difference between the desired and actual performance. This is typically achieved using a PID (Proportional, Integral, Derivative) controller, which helps to minimize the error and improve the accuracy of the motor's movements.

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

To implement PID control for a stepper motor in Arduino, you need to first set up the hardware, including the motor, driver, and feedback sensors (like encoders). Then, you can use a PID library, such as the Arduino PID Library, to define the PID parameters (Kp, Ki, Kd). In your loop, read the current position from the encoder, calculate the error from the desired position, and use the PID controller to determine the appropriate motor commands to minimize this error.

3. What are the advantages of using PID control with stepper motors?

The advantages of using PID control with stepper motors include improved accuracy and stability in positioning, smoother motion, and the ability to handle disturbances and changes in load more effectively. PID control helps to eliminate overshoot and oscillation, which can be common in open-loop systems, leading to better overall performance in applications requiring precise control.

4. What components do I need to control a stepper motor in closed loop with Arduino?

To control a stepper motor in closed loop with Arduino, you will need the following components: an Arduino board, a stepper motor, a stepper motor driver (such as A4988 or DRV8825), a feedback sensor (like an encoder), and the necessary wiring and power supply. Optionally, you may also want to include a PID library for easier implementation of the control algorithm.

5. How do I tune the PID parameters for my stepper motor?

Tuning PID parameters (Kp, Ki, Kd) for your stepper motor can be done using several methods. A common approach is the Ziegler-Nichols method, where you first set Ki and Kd to zero and increase Kp until the system oscillates. Then, you can adjust Ki and Kd based on the oscillation period and amplitude. Alternatively, you can use software tools or libraries that offer auto-tuning features to help find optimal parameters more easily. Experimentation and iterative adjustments are often necessary to achieve the best performance for your specific application.

Similar threads

Back
Top