Closed-Loop Speed Control of a DC Motor With Encoder Using a Discrete PI Controller on Arduino - V1.0
by sss2022 in Circuits > Arduino
1373 Views, 9 Favorites, 0 Comments
Closed-Loop Speed Control of a DC Motor With Encoder Using a Discrete PI Controller on Arduino - V1.0
In this project, I present a simple and educational setup to control the rotational speed of a DC motor using an Arduino and a PI controller.
The system is based on a JGB37-520 DC Gear Motor equipped with an integrated rotary encoder and a mounted wheel. The encoder generates pulses that allow the Arduino to calculate the motor’s real-time speed in revolutions per minute (RPM).
A PI controller is implemented to regulate the motor speed. The reference is applied as a step input (300 RPM for 5 seconds, then 0 RPM for 5 seconds) in order to analyze the transient response. The measured speed is filtered using a first-order low-pass filter and used as feedback to improve stability and reduce noise.
This project demonstrates practical closed-loop speed control and transient behavior analysis of a DC motor. This version (V1.0) serves as the base for future improvements.
Supplies
- Arduino Uno development board
- USB Type-A to USB Type-B cable
- 6V DC motor with wheel and encoder ( JGB37-520)
- L298N motor driver module
- 2 × 3.7 V 18650 Li-ion batteries
- 18650 battery holder with switch
- Jumper wires
Closed-Loop Speed Control Block Diagram
The diagram illustrates the complete closed-loop speed control system of the DC motor. The reference speed is first compared with the filtered measured speed to generate the error signal. This error is processed by the discrete PI controller implemented on the Arduino, which computes the appropriate control signal. The control output is sent to the L298N motor driver as a PWM signal, which regulates the voltage applied to the DC gear motor with encoder. The encoder measures the motor’s rotational speed and sends pulse signals back to the Arduino. The measured speed is then passed through a discrete low-pass filter to reduce noise before being fed back into the control loop. This feedback structure ensures stable and accurate speed regulation while allowing transient response analysis during step changes in the reference signal.
Motor Specifications
The motor used in this project is a JGB37-520 DC Gear Motor with the following characteristics:
- Gear reduction ratio: 9.6:1
- Rated voltage: 6 V
No-load conditions:
- Speed: 620 RPM
- Current: 0.1 A
Rated load conditions:
- Speed: 450 RPM
- Current: 0.45 A
- Torque: 0.22 kg·cm
- Output power: 2.7 W
The motor is equipped with an incremental encoder with 11 CPR (Counts Per Revolution) on the motor shaft. Considering the gear reduction ratio of 9.6:1, the effective encoder resolution at the output shaft is:
CPR_TOTAL=11×9.6=105.6 pulses per output revolution.
This resolution is used for speed calculation in RPM.
Calculating Speed in Rpm
To determine the rotational speed of the JGB37-520 motor, an incremental encoder mounted on the motor shaft is employed. This encoder generates a sequence of electrical pulses as the shaft rotates. By counting the number of pulses within a defined sampling interval, the motor speed can be accurately estimated.
The measurement time interval is converted into seconds to ensure unit consistency. The total number of pulses per revolution at the motor’s output shaft accounts for both the encoder resolution and the gearbox reduction ratio:
CPR_TOTAL=CPR_MOTOR.Gearbox_Ratio
Where CPR_MOTOR represents the number of pulses generated per revolution of the motor shaft, and Gearbox_Ratio corresponds to the mechanical reduction ratio of the JGB37-520 gearbox.
The rotational speed in revolutions per minute (RPM) is then calculated as:
RPM=(ΔP/ΔT)×(60/CPR_TOTAL)
Where ΔP is the number of pulses counted during the sampling period ΔT (expressed in seconds). This equation converts pulses per second into RPM at the output shaft, while incorporating both encoder resolution and gearbox ratio.
Finally, the calculated speed can be compared with measurements obtained from an external tachometer to validate the accuracy and reliability of the system.
Discrete PI Controller Implementation
In this project, the PI controller is implemented in its discrete-time form, since the algorithm runs digitally on the Arduino with a fixed sampling period. Instead of using the continuous integral expression, the controller approximates the integral as a cumulative sum of the error over time. At each sampling instant, the speed error is computed as the difference between the reference speed and the filtered measured speed. The integral term is then updated by adding the product of the error and the sampling time.
The control signal is calculated using the discrete PI equation:
u[n]=Kp.e[n]+Ki.∑(e[n].ΔT) with e[n]=yr[n]−y[n]
Where Kp improves the dynamic response, while Ki eliminates steady-state error. yr[n] represents the reference speed, y[n] is the measured filtered speed, and ΔT is the sampling interval expressed in seconds. This discrete implementation makes the controller suitable for real-time embedded systems while maintaining effective closed-loop speed regulation.
Discrete Low-Pass Filter for the Speed Signal
In this project, we used a first-order low-pass filter to smooth the noisy motor speed signal and obtain a more stable measurement. In discrete form, the filter is written as:
y[n]=α.y[n−1]+(1−α).x[n]
where y[n] is the filtered speed, x[n] the noisy speed, and the constant α is related to the filter time constant tau and the sampling period Ts by:
α=tau / (tau+Ts)
This method helps reduce rapid signal variations and provides a more reliable estimate of the motor speed.
Encoder Wiring
Wiring:
- M1: Red wire => positive power supply to the motor (+) (reversing the positive and negative terminals of the motor will change the rotation)
- M2: White wire => negative power supply to the motor (-) (reversing the positive and negative terminals of the motor will change the rotation)
- C1: Yellow wire => First output of the encoder.
- C2: Green wire => Second output of the encoder.
- 3.3V - 5V: Blue wire => positive power supply to the encoder (+), cannot be wrong
- GND: Black wire => negative power supply to the encoder (-), cannot be wrong
Circuit Diagram and Source Code
The schematic was created using Tinkercad and Fritzing, as the L298D module is not available on Tinkercad.
Downloads
Displaying and Visualizing the DC Motor Regulated Speed
The system variables are displayed using Arduino’s serial communication tools: the Serial Monitor and the Serial Plotter. The program sends four variables through the serial port: the time (t), the reference speed (rpmRef), the measured speed (rpm), and the filtered measured speed (rpmFilt). These values are transmitted in a comma-separated format, making them easy to read and process.
The Serial Monitor displays these variables in numerical form, allowing real-time observation of the control system behavior and verification that the program is running correctly. The data shown in the Serial Monitor can also be copied and pasted into a text file, which can then be imported into external software such as MATLAB or Google Sheets for plotting graphs and performing a more detailed analysis of the system response. If you would like to learn how to directly display and plot the data in Google Sheets, you can watch the following video: https://www.youtube.com/watch?v=QWIbKbOPyxY&t=52s
The Serial Plotter provides a quick graphical visualization of the signals, which is useful for observing the general behavior of the system during experiments. However, due to display limitations, the plotted curves may not perfectly represent the actual measured data. For precise analysis and better visualization of the results, using dedicated data analysis tools is recommended.
Tuning of the PI Controller and Filter Parameters
The controller parameters, namely Kp, Ki, and the filter coefficient α, strongly influence the behavior of the system. They affect several important characteristics such as signal smoothing, overshoot, response time, and steady-state error. In this project, the values α=0.7, Kp=2, and Ki=3 were determined experimentally in order to obtain a satisfactory motor response. However, these parameters can be adjusted depending on the application and desired performance, since motor characteristics may vary. Therefore, it is recommended to search for optimal parameters to achieve the best compromise between response speed, stability, and speed control accuracy.