Module-03:
Speed Control of a DC Motor Using PID Controller
1 Part A: Design of a PID Controller for DC Motor
Speed Control
In this module, the touchless dustbin system is upgraded with a PID-based speed control
mechanism for the DC motor. Because the speed at which the motor operates directly affects
how quickly the lid opens and closes, precise speed regulation is crucial for consistent and
reliable performance. You will use the same DC motor from the previous setup, removing it
from the dustbin to focus on implementing closed-loop speed control of the DC motor. The
motor’s rotational speed (RPM) will be measured using a quadrature encoder and actively
maintained at a user-selected reference value, chosen via a DIP switch. Both the target RPM
and the real-time measured RPM will be monitored and displayed using the Arduino IDE serial
monitor, ensuring clear feedback and control throughout the process.
This experiment will help you understand:
How to connect and utilize a DC motor with a built-in quadrature encoder for real-time
speed and direction sensing.
Reading DIP switch or digital input states to specify user-defined speed setpoints.
The principles and practical implementation of PID controllers on embedded hardware.
The role of PPR (Pulses Per Revolution), gear ratio of a DC motor and how these influence
accurate speed and position measurement with integrated encoders.
Real-time monitoring and interpretation of feedback and control signals using the Arduino
serial monitor and plotter.
Objectives:
1. Accurately measure the DC motor’s rotational speed (RPM) and direction with an at-
tached quadrature encoder.
2. Select desired RPM setpoints using a DIP switch, as specified:
1
DIP Switch State Reference Speed
0 90% of r
1 80% of r
2 70% of r
3 60% of r
Here, r is the maximum RPM at rated voltage for the supplied motor, determined by
experiment.
3. Display real-time current and target RPMs in the following serial format:
Des: ...... and Cur: ......
4. Design, implement, and tune a PID controller to achieve setpoint tracking. Begin with
KI = KD = 0, tune KP , then incrementally introduce KI and KD to optimize settling
time, overshoot, and error.
Pre-Lab Work:
1. Study DC motor speed control via PWM and the L293D driver
[Link]
2. Learn to use Arduino Serial Monitor and Serial Plotter for data visualization
[Link]
3. Review DIP switch operation and digital input reading in Arduino.
4. Understand quadrature encoder fundamentals: channels, PPR, direction, gear ratio, and
speed calculation.
Module Theory Highlights:
Encoders and Direction Sensing:
Quadrature encoders output pulses on channels A (C1 ) and B (C2 ) with a 90◦ phase shift.
Using interrupts on channel A, read channel B in the ISR to decode both pulse count and
rotation direction.
Pulse per Revolution (PPR):
PPR is the number of pulses generated per encoder shaft revolution. Determine PPR ex-
perimentally by tracking pulse count for a full shaft rotation. Accurate knowledge of PPR
is critical for converting measured pulse rates to RPM and for all feedback calculations.
Speed and Gear Ratio:
Use the measured PPR value and any observed gear ratio to calculate the true speed of
PPS
the motor shaft. Speed (in rev/sec) is given by PPR , where PPS (pulses per second) is
determined by counting encoder pulses in fixed time steps.
2
Filtering:
Apply a low-pass filter to encoder speed measurements to reduce noise and improve control
performance.
PID Control Logic:
Implement the PID law by calculating the error e = vref − vmeasured and summing the
proportional, integral (with anti-windup), and derivative terms to generate the actuator
command. Tune gains to achieve desired transient and steady-state metrics.
Report Guidelines:
1. Experimental objectives
2. Neatly drawn circuit and block diagrams showing all connections and signal flow
3. Flowchart or pseudo code for the implemented control algorithm
4. Key observations and screenshots/plots clearly labeled, including:
Serial monitor/plotter outputs for various controller settings:
– Impact of KP (KI = KD = 0)
– Impact of KI (KP = KD = 0)
– Impact of KD (KP = KI = 0)
– Final tuned system performance with all gains (show settling bands if possible)
Raw and filtered encoder signals (with plots)
Evidence and explanation of PPR measurement and direction sensing
Demonstration of anti-windup effect (if implemented)
Gear ratio determination method and results
5. Clear answers to the following technical questions
Answer the following:
1. Draw and explain the block diagram of your closed-loop system. Where is the setpoint,
and how is feedback incorporated?
2. Why is closed-loop (feedback) control required for this application? Why wouldn’t open-
loop PWM control suffice?
3. How did you determine the PPR and gear ratio? Discuss their importance in your project.
4. How was rotation direction detected and validated using the encoder channels?
5. What maximum RPM (r) did you experimentally determine for your motor?
3
6. Based on discussions on DC motor in EE250, what is the order of the transfer function
you expect the DC motor to have. Sketch the approximate root locus of the DC motor.
7. Describe the PID tuning approach used - Closed loop Zieglar-Nichols or trial-and-error.
Justify
8. Explain the effect of increasing each individual gain (KP , KI , KD ), using both time-
domain results and control theory concepts.
9. What filtering scheme was used on the encoder data, and how did it affect control per-
formance?
4
Understanding Encoder Channels A and B
Rotary encoders use two output channels, A and B, to generate digital pulses as the shaft
rotates. These signals are offset by 90◦ (quadrature encoding), allowing both rotation count
and direction detection.
Quadrature Encoding Principle
In quadrature encoding, channels A and B produce pulse trains with transitions occurring at
different times. By observing which channel transitions first, the direction of shaft rotation is
determined.
Timing Diagram
Channel A
Channel B
Time
Interpretation:
Channel A and B pulses are phase-shifted.
The leading signal identifies rotation direction; e.g., if A leads B, the shaft rotates one
way; if B leads A, the other way.
Direction Sensing
Detecting a rising edge on channel A and reading channel B at that instant:
B HIGH: Increase count (e.g., counter-clockwise).
B LOW: Decrease count (e.g., clockwise).
This enables efficient position and direction tracking, typically by attaching interrupts to chan-
nel A and sampling channel B inside the ISR.
Refer: [Link]
5
Counting Pulses Per Revolution (PPR) & Speed Mea-
surement
What is PPR and Why is it Important?
Pulse Per Revolution (PPR) indicates how many electrical pulses an encoder produces per full
shaft rotation. Higher PPR increases measurement resolution and enables accurate calculation
of speed or position, which are critical for closed-loop motor control applications.
PPR enables:
Fine position and speed measurements for precise feedback.
Conversion of pulse counts into real-world quantities (rotation, speed, angle).
Proper function of control algorithms such as PID.
Procedure for Determining PPR and Calculating Speed
1. Power the encoder: Connect the VCC and GND pins of the N20 DC motor’s encoder
to a 5V supply; this ensures the encoder receives power. The M1 and M2 pins of the N20
DC motor should be connected to the L293D motor driver, which provides power to the
motor itself—typically 3V for the motors supplied in this experiment.
Figure 1: N20 DC motor magnet
Note: While determining the PPR of the encoder, you do not need to supply 3V to the
M1 and M2 motor terminals. Instead, manually rotate the encoder wheel or magnet to
generate encoder pulses as mentioned in Step-4.
6
2. Pulse Counting: Use interrupts to detect the rising edge on channel A (C1 ), with
direction determined by the state of channel B (C2 ).
attachInterrupt(digitalPinToInterrupt(encoderChA), encoderA_SR, RISING);
void encoderA_SR() {
if (digitalRead(encoderChB)==HIGH) {
en_pulse_count++; // One direction
} else {
en_pulse_count--; // Opposite direction
}
}
3. Monitor Encoder Count: Print en pulse count to the serial monitor for real-time
tracking.
4. Manual Rotation: Spin the magnet/encoder wheel (see Figure 1) manually until the
output shaft completes a full revolution. You will see the encoder count increase in the
serial monitor.
5. Record PPR: The final count on the serial monitor after one shaft turn is your encoder’s
PPR.
6. Find Gear Ratio: Dividing shaft revolutions by encoder wheel revolutions yields the
motor’s gear ratio.
7. Measure PPS: Count encoder pulses over a known time step; divide by elapsed time to
compute PPS.
8. Speed Calculation: Compute speed using:
PPS
Speed (rev/sec) =
PPR
where PPS is pulses measured in one second.
9. Note: Keep motor IN1 and IN2 pins LOW as direction is not essential for finding the
PPR.
PID Control for Speed Regulation
Implementation Steps
1. Setup: - Configure all relevant motor and encoder pins. - Initialize PID parameters: Kp ,
Ki , Kd .
7
2. Set-Point: - Define and update the desired speed (vref ).
3. Encoder Reading: - Use ISR for pulse count, calculate measured speed; optionally filter
velocity.
4. PID Control Logic: - Compute error: e = vref − vmeasured . - Calculate Proportional,
Integral, and Derivative terms and combine them:
u = Kp e + Ki · integral + Kd · derivative
- Apply anti-windup by capping integral accumulation if output saturates.
5. Motor Command: - Set direction and PWM values as per control output, within
hardware bounds.
6. Monitoring: - Output key variables for tuning and diagnostics.
7. Safety: - Include any necessary failsafes or delays in control loop.
PID Pseudo Code Example
START
INITIALIZE: set pins, interrupts, PID parameters, set-point
LOOP:
Read encoder count and velocity (safe access with interrupts)
Filter velocity (optional)
Update set-point if required
error = set-point - measured velocity
error_proportional = Kp * error
error_integral += error * elapsed_time (with anti-windup)
error_derivative = Kd * (error - previous_error) / elapsed_time
previous_error = error
control_output = error_proportional + Ki * error_integral + error_derivative
Set motor direction and clamp output
Apply anti-windup if needed
Issue motor command (direction, PWM)
Log variables for debugging
Wait small delay
END LOOP
8
OBSERVATION SHEET - MODULE 3A
This sheet must be attached to the report (print on both sides).
1. Show encoder signals (filtered and unfiltered) to the TA with screenshots comparing both
(attach with report).
TA Signature with Date:
2. Demonstrate how the value of PPR was determined experimentally, including screenshots
or serial monitor outputs for one full motor shaft rotation (attach with report).
TA Signature with Date:
3. Find the gear ratio (Show computation in report also).
TA Signature with Date:
4. Show measurement of pulses per second (PPS) for a given time step, and subsequent
calculation of RPM using the determined PPR value (Show computation in report also).
TA Signature with Date:
5. What is the maximum rpm of the DC motor at rated voltage? (Show computation in
9
report also)
TA Signature with Date:
6. Demonstrate direction detection: show that pulse count increases or decreases with
changes in shaft rotation direction. Provide screenshots or recorded observations for
both directions.
TA Signature with Date:
7. Demonstrate proper interfacing of the motor and DIP switch with Arduino UNO. Show
that current and desired RPM values are printed on the serial monitor.
TA Signature with Date:
8. Show output plots for different values of KP , keeping KI = KD = 0. Confirm screenshots
were taken (Attach with report).
TA Signature with Date:
9. Show output plots for increasing KI with KP = KD = 0 (Attach with report).
10
TA Signature with Date:
10. Show output plots for increasing KD with KP = KI = 0 (Attach with report).
TA Signature with Date:
11. Show final tuned system and verify that desired RPM is met (within 95%) for all DIP
switch states. Provide settling bands and overshoot for each case (Attach with report).
TA Signature with Date:
The report for Module-03 will have two observation sheets. The present observation
sheet is for Module-03A. For part B of the Module-03, each team must have one laptop
with them with MATLAB latest version installed. Module-03B will be done using MATLAB
and the system that you have designed till now.
11