# Import necessary modules
import machine # For ADC and other hardware access
import time # For delays in the control loop
# Constants for the system
DESIRED_CURRENT = 1.0 # Setpoint: Desired charging current in Amps (e.g., 1A)
KP = 0.1 # Proportional gain: Tune this! Start low (e.g., 0.01) and increase for better response
MIN_VOLTAGE = 3.0 # Minimum safe voltage for the USB-C output (Volts)
MAX_VOLTAGE = 4.2 # Maximum safe voltage (e.g., for Li-Ion cell)
LOOP_DELAY = 0.1 # Delay between control loops in seconds (e.g., 100ms)
# ADC setup for current sensor
# Assuming you're using an ADC pin connected to your current sensor (e.g., a voltage output proportional to current)
ADC_PIN = 26 # Example: ADC0 on Pico
adc_current = machine.ADC(ADC_PIN)
# Function to read and convert ADC value to current (Amps)
# This is a placeholder; calibrate based on your sensor (e.g., using a shunt resistor)
def read_current():
raw_value = adc_current.read_u16() # Read raw 16-bit value (0-65535)
# Example conversion: Assume sensor outputs 0-3.3V proportional to 0-2A current
voltage = (raw_value / 65535) * 3.3 # Convert to voltage (0-3.3V)
current = (voltage / 3.3) * 2.0 # Scale to 0-2A (adjust based on your sensor)
return current # Returns current in Amps
# Function to set the USB-C output voltage via AP33772
# This is a placeholder; replace with your actual code (e.g., I2C commands to AP33772)
def set_usb_voltage(voltage):
# Example: If AP33772 is controlled via I2C, you might send a command here
# For pseudo-code, just print it or simulate it
print(f"Setting USB voltage to {voltage}V") # Replace with real implementation
# Actual code might look like: i2c.writeto(ADDRESS, bytes_for_voltage)
# Main function for the proportional control loop
def constant_current_charging():
current_set_voltage = 3.7 # Initial voltage (e.g., a reasonable starting point for Li-Ion)
set_usb_voltage(current_set_voltage) # Set the initial voltage
while True: # Main control loop
measured_current = read_current() # Read the actual current from the sensor
# Calculate the error
error = DESIRED_CURRENT - measured_current # Error = Setpoint - Measured
# Compute the proportional output (voltage adjustment)
voltage_adjustment = KP * error # This gives the change in voltage
# Apply the adjustment to the current voltage
new_voltage = current_set_voltage + voltage_adjustment
# Clamp the voltage to safe limits to prevent over/under-voltage
if new_voltage < MIN_VOLTAGE:
new_voltage = MIN_VOLTAGE
elif new_voltage > MAX_VOLTAGE:
new_voltage = MAX_VOLTAGE
# Update and set the new voltage
current_set_voltage = new_voltage
set_usb_voltage(current_set_voltage)
# Optional: Print for debugging
print(f"Measured Current: {measured_current:.2f}A, Error: {error:.2f}A, New Voltage: {new_voltage:.2f}V")
# Add a delay to allow the system to stabilize and prevent CPU overload
time.sleep(LOOP_DELAY)
# Safety check: Stop if battery voltage reaches limit (assuming you have a way to read it)
# battery_voltage = read_battery_voltage() # Implement this if you have a sensor
# if battery_voltage >= 4.2: # Example threshold
# print("Battery voltage reached limit; stopping charging")
# break # Exit the loop
# Run the charging function
try:
constant_current_charging()
except Exception as e:
print(f"Error: {e}")
# Add cleanup code if needed, e.g., set voltage to 0