Understanding PID Control for Battery Charging
The PID (Proportional-Integral-Derivative) controller is a fundamental control loop feedback mechanism that helps maintain your desired charging current. Let me explain each component and how to tune them for your battery charging application.
The Three Components of PID
1. Proportional Term (KP)
Role: Provides an immediate response to the current error (difference between target and actual current).
- Effect: The larger the error, the stronger the correction
- Behavior: Creates a direct relationship between error and correction
- Example: If current is 0.5A below target, KP determines how much voltage to add immediately
2. Integral Term (KI)
Role: Accumulates past errors to eliminate steady-state error (the persistent difference between target and actual).
- Effect: Corrects for systematic biases in the system
- Behavior: Builds up over time to compensate for consistent errors
- Example: If your circuit has consistent resistance that causes a persistent 0.1A shortfall, KI will gradually increase voltage to compensate
3. Derivative Term (KD)
Role: Predicts future error based on the current rate of change.
- Effect: Dampens the system response to prevent overshoot
- Behavior: Reacts to how quickly the error is changing
- Example: If current is rising too quickly toward the target, KD will reduce the correction to prevent overshooting
Tuning the PID Parameters
Step-by-Step Tuning Process
-
Start with KP:
- Set KI and KD to 0
- Increase KP until the system responds quickly but doesn't oscillate
- You should see the current approach the target but not quite reach it
-
Add KI:
- With KP set, increase KI to eliminate the remaining steady-state error
- Start with KI = KP/10 and adjust
- Watch for overshoot - if the current oscillates around the target, reduce KI
-
Add KD:
- With KP and KI set, add a small amount of KD to reduce overshoot
- Start with KD = KP/100 and adjust
- KD helps prevent the current from oscillating around the target
Practical Tuning for Battery Charging
For your battery charging application, I recommend this approach:
# Start with conservative values
KP = 0.1 # Start with a moderate proportional gain
KI = 0.01 # Start with low integral gain (10% of KP)
KD = 0.001 # Start with very low derivative gain (1% of KP)
# Tuning procedure:
# 1. Set KI and KD to 0, adjust KP until you get a fast response without oscillation
# 2. Gradually increase KI until steady-state error is eliminated
# 3. Add a small amount of KD to reduce any overshoot
Can Default Values Be Used Reliably?
No, default values cannot be reliably used without tuning. The appropriate PID values depend on:
- Your specific circuit characteristics (resistance, capacitance)
- The response time of your USB-C power supply
- The sampling rate of your control loop
- The physical characteristics of your battery
However, the values I provided in the example code are reasonable starting points for a battery charging system with:
- A control loop running at 10Hz (100ms intervals)
- Typical lithium-ion battery characteristics
- Moderate circuit resistance
Practical Tuning Tips
-
Use a dummy load first: Before connecting to a real battery, test with a resistor that approximates your expected battery resistance.
-
Monitor the response: Watch how the current responds to changes in voltage:
- If it's sluggish, increase KP
- If it oscillates, decrease KP and/or increase KD
- If it never quite reaches the target, increase KI
-
Start with small changes: Make small adjustments to one parameter at a time.
-
Consider your safety margins: Ensure your PID tuning doesn't cause the system to exceed safe voltage or current limits.
-
Test under different conditions: Batteries behave differently at different states of charge and temperatures.
Alternative to PID: Simplified Control
If PID tuning seems too complex, you can use a simpler approach:
# Simplified proportional control with integral windup protection
KP = 0.2 # Single gain factor
integral = 0
max_integral = 0.5 # Prevent integral windup
while True:
error = TARGET_CURRENT - read_charge_current()
# Proportional term
output = KP * error
# Integral term with windup protection
integral += error * 0.1
integral = max(-max_integral, min(integral, max_integral))
output += integral * 0.1
# Apply correction
new_voltage = usb_voltage + output
set_usb_supply_voltage(new_voltage)
This simplified approach often works well for battery charging applications where precise control isn't as critical as safety and reliability.