I have a Raspberry Pi Pico modified with an AP33772 USB controller that allows me to…
- User
I have a Raspberry Pi Pico modified with an AP33772 USB controller that allows me to pull specific voltages from a PPS enabled USB C power supply. The Pi also includes sensors for the USB C current and voltage, as well as the Pico's normal ADC interface. I would like to implement Constant Current lithium ion cell charging by manipulating the USB C output voltage to maintain the constant current.
Can you explain how to use a proportional control loop to implement the constant current charging stage? Feel free to use MicroPython psuedo-code.
Keep in mind that in my application, I will charge many different cells and I may use different power supplies. Any manual tuning dependent on specific hardware or cells will not be possible.
Assistant
For constant current charging using proportional control, you'll adjust the USB-C voltage to maintain your target charging current. Here's how to implement this:
The basic principle is that when current is below target, you increase voltage; when above target, you decrease voltage. The proportional gain (Kp) determines how aggressively the system responds to errors.
Here's a MicroPython implementation:
import time from machine import Timer class CCCharger: def __init__(self, usb_controller, current_sensor, voltage_sensor): self.usb = usb_controller self.current_sensor = current_sensor self.voltage_sensor = voltage_sensor # Control parameters self.target_current = 0.0 # Amps self.kp = 0.5 # Proportional gain (V/A) self.voltage_min = 3.0 # Minimum safe voltage self.voltage_max = 4.2 # Maximum cell voltage self.usb_voltage_max = 20.0 # USB-C PPS limit # State variables self.current_voltage = 5.0 # Starting voltage self.control_active = False def set_target_current(self, current_amps): """Set the target charging current in amps""" self.target_current = current_amps def read_current(self): """Read actual current from sensor""" return self.current_sensor.read_current() def read_cell_voltage(self): """Read cell voltage from sensor""" return self.voltage_sensor.read_voltage() def control_loop(self): """Main proportional control loop""" if not self.control_active: return # Read actual current actual_current = self.read_current() cell_voltage = self.read_cell_voltage() # Safety check - stop if cell voltage too high if cell_voltage >= self.voltage_max: self.stop_charging() return # Calculate error current_error = self.target_current - actual_current # Proportional control: adjust voltage based on current error voltage_adjustment = self.kp * current_error # Calculate new USB voltage new_voltage = self.current_voltage + voltage_adjustment # Apply limits new_voltage = max(self.voltage_min, min(new_voltage, self.usb_voltage_max)) # Ensure we don't exceed cell voltage limit (with some headroom for losses) max_usb_voltage = cell_voltage + 0.5 # 0.5V headroom for cable/circuit losses new_voltage = min(new_voltage, max_usb_voltage) # Set new voltage self.current_voltage = new_voltage self.usb.set_voltage(new_voltage) def start_charging(self, target_current): """Start the charging process""" self.set_target_current(target_current) self.control_active = True # Start with voltage slightly above cell voltage initial_cell_voltage = self.read_cell_voltage() self.current_voltage = initial_cell_voltage + 0.3 self.usb.set_voltage(self.current_voltage) # Set up timer for control loop (100Hz) self.timer = Timer(-1) self.timer.init(period=10, mode=Timer.PERIODIC, callback=lambda t: self.control_loop()) def stop_charging(self): """Stop charging""" self.control_active = False self.timer.deinit() self.usb.set_voltage(5.0) # Return to safe default # Usage example charger = CCCharger(usb_controller, current_sensor, voltage_sensor) # Start charging at 1A charger.start_charging(target_current=1.0) # Monitor charging while charger.control_active: current = charger.read_current() voltage = charger.read_cell_voltage() print(f"Current: {current:.3f}A, Cell Voltage: {voltage:.3f}V") time.sleep(1)For automatic adaptation without manual tuning, consider these enhancements:
- Adaptive Gain: Start with conservative Kp and increase it if the system responds too slowly:
def adaptive_control_loop(self): # Track error over time current_error = self.target_current - self.read_current() # If error persists, increase gain slightly if abs(current_error) > 0.1 and abs(current_error) < abs(self.last_error): self.kp *= 1.1 # Increase gain by 10% elif abs(current_error) > abs(self.last_error): self.kp *= 0.9 # Decrease if oscillating self.kp = max(0.1, min(self.kp, 2.0)) # Keep within safe bounds self.last_error = current_error- Anti-windup: Prevent voltage from increasing when current can't increase (e.g., due to resistance):
def control_loop_with_antiwindup(self): # Only increase voltage if current is actually responding if voltage_adjustment > 0 and actual_current < self.last_current - 0.01: # Current decreased despite voltage increase - likely at limit voltage_adjustment = 0 self.last_current = actual_current- Soft Start: Gradually ramp up current to avoid overshoots:
def soft_start(self, final_current, ramp_time=2.0): steps = int(ramp_time * 100) # 100Hz control loop for i in range(steps): intermediate_target = final_current * (i + 1) / steps self.set_target_current(intermediate_target) time.sleep(0.01)The key to hardware-independent operation is starting with conservative parameters and letting the system adapt based on observed behavior. The proportional controller will naturally find the right voltage for any cell/cable combination as long as the gain isn't too high (which would cause oscillation) or too low (which would make it sluggish).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.