The PID is one of popular control algorithms widely used in almost every devices, from industrial to educational purposes. I will show you a simple PID algorithm in motor control system. Motor controller can be made by using open loop control, the user actively control the motor and if there is an error the system can not compensate it. By using close loop control, the system can compensate the error automatically in microseconds, and it can hold the motor position at desired set point. This project used PID algorithm to control the DC motor speed.
Materials and Components:
-X-TAL 4 Mhz
-Motor driver L298N
-Motor DC 6V
-Rotary Encoder with 22 holes
-and some capacitors and resistors
The value of set point is controlled using trimpot. The output values from trimpot are converted into digital form by ATMega8535's ADC. LCD is used to display the set point value and rotary encoder value (measured DC motor speed). Therefore, I set the change of 1 RPM is equal to 1 bit of the trimpot output value. L298N is a commonly used dual H-bridge to control 2 DC motors. PWM signals to control the DC motor speed are generated from output timer 2 (8 bit) on Port D.7. Push button is connected to Port D.2 to change the motor direction. You can see the schematic of this DC motor controller on the left side.
After initializing all the necessary variables, the microcontroller convert the trimpot output value using its ADC feature. Rotary encoder pulses will trigger the interrupt 0. IN ISR 0 (Interrupt Service Routine 0) the pulses will be counted. It is used to measure sensed RPM of the DC motor which will be done in ISR timer 1 every 0.1 second. PID output value will be determined in another subroutine. The output will be converted to PWM signals by using timer 2. The last thing to do is display set point value and sensed RPM value on LCD, and then repeat all of steps from beginning, it goes on and on until the machine is turned off. The codes are developed in AVR Studio.